A DataGrid was returning an invalid CurrentPageIndex value error when the associated Search button was clicked. The actual error is given below:
DotNetNuke.Services.Exceptions.ModuleLoadException:
Invalid CurrentPageIndex value. It must be >= 0 and < the PageCount.
---> System.Web.HttpException: Invalid CurrentPageIndex value. It must be
>= 0 and < the PageCount. at ...
A search box, with image button, was implemented with a subroutine to handle the clicking of the image button. Clicking on the image button, changes the parameters to populate the DataGrid.
Initial use of the search caused the error given above, indicating that the assigned page index was incorrect. The previous page number had been maintained, which was now larger than the current number of pages.
Resetting the value of CurrentPageIndex to 0, ensured that if there was only sufficient data for a single page then there would be no error. It is also the first page, page numbering showing as starting at 1, but in code terms it starts at 0. Settings a value also ensures that it doesn’t carry over a previous value, which may be outside the bounds of the new DataGrid.
For a change of the criteria for the given results the page number should also be reset, back to the first page, allowing the site visitor to navigate their way once again through the page results.
Given below is the code for the search field, image button, and the DataGrid.
<div> <asp:TextBox id="tbxSearch" Runat="server" /> <asp:ImageButton id="btnSearch" Runat="server" ImageUrl="~/images/icon_search_16px.gif"></asp:ImageButton> </div> <div> <asp:DataGrid ID="gdvApplications" runat="server" AllowPaging="false" AllowSorting="true" AutoGenerateColumns="false" GridLines="none" Width="100%" border="0" > <Columns> <asp:TemplateField SortExpression="ApplName" HeaderText="Applicant"> <ItemTemplate> <asp:Label runat="server" Visible="true" Text='<%# DataBinder.Eval(Container, "DataItem.ApplFirstName").ToString & " " & DataBinder.Eval(Container, "DataItem.ApplLastName").ToString %>' ID="ApplName" /> <asp:Label runat="server" Visible="false" Text='<%# DataBinder.Eval(Container, "DataItem.ApplicationId").ToString %>' ID="ApplicationId" Name="ApplicationId" /> </ItemTemplate> </asp:TemplateField> <asp:TemplateField ItemStyle-Width="100" SortExpression="appldate" HeaderText="Date"> <ItemTemplate> <asp:Label runat="server" Visible="true" Text='<%# DataBinder.Eval(Container, "DataItem.ApplDate").ToShortDateString %>' ID="ApplDate" NAME="ApplDate" /> </ItemTemplate> </asp:TemplateField> </Columns> </asp:DataGrid> </div> The subroutine btnSearch_Click which handles the search submit, is shown in the code below.. In this implementation the CurrentPageIndex is set to 0. Private Sub btnSearch_Click(ByVal sender As System.Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles btnSearch.Click grdApplications.CurrentPageIndex = 0 BindData(tbxSearch.Text) End Sub
If you are implementing your own paging control to be used alongside the DataGrid ensure that the first page is set to 0, not 1, and also that the last page will be the 1 less than the number.


