I have a simple page that, on the initial load, databinds to a GridView. This gridview has sorting and paging enabled, and is also surrounded by an UpdatePanel.
When the user does the following, I receive this 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.
Anyone know what would cause this? It only seems to be a problem when clicking on the same page. If a different page is clicked on the return visit, it's fine. If a column is sorted, then sorted again on the return visit, that is also fine. I'm not sure what specifically about clicking the page twice is causing the problem.
Here's the code for the pager:
protected void gvResults_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
gvResults.DataSource = SearchResults;
gvResults.PageIndex = e.NewPageIndex;
gvResults.DataBind();
}
where gvResults is the GridView, and 'SearchResults' is a List stored in the viewstate.
edit
It appears that although the gridview isn't displaying page 5 when the user returns to the page (reverts back to page 1), for some reason the browser did save the viewstate, and has the gridview at page 5. So if I click on page 4 and go through the code for the paging event, I can see that it thinks the page it was on was 5... even though the displayed content was for page 1.
Moral of the story is apparently the viewstate is getting saved when the user clicks the back button to return to the page, but the displayed table is not.
This error is related with the cached content in browser. So the solution is to disallow caching this page in browser:
Response.Cache.SetCacheability(HttpCacheability.NoCache)
Response.Cache.SetExpires(DateTime.Now)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With