Click here to Skip to main content
15,889,808 members
Please Sign up or sign in to vote.
2.50/5 (2 votes)
See more:
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
C#
protected void Page_Load(object sender, EventArgs e)
{
    if(!IsPostBack)
    {
        GridView_QueryResult.DataSource = dataTable_QueryResult;
        GridView_QueryResult.DataBind();
    }
}


The datatable is a SQL result and inserted to cache, the code is:
C#
Cache.Insert("dataTable_QueryResult", dataTable_QueryResult, null, DateTime.MaxValue, TimeSpan.FromMinutes(10));


The datatable is bound to the gridview using such code:
C#
DataTable dataTable_QueryResult = new DataTable();
dataTable_QueryResult = (DataTable)Cache["dataTable_QueryResult"];                       GridView_QueryResult.DataSource = dataTable_QueryResult;
GridView_QueryResult.DataBind();


The code of the Button even for clearing the gridview is:
C#
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's sorting event code is:
C#
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
C#
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!
HTML
EnableEventValidation="false" .
Posted
Comments
Sergey Alexandrovich Kryukov 19-Oct-12 12:44pm    
Take a look at each immediate string constants like "SortDirection", and especially "ASC" and "DESC" (what, you don't know enumeration types?!). As soon as you hard-code them, your programming makes little to no sense, and you code does not worth consideration. First, you need to re-write your code in acceptable manner.
--SA
daiwuju 23-Oct-12 10:30am    
Thanks for your suggestion. I rewrite the sorting code, and now it works.

1 solution

I tried to create the same project and its working fine in my side.Is it possible for you share the complete solution.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900