在一个UpdatePanel中放入一gridView,点击分页时却需要点击两次才显示正确内容,刚开始的时候以为是UpdatePanel问题,在网上搜索得到了一些结论但没能解决问题。通过fiddler监控http信息能看到在第一次点击时传递的信息是正确的,如下图:
能够传递正确的页码,但返回的页面数据却不正确。所以应该是代码问题,因为表格数据是通过自定义过程读取,所以每次页面提交时都需要重新获取数据,Gridview不能通过viewstate属性传递其数据源的。
获取数据的代码如:
private void GetData()
{
localhost.QueryCondition condition = null;
if (txtSearchValue.Value != "")//为空查询全部
{
condition = new localhost.QueryCondition
{
FieldName = slctSearchFields.Value,
FieldOper = slctCondition.Value,
FieldValue = txtSearchValue.Value
};
}
localhost.CominfoTemplateService1 service = new localhost.CominfoTemplateService1();
gridInfo.DataSource = service.SearchComsByUserName(Context.User.Identity.Name, condition);
gridInfo.DataBind();
}
而在表格分页代码是这样的。
protected void gridInfo_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GetData();
gridInfo.PageIndex = e.NewPageIndex;
}跟踪代码发现在grid的databind后,就会触发rowdata_bind事件,会填充数据,而此时表格的页码还没有发生变化,调用两个语句的顺序为:gridInfo.PageIndex = e.NewPageIndex;
GetData();问题解决。