分页或排序后清除gridview的问题 [英] Problem in clearing the gridview after paging or sorting

查看:71
本文介绍了分页或排序后清除gridview的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个gridview,其中datasouce是一个数据表.每次我加载要显示的数据,然后清除GridView中所有付清的数据时,它都可以正常工作.但是,如果我加载数据并进行排序或分页操作,然后清除所有显示的数据,则会出现错误:

无效的回发或回调参数.使用< pages enableeventvalidation ="true">启用事件验证.在配置中或<%@页面EnableEventValidation ="true"%>在页面中.为了安全起见,此功能验证回发或回调事件的参数源自最初呈现它们的服务器控件.如果数据有效且预期,请使用ClientScriptManager.RegisterForEventValidation方法以注册回发或回调数据以进行验证.

页面加载事件为

I have a gridview which datasouce is a datatable. Everytime I load data for display and then clear all diapayed data in the GridView, it works fine. But if I load the data, and do a sorting or paging operation, then clear all the data displyed, there comes the error:

Invalid postback or callback argument. Event validation is enabled using <pages enableeventvalidation="true"> in configuration or <%@ Page EnableEventValidation="true" %> in a page. For security purposes, this feature verifies that arguments to postback or callback events originate from the server control that originally rendered them. If the data is valid and expected, use the ClientScriptManager.RegisterForEventValidation method in order to register the postback or callback data for validation.

The page load event is

protected void Page_Load(object sender, EventArgs e)
{
    if(!IsPostBack)
    {
        GridView_QueryResult.DataSource = dataTable_QueryResult;
        GridView_QueryResult.DataBind();
    }
}



数据表是一个SQL结果,并插入到缓存中,代码为:



The datatable is a SQL result and inserted to cache, the code is:

Cache.Insert("dataTable_QueryResult", dataTable_QueryResult, null, DateTime.MaxValue, TimeSpan.FromMinutes(10));



使用以下代码将数据表绑定到gridview:



The datatable is bound to the gridview using such code:

DataTable dataTable_QueryResult = new DataTable();
dataTable_QueryResult = (DataTable)Cache["dataTable_QueryResult"];                       GridView_QueryResult.DataSource = dataTable_QueryResult;
GridView_QueryResult.DataBind();



即使清除网格视图,Button的代码也是:



The code of the Button even for clearing the gridview is:

protected void Button_DataClear_Click(object sender, EventArgs e)
{
    dataTable_QueryResult.Clear();
    GridView_QueryResult.DataSource = dataTable_QueryResult;
    GridView_QueryResult.DataBind();

    //clear the cache inserted by the datatable
    List<string> keys = new List<string>();
    IDictionaryEnumerator enumerator = Cache.GetEnumerator();
    while (enumerator.MoveNext())
    {
        keys.Add(enumerator.Key.ToString());
    }
    for (int i = 0; i < keys.Count; i++)
    {
        if (keys[i] == "dataTable_QueryResult")
            Cache.Remove(keys[i]);
    }
}



Gridview的排序事件代码为:



Gridview''s sorting event code is:

protected void GridView_QueryResult_Sorting(object sender, GridViewSortEventArgs e)
{
    if (Cache["dataTable_QueryResult"] != null)
    {
        dataTable_QueryResult = new DataTable();
        dataTable_QueryResult = (DataTable)Cache["dataTable_QueryResult"];
        DataView dv = new DataView(dataTable_QueryResult);
        string sortDirection = GetSortDirection();
        sortOrder = e.SortExpression + " " + sortDirection;
        dv.Sort = sortOrder;
        GridView_QueryResult.DataSource = dv;
        GridView_QueryResult.DataBind();
    }
}
private string GVSortDirection
{
    get
    {
        return ViewState["SortDirection"] as string ?? "DESC";
    }
    set
    {
        ViewState["SortDirection"] = value;
    }
}

private string GetSortDirection()
{
    switch (GVSortDirection)
    {
        case "ASC":
            GVSortDirection = "DESC";
            break;
        case "DESC":
            GVSortDirection = "ASC";
            break;
    }
    return GVSortDirection;
}



寻呼事件的代码是



The pageing event''s code is

protected void GridView_QueryResult_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
    GridView_QueryResult.PageIndex = e.NewPageIndex;
    int recordCount = 0;

    if (Cache["dataTable_QueryResult"] != null)
    {
        DataTable dataTable_QueryResult = new DataTable();
        dataTable_QueryResult = (DataTable)Cache["dataTable_QueryResult"];
        DataView dv = new DataView(dataTable_QueryResult);
        dv.Sort = sortOrder;
        GridView_QueryResult.DataSource = dv;
        GridView_QueryResult.DataBind();
        recordCount = dataTable_QueryResult.Rows.Count;
    }
	
    int pageCount = (int)Math.Ceiling(recordCount * 1.0 / GridView_QueryResult.PageSize);

    if (recordCount == 0)
    {
        GridView_QueryResult.PageIndex = 0;
    }
    else if (GridView_QueryResult.PageIndex >= pageCount)
    {
        GridView_QueryResult.PageIndex = pageCount - 1;
    }
}



现在,我想知道为什么以及除了以下设置之外的解决方案,谢谢!



Now, I want to know why and a solution other than the following seting, Thanks!

EnableEventValidation="false" .

推荐答案

我试图创建相同的项目,并且在我这方面工作正常.您有可能分享完整的解决方案吗?
I tried to create the same project and its working fine in my side.Is it possible for you share the complete solution.


这篇关于分页或排序后清除gridview的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆