Search code examples
c#asp.net.netgridviewaspxgridview

asp.net grid view gives random page numbers


I have a grid view, each page should have 5 elements, when there are more than 5, there should be paging.

The current case

when I have four elements, the paging doesn't appear ---> Good

when I have five elements, the paging contains (1,2) pages appears ---> wrong

when I click on any of these pages number, the pages numbers start to incearse. for example, if I have two pages numbers (1,2) and I clicked on page 2, then I will have (1,2,3) page numbers and so on.. ---> Wrong

The code

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 

                CellPadding="4" ForeColor="#333333" GridLines="None" AllowPaging="True" AllowSorting="True"
                OnPageIndexChanging="GridView1_PageIndexChanging" PageSize="5">
                <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
                <Columns>
                    <asp:BoundField DataField="customerID" HeaderText="Customer ID" ReadOnly="true">
                        <HeaderStyle HorizontalAlign="Left" Wrap="False" />
                        <ItemStyle HorizontalAlign="Left" VerticalAlign="Middle" />
                    </asp:BoundField>
                    <asp:BoundField DataField="firstName" HeaderText="Forename" ReadOnly="true">
                        <HeaderStyle HorizontalAlign="Left" Wrap="False" />
                        <ItemStyle HorizontalAlign="Left " VerticalAlign="Middle" />
                    </asp:BoundField>
                    <asp:BoundField DataField="lastName" HeaderText="Surname" ReadOnly="true">
                        <HeaderStyle HorizontalAlign="Left" Wrap="False" />
                        <ItemStyle HorizontalAlign="Left " VerticalAlign="Middle" />
                    </asp:BoundField>
                    <asp:BoundField DataField="mobileNumber" HeaderText="Phone" ReadOnly="true">
                        <HeaderStyle HorizontalAlign="Left" Wrap="False" />
                        <ItemStyle HorizontalAlign="Left" VerticalAlign="Middle" />
                    </asp:BoundField>
                    <asp:TemplateField HeaderText="Details" ItemStyle-HorizontalAlign="Right" HeaderStyle-HorizontalAlign="Center">
                        <ItemTemplate>
                            <a href="javascript:void(0);" onclick="LoadDetails('<%#Eval("customerID")%>','<%#Eval("mobileNumber")%>',this)">
                                View Details</a>
                        </ItemTemplate>
                        <HeaderStyle HorizontalAlign="Center"></HeaderStyle>
                        <ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" />
                    </asp:TemplateField>
                </Columns>
                <EditRowStyle BackColor="#999999" />
                <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
                <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
                <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Justify" CssClass="pageer"
                    VerticalAlign="Middle" />
                <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
                <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
                <SortedAscendingCellStyle BackColor="#E9E7E2" />
                <SortedAscendingHeaderStyle BackColor="#506C8C" />
                <SortedDescendingCellStyle BackColor="#FFFDF8" />
                <SortedDescendingHeaderStyle BackColor="#6F8DAE" />
            </asp:GridView>

and I already handeled the page index change:

protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            GridView1.PageIndex = e.NewPageIndex;
            GridView1.DataBind();
        }

Solution

  • You populate the DataTable that is a static field in the page. In this case, this table won't be created each time you send postbacks to the page. As a result, fetched from a database records will be added to existing records in the DataTable. Remove the static statement from this field:

    private DataTable dataTable = getDataTableStructure();