ASP.Net:DataPager控件总是以寻呼落后一步 [英] ASP.Net : DataPager Control always a step behind with paging

查看:210
本文介绍了ASP.Net:DataPager控件总是以寻呼落后一步的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

看看下面的例子......在网页的的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的 事件,而不是在的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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆