vs2008中datapager分页问题的解决方法
来自:种豆
时间:2017-12-29
阅读:707次
原文链接
今天看了一下ListView和DataPager配合做数据分页的教程,感觉很方便功能很强大,用在自己的项目上面时却出现了问题,具体表现在点击上一页、下一页或者数字跳转页面时不跳页或是乱跳页,并且有时不返回数据。
public partial class ListViewTest : System.Web.UI.Page
...{
protected void Page_Load(object sender, EventArgs e)
...{
if (!Page.IsPostBack)
BindData();
}
protected void BindData()
...{
try
...{
lv.DataSource = Tag.GetList();//使用LINQ通过微软企业库获得信息返回泛型的LIST<lv>对象
lv.DataBind();
}
catch (Exception ex)
...{
MelHelper.HandleException(ex);
}
}
}
出现如开始提及的问题。后来在在网上找到了两个关于上述提到的datapager分页问题的解决方案。
1 把Page_Load里的数据绑定移到Page_PreRender中
public partial class ListViewTest : System.Web.UI.Page
...{
protected void Page_Load(object sender, EventArgs e)
...{
//if (!Page.IsPostBack)
// BindData();
}
protected void Page_PreRender(object sender, EventArgs e)
...{
BindData();
}
......
}
2 在PagePropertiesChanging中重新绑定
public partial class ListViewTest : System.Web.UI.Page
...{
protected void Page_Load(object sender, EventArgs e)
...{
if (!Page.IsPostBack)
BindData();
}
protected void Page_PreRender(object sender, EventArgs e)
...{
//BindData();
}
protected void lv_PagePropertiesChanging(object sender, PagePropertiesChangingEventArgs e)
...{
dp.SetPageProperties(e.StartRowIndex, e.MaximumRows, false);
BindData();
}
......
}