ASP.Net:DataPager控件总是以寻呼落后一步 [英] ASP.Net : DataPager Control always a step behind with paging
问题描述
看看下面的例子......在网页的的ListView
和 DataPager的
用于寻呼的数据在的ListView
:
Take the following example...a page with a ListView
and a DataPager
used for paging the data of the ListView
:
的 code背后:的
protected void Page_Load(object sender, EventArgs e)
{
MyList.DataSource = GetSomeList();
MyList.DataBind();
}
的来源:的
<asp:ListView ID="MyList" runat="server">
<% //LayoutTemplate and ItemTemplate removed for the example %>
</asp:ListView>
<asp:DataPager ID="ListPager" PagedControlID="MyList" runat="server" PageSize="10">
<Fields>
<asp:NumericPagerField />
</Fields>
</asp:DataPager>
同的问题 DataPager的
是,它始终是一个步骤,后面与结合
The problem with the DataPager
is that it is always a step-behind with the binding.
例如,当页面加载它是在页号1。然后当你点击第3页,它回发后保持第1页。然后单击第5页,并且回传后,发现自己3页...,之后单击第6页,它发现自己5页...等等等等。
For example, when the page loads it's on page number 1. Then when you click on page 3, it stays on page 1 after the postback. Then you click on page 5, and after the postback it finds itself on page 3...and after that you click on page 6, and it finds itself on page 5...and so on and so forth.
为什么不分页按预期工作?
Why isn't the paging working as expected?
推荐答案
问题是由于对的Page_Load
事件的绑定发生的历史。
Solution
The problem is due to the binding occuring on the Page_Load
event.
对于这个工作如预期,绑定需要在的在preRender
,而不是在 DataPager的
$发生C $ C>事件的Page_Load
。
For this to work as expected, the binding needs to happen in the DataPager
's OnPreRender
event, not in the Page_Load
.
的来源:的
<asp:DataPager ID="ListPager" PagedControlID="MyList" runat="server" PageSize="10"
OnPreRender="ListPager_PreRender">
<Fields>
<asp:NumericPagerField />
</Fields>
</asp:DataPager>
的 code背后:的
protected void Page_Load(object sender, EventArgs e)
{
//Binding code moved from Page_Load
//to the ListView's PreRender event
}
protected void ListPager_PreRender(object sender, EventArgs e)
{
MyList.DataSource = GetSomeList();
MyList.DataBind();
}
这篇关于ASP.Net:DataPager控件总是以寻呼落后一步的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!