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?
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();
}
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();
}
You are missing the OnPreRender event on the datapager!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With