I'm trying to make use of asp:GridView's built in paging functionality, but cannot seem to get the "Last Page" button to work when the mode is set to NumericFirstLast. Here's the code sample:
<asp:GridView ID="GridView1" runat="server" AllowSorting="true" AllowPaging="true"
PagerSettings-Mode="NumericFirstLast" PageSize="15"
OnPageIndexChanging="GridView1_PageIndexChanging">
<%-- Column Definitions --%>
</asp:GridView>
// C# Behind
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
LoadGrid();
}
}
public void LoadGrid()
{
// Assign DataSource
GridView1.DataBind();
}
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
LoadGrid();
}
What's happening is when the last button is being clicked on the pager e.NewPageIndex
holds a value of -1, meaning the assignment throws an exception. However, the GridView1.PageCount
during the index changing event is 0 so I cannot use that to set the page index to count - 1. Any suggestions as to what is going on with this code or why I am receiving a -1 from the event args?
Well, I cannot explain why, nor find the answer in MSDN documentation, but as Ehsan suggested in the comments above, when e.NewPageIndex == -1
if you set GridView1.PageIndex
to Int32.MaxValue
it forces the grid to the last page.
If anyone happens to find documentation for why/how this works, please post a comment to that!