I found a problem with the GridView pager in ASP.NET 4.5 and 4.5.1. Starting with .NET 2-4, I have never encountered such a problem.
By the time, I have a gridview that I populate with data in code like this:
protected int CurrentPage { get { return SearchResults.PageIndex + 1; } } protected void Page_Load(object sender, EventArgs e) { if(!IsPostBack) BindGrid(); } private void BindGrid() { int totalRowCount = 0; SearchResults.DataSource = GetPageData(SearchResults.PageIndex, SearchResults.PageSize, out totalRowCount); SearchResults.VirtualItemCount = totalRowCount; SearchResults.DataBind(); } private IEnumerable GetPageData(int start, int count, out int totalRowCount) { return Membership.GetAllUsers(start, count, out totalRowCount); } protected void SearchResults_PageIndexChanging(object sender, GridViewPageEventArgs e) { SearchResults.PageIndex = e.NewPageIndex; BindGrid(); }
The problem is that if I get to the last page of the GridView and I try to return to any other page, my IndexChanging page does not work. The problem only occurs if the last page does not have the same number of entries, such as PageSize. The behavior is that my page reloads, the gridview page is filled with empty rows of data to the PageSize value. VirtualItemCount correctly represents the generic ItemCount.
Markup if you find something there:
<asp:GridView runat="server" CellPadding="0" CellSpacing="0" GridLines="None" CssClass="table table-condensed table-striped table-footer" ID="SearchResults" AllowCustomPaging="true" AllowPaging="true" PageSize="6" OnPageIndexChanging="SearchResults_PageIndexChanging" AutoGenerateColumns="false" UseAccessibleHeader="true"> ... <PagerTemplate> <span class="pull-left"> <strong><%= SearchResults.PageIndex * SearchResults.PageSize + 1 %></strong> - <strong><%= CurrentPage * SearchResults.PageSize %></strong> </span> <span class="pull-left"> Total records: <strong><%= SearchResults.VirtualItemCount %></strong> </span> <ul class="pagination pull-right"> <li><asp:LinkButton runat="server" CommandName="Page" CommandArgument="First"><span class="glyphicon glyphicon-backward"></span></asp:LinkButton></li> <li><asp:LinkButton runat="server" CommandName="Page" CommandArgument="<%# CurrentPage - 2 %>" Visible="<%# CurrentPage > 2 %>"><%= CurrentPage - 2 %> </asp:LinkButton></li> <li><asp:LinkButton runat="server" CommandName="Page" CommandArgument="<%# CurrentPage - 1 %>" Visible="<%# CurrentPage > 1 %>"><%= CurrentPage - 1 %> </asp:LinkButton></li> <li class="active"><a href="#"><%= CurrentPage %></a></li> <li><asp:LinkButton runat="server" CommandName="Page" CommandArgument="<%# CurrentPage + 1 %>" Visible="<%# CurrentPage < SearchResults.PageCount %>"><%= CurrentPage + 1 %></asp:LinkButton></li> <li><asp:LinkButton runat="server" CommandName="Page" CommandArgument="<%# CurrentPage + 2 %>" Visible="<%# CurrentPage < SearchResults.PageCount - 1 %>"><%= CurrentPage + 2 %></asp:LinkButton></li> <li><asp:LinkButton runat="server" CommandName="Page" CommandArgument="Last"><span class="glyphicon glyphicon-forward"></span></asp:LinkButton></li> </ul> </PagerTemplate> </asp:GridView>
Thank you very much, I have been doing this for several days. Of course, I could use the QueryString approach, but since I will use many tables, I would like to stick with the postback method, if possible ...
EDIT:
The simplest workaround I found was doing a BindGrid on each page of the_Load. For some reason, the PageIndexChanging function just doesn't work on the last page if LastPageSize == PageSize. Then the DataBind is not called to bind the CommandArguments, so I cannot correctly postback.
On the other hand, this is not very clear and can cause problems ... At least double binding = double SQL calls for data on the change page ... Otherwise, I have no idea how to force PageIndexChanging here and it seems like a new problem .NET is for me.