views:

1329

answers:

3

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.
  • Clicks the pager to change to a specific page (lets say 5)
  • Clicks on a link to navigate to another page
  • Presses the back button in their browser to return to the page with the GridView
  • Grid is now reverted back to it's original state (on page 1) since the browser didn't track that, and so the user clicks to go to page 5 again, resulting in the error

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.

A: 

What happenes is exactly what the error says - the control the user tries to use after clicking the Back button is not the original one, since it's loaded from cache. This could be identified by volatile data you have there or by a timestamp - that I don't know.

What you could do is to put

<%@ Page EnableEventValidation="false" %>

on that page, and let us know if that worked and wether it broke anything else on your page...

synhershko
That would do the trick; but I'd rather not turn off validation if there's another way around the problem
John
A: 

After searching I couldn't really find a reasonable answer; however, I did find that you can use the 3.5 preview controls w/ asp.net 2.0, so I added the History control to the page, and let that handle the back button issues.

Looks like it resolved the issue

Thanks!

John
A: 

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)
Jun