asp.net网格视图提供随机页码

时间:2014-11-23 12:05:42

标签: c# asp.net .net gridview aspxgridview

我有一个网格视图,每个页面应该有5个元素,当有超过5个时,应该有分页。

当前案例

当我有四个元素时,分页不会出现--->好

当我有五个元素时,分页包含(1,2)页面出现--->错

当我点击这些页面中的任何一个页码时,页码开始乱码。例如,如果我有两个页码(1,2)并且我点击了第2页,那么我将有(1,2,3)页码,依此类推...... --->错

代码

<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>

我已经处理了页面索引更改:

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

1 个答案:

答案 0 :(得分:1)

填充DataTable,它是页面中的静态字段。在这种情况下,每次向页面发送回发时都不会创建此表。因此,从数据库中提取的记录将被添加到DataTable中的现有记录中。从此字段中删除静态语句:

private DataTable dataTable = getDataTableStructure();