ASP.Net:DataPager 控件始终落后于分页

发布于 2024-07-27 05:24:24 字数 851 浏览 10 评论 0原文

举个例子...一个带有 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

硪扪都還晓 2024-08-03 05:24:24

解决方案

该问题是由于 Page_Load 事件上发生的绑定造成的。

为了使其按预期工作,绑定需要在 DataPagerOnPreRender 事件中进行,而不是在 Page_Load 中进行代码>.

来源:

<asp:DataPager ID="ListPager" PagedControlID="MyList" runat="server" PageSize="10"
    OnPreRender="ListPager_PreRender">

<Fields>
        <asp:NumericPagerField  />
    </Fields>
</asp:DataPager>

背后代码:

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();    
}

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's OnPreRender event, not in the Page_Load.

Source:

<asp:DataPager ID="ListPager" PagedControlID="MyList" runat="server" PageSize="10"
    OnPreRender="ListPager_PreRender">

<Fields>
        <asp:NumericPagerField  />
    </Fields>
</asp:DataPager>

Code Behind:

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();    
}
过气美图社 2024-08-03 05:24:24

我遇到了同样的问题,但每次在数据分页器预渲染上绑定对我来说不是一个选择。 相反,我可以通过仅在分页发生时进行绑定来完成同样的事情。 该解决方案可以用作 Andreas 预渲染解决方案的替代方案。 以下内容对我有用:

通过附加到 ListView 的 PagePropertiesChanged 事件,我能够纠正分页问题,​​而无需绑定数据分页器的每个预呈现器。

注意: 大多数数据分页器属性都是在外观文件中设置的,这就是它们不在标记中的原因。

标记:

<asp:DataPager ID="ListPager" runat="server" PagedControlID="MyList" />
<asp:ListView ID="MyList" runat="server">
    <% //LayoutTemplate and ItemTemplate removed for the example %>
</asp:ListView>

隐藏代码:

protected void Page_Load(object sender, EventArgs e) {
   MyList.PagePropertiesChanged += new EventHandler(MyList_PagePropertiesChanged);
}

/// <summary>
/// Handles the situation where the page properties have changed.  Rebind the data
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void MyList_PagePropertiesChanged(object sender, EventArgs e) {
   MyList.DataSource = GetSomeList();
   MyList.DataBind();
}

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:

<asp:DataPager ID="ListPager" runat="server" PagedControlID="MyList" />
<asp:ListView ID="MyList" runat="server">
    <% //LayoutTemplate and ItemTemplate removed for the example %>
</asp:ListView>

Code Behind:

protected void Page_Load(object sender, EventArgs e) {
   MyList.PagePropertiesChanged += new EventHandler(MyList_PagePropertiesChanged);
}

/// <summary>
/// Handles the situation where the page properties have changed.  Rebind the data
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void MyList_PagePropertiesChanged(object sender, EventArgs e) {
   MyList.DataSource = GetSomeList();
   MyList.DataBind();
}
忆沫 2024-08-03 05:24:24

您缺少数据分页器上的 OnPreRender 事件!

You are missing the OnPreRender event on the datapager!

乱了心跳 2024-08-03 05:24:24

以下作品非常适合我。

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles   Me.Load
Dim ds As DataSet
ds = SQLHELPER.ExecuteDataSet(CommandType.StoredProcedure, "sp_Locations")
rs.EnableViewState = False
rs.DataSource = ds
rs.DataBind()
End Sub

Protected Sub rs_PagePropertiesChanging(ByVal sender As Object, ByVal e As    PagePropertiesChangingEventArgs)
'set current page startindex, max rows and rebind to false
Pager.SetPageProperties(e.StartRowIndex, e.MaximumRows, False)
'rebind List View
rs.DataBind()
End Sub

<asp:ListView ID="rs" runat="server" onpagepropertieschanging="rs_PagePropertiesChanging">

Following works perfect for me.

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles   Me.Load
Dim ds As DataSet
ds = SQLHELPER.ExecuteDataSet(CommandType.StoredProcedure, "sp_Locations")
rs.EnableViewState = False
rs.DataSource = ds
rs.DataBind()
End Sub

Protected Sub rs_PagePropertiesChanging(ByVal sender As Object, ByVal e As    PagePropertiesChangingEventArgs)
'set current page startindex, max rows and rebind to false
Pager.SetPageProperties(e.StartRowIndex, e.MaximumRows, False)
'rebind List View
rs.DataBind()
End Sub

<asp:ListView ID="rs" runat="server" onpagepropertieschanging="rs_PagePropertiesChanging">
悍妇囚夫 2024-08-03 05:24:24

或者,如果您正在构建仅包含 ListView 的用户控件,则只需将分页器事件处理程序指向 Page_Load 方法,因为 Page_Load 方法不运行任何内容别的:

<asp:DataPager ID="ListPager" PagedControlID="MyList" runat="server" PageSize="10"
OnPreRender="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:

<asp:DataPager ID="ListPager" PagedControlID="MyList" runat="server" PageSize="10"
OnPreRender="Page_Load">
不弃不离 2024-08-03 05:24:24

在页面加载中,您应该将代码放在
if (!IsPostBack)
{

会解决你的问题。

in the page load you should put the code between
if (!IsPostBack)
{
}

It will solve your problem.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文