ASP.Net:DataPager 控件始终落后于分页
举个例子...一个带有 ListView
和一个 DataPager
的页面,用于对 ListView
的数据进行分页:
代码隐藏:
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
的问题在于它始终落后于绑定。
例如,当页面加载时,它位于第 1 页。然后,当您单击第 3 页时,回发后它仍保留在第 1 页。 然后您单击第 5 页,回发后它发现自己位于第 3 页...然后您单击第 6 页,它发现自己位于第 5 页...等等。
为什么分页没有按预期工作?
Take the following example...a page with a ListView
and a DataPager
used for paging the data of the ListView
:
Code Behind:
protected void Page_Load(object sender, EventArgs e)
{
MyList.DataSource = GetSomeList();
MyList.DataBind();
}
Source:
<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>
The problem with the DataPager
is that it is always a step-behind with the binding.
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
解决方案
该问题是由于
Page_Load
事件上发生的绑定造成的。为了使其按预期工作,绑定需要在
DataPager
的OnPreRender
事件中进行,而不是在Page_Load 中进行代码>.
来源:
背后代码:
Solution
The problem is due to the binding occuring on the
Page_Load
event.For this to work as expected, the binding needs to happen in the
DataPager
'sOnPreRender
event, not in thePage_Load
.Source:
Code Behind:
我遇到了同样的问题,但每次在数据分页器预渲染上绑定对我来说不是一个选择。 相反,我可以通过仅在分页发生时进行绑定来完成同样的事情。 该解决方案可以用作 Andreas 预渲染解决方案的替代方案。 以下内容对我有用:
通过附加到 ListView 的 PagePropertiesChanged 事件,我能够纠正分页问题,而无需绑定数据分页器的每个预呈现器。
注意: 大多数数据分页器属性都是在外观文件中设置的,这就是它们不在标记中的原因。
标记:
隐藏代码:
I ran into this same problem, but binding everytime on datapager prerender was not an option for me. Instead, I was able to accomplish much the same thing by binding only when the paging occurred. This solution can be used as an alternative to the prerender solution by Andreas. The following worked for me:
By attaching to the ListView's PagePropertiesChanged event, I was able to correct the paging issue without having to bind on every prerender of the data pager.
NOTE: Most of the data pager properties are setup in a skin file, which is why they are not in the markup.
Markup:
Code Behind:
您缺少数据分页器上的 OnPreRender 事件!
You are missing the OnPreRender event on the datapager!
以下作品非常适合我。
Following works perfect for me.
或者,如果您正在构建仅包含 ListView 的用户控件,则只需将分页器事件处理程序指向
Page_Load
方法,因为 Page_Load 方法不运行任何内容别的:Alternatively, if you are building a User control only containing the ListView, you can simply point the pager event handler to the
Page_Load
method, since the Page_Load method isn't running anything else:在页面加载中,您应该将代码放在
if (!IsPostBack)
{
它
会解决你的问题。
in the page load you should put the code between
if (!IsPostBack)
{
}
It will solve your problem.