简体   繁体   English

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

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

Take the following example...a page with a ListView and a DataPager used for paging the data of the ListView : 以下示例为例:一个包含ListViewDataPager的页面,用于分页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. 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. 例如,当页面加载它在页码1时。然后当您单击第3页时,它在回发后保留在第1页。 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. 然后你点击第5页,在回发后它在第3页找到自己...然后你点击第6页,它在第5页找到自己......依此类推。

Why isn't the paging working as expected? 为什么分页不按预期工作?

Solution

The problem is due to the binding occuring on the Page_Load event. 问题是由于Page_Load事件发生绑定。

For this to work as expected, the binding needs to happen in the DataPager 's OnPreRender event , not in the Page_Load . 为了使其按预期工作, 绑定需要在DataPagerOnPreRender事件中发生 ,而不是在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();    
}

I ran into this same problem, but binding everytime on datapager prerender was not an option for me. 我遇到了同样的问题,但每次在datapager prerender上绑定对我来说都不是一个选择。 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. 该解决方案可用作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. 通过附加到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();
}

您错过了数据包中的OnPreRender事件!

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

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: 或者,如果要构建仅包含ListView的User控件 ,则只需将pager事件处理程序指向Page_Load方法,因为Page_Load方法没有运行任何其他方法:

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

in the page load you should put the code between if (!IsPostBack) { } 在页面加载中你应该把代码放在if(!IsPostBack){}之间

It will solve your problem. 它会解决你的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM