我有一个简单的页面,初始加载时绑定到 GridView。这个 GridView 启用了排序和分页,并且还被 UpdatePanel 包围着。
当用户执行以下操作时,我会收到此错误:
当用户执行以下操作时,我会收到此错误:
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.
- 点击分页器以跳转到指定页面(比如第5页)
- 点击链接以导航到另一个页面
- 按下浏览器的后退按钮返回带有GridView的页面
- 由于浏览器未跟踪,因此网格现在已恢复为其原始状态(在第1页),因此用户再次单击以转到第5页,从而导致错误。
有人知道是什么原因引起的吗?只有在单击同一页时才似乎存在问题。如果在返回访问时单击不同的页面,则没有问题。如果对列进行排序,然后在返回访问时再次排序,也没有问题。我不确定关于两次单击页面特别会导致问题的具体原因。
这是分页器的代码:
protected void gvResults_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
gvResults.DataSource = SearchResults;
gvResults.PageIndex = e.NewPageIndex;
gvResults.DataBind();
}
其中gvResults是GridView,“SearchResults”是存储在viewstate中的列表。
编辑
似乎当用户返回页面时(恢复到第1页),虽然GridView没有显示第5页,但出于某种原因浏览器确实保存了视图状态,并且将GridView保存在第5页。因此,如果我单击第4页并通过分页事件的代码,我可以看到它认为它所在的页面是5……即使显示的内容是第1页的内容。
故事的寓意是显然当用户单击“返回”按钮返回页面时,视图状态得到了保存,但显示的表格却没有。