C# ASP.Net : DataPager Control 总是落后于分页

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1130439/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 08:55:33  来源:igfitidea点击:

ASP.Net : DataPager Control always a step behind with paging

c#asp.netlistviewdatapager

提问by Andreas Grech

Take the following example...a page with a ListViewand a DataPagerused for paging the data of the ListView:

以下面的例子为例……一个带有 aListView和 a的页面DataPager用于对 的数据进行分页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 DataPageris that it is always a step-behind with the binding.

的问题DataPager在于它总是落后于绑定。

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.

例如,当页面加载时它位于第 1 页。然后当您单击第 3 页时,它在回发后停留在第 1 页。然后您单击第 5 页,在回发后它会在第 3 页上找到自己...然后您单击第 6 页,它会在第 5 页上找到自己...依此类推。

Why isn't the paging working as expected?

为什么分页没有按预期工作?

采纳答案by Andreas Grech

Solution

解决方案

The problem is due to the binding occuring on the Page_Loadevent.

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

For this to work as expected, the binding needs to happen in the DataPager's OnPreRenderevent, not in the Page_Load.

为了使其按预期工作,绑定需要发生在DataPager's OnPreRenderevent 中,而不是在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();    
}

回答by Jay S

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:

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

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.

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

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

回答by NMC

You are missing the OnPreRender event on the datapager!

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

回答by Sandip Patel

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

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

It will solve your problem.

它会解决你的问题。

回答by urooj.org

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">

回答by Hugo Nava Kopp

Alternatively, if you are building a User controlonly containing the ListView, you can simply point the pager event handler to the Page_Loadmethod, since the Page_Load method isn't running anything else:

或者,如果您正在构建仅包含 ListView的用户控件,您可以简单地将寻呼机事件处理程序指向该Page_Load方法,因为 Page_Load 方法没有运行其他任何东西:

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