在GRIDVIEW中实现完美自定义分页

本文介绍了一个使用ASP.NET实现的GridView分页及数据绑定案例,详细展示了前台ASPX代码与后台CS代码的结合使用,实现了自定义分页导航,并通过ADO.NET连接数据库进行数据填充。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

前台ASPX代码
<asp:GridView ID="GridView1" runat="server" DataKeyNames="ID" Height="164px" Width="765px" AutoGenerateColumns="False" AllowPaging="True" CellPadding="4" ForeColor="#333333" GridLines="None" OnPageIndexChanging="GridView1_PageIndexChanging" PageSize="8" OnRowDataBound="GridView1_RowDataBound">
                        <Columns>
                            <asp:BoundField DataField="ID" HeaderText="序号" />
                            <asp:BoundField DataField="UserName" HeaderText="报修人" />
                            <asp:BoundField DataField="Phone" HeaderText="联系电话" />
                            <asp:BoundField DataField="Place" HeaderText="报修地点" />
                            <asp:TemplateField HeaderText="故障描述">
                                <EditItemTemplate>
                                    <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("ConkOut") %>'></asp:TextBox>
                                </EditItemTemplate>
                                <ItemTemplate>
                                    <asp:Label ID="Label1" runat="server" Text='<%# Bind("ConkOut") %>'></asp:Label>
                                    <asp:Label ID="Label6" runat="server" Text='<%# Eval("TimeBX") %>'></asp:Label>
                                </ItemTemplate>
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="维修回复">
                                <EditItemTemplate>
                                    <asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("Revert") %>'></asp:TextBox>
                                </EditItemTemplate>
                                <ItemTemplate>
                                    <asp:Label ID="Label2" runat="server" Text='<%# Bind("Revert") %>'></asp:Label>
                                    <asp:Label ID="Label7" runat="server" Text='<%# Eval("TimeHH") %>'></asp:Label>
                                </ItemTemplate>
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="维修状态">
                                <EditItemTemplate>
                                    <asp:TextBox ID="TextBox3" runat="server" Text='<%# Bind("State") %>'></asp:TextBox>
                                </EditItemTemplate>
                                <ItemTemplate>
                                    <asp:CheckBox ID="CheckBox1" runat="server" Checked='<%# Eval("State") %>' />
                                </ItemTemplate>
                            </asp:TemplateField>
                        </Columns>
                        <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
                        <RowStyle BackColor="#EFF3FB" />
                        <EditRowStyle BackColor="#2461BF" />
                        <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
                        <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
                        <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
                        <AlternatingRowStyle BackColor="White" />


///下面是关键代码



                        <PagerTemplate>
                            
                            <table style="width: 617px">
                                <tr>
                                    <td colspan="6" style="height: 23px">
                                        <asp:Label ID="LabelCurrentPage" runat="server" Height="16px" Width="89px">当前页:<%# ((GridView)Container.NamingContainer).PageIndex + 1 %></asp:Label>
                                        &nbsp;&nbsp;
                                        <asp:Label ID="LabelPageCount" runat="server">总页数:<%# ((GridView)Container.NamingContainer).PageCount %></asp:Label>
                                        &nbsp;
                                        <asp:LinkButton ID="LinkButtonFirstPage" runat="server" CommandArgument="First" CommandName="Page"
                                            Enable="<%# ((GridView)Container.NamingContainer).PageIndex != 0 %>" ForeColor="White" Height="15px" Width="39px" OnClick="LinkButtonFirstPage_Click">首页</asp:LinkButton>
                                        &nbsp;
                                        <asp:LinkButton ID="LinkButtonPreviousPage" runat="server" CommandArgument="Prev"
                                            CommandName="Page" Enable="<%# ((GridView)Container.NamingContainer).PageIndex != 0 %>" ForeColor="White" Width="44px" OnClick="LinkButtonPreviousPage_Click">上一页</asp:LinkButton>
                                        &nbsp; &nbsp;<asp:LinkButton ID="LinkButtonNextPage" runat="server" CommandArgument="Next" CommandName="Page"
                                            Enable="<%# ((GridView)Container.NamingContainer).PageIndex != ((GridView)Container.NamingContainer).PageCount - 1 %>" ForeColor="White" Height="16px" Width="51px" OnClick="LinkButtonNextPage_Click">下一页</asp:LinkButton>
                                        &nbsp; &nbsp;&nbsp;
                                        <asp:LinkButton ID="LinkButtonLastPage" runat="server" CommandArgument="Last" CommandName="Page"
                                            Enable="<%# ((GridView)Container.NamingContainer).PageIndex != ((GridView)Container.NamingContainer).PageCount - 1 %>" ForeColor="White" Width="46px" OnClick="LinkButtonLastPage_Click">尾页</asp:LinkButton>
                                        &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp;<asp:textbox id="txtNewPageIndex" runat="server" width="42px" text='<%# ((GridView)Container.Parent.Parent).PageIndex + 1 %>' />
<asp:linkbutton id="btnGo" runat="server" causesvalidation="False" commandargument="-2" commandname="Page" text="GO" ForeColor="White" Width="34px" OnClick="btnGo_Click" /></td>
                                </tr>
                            </table>
                            &nbsp;&nbsp;

                        </PagerTemplate>
                    </asp:GridView>

后台CS代码
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView theGrid = sender as GridView; //定义一个GridView theGrid
        int newPageIndex = 0;
        if (-3 == e.NewPageIndex)
        { // when click the "GO" Button
            TextBox txtNewPageIndex = null;
            //GridViewRow pagerRow = theGrid.Controls[0].Controls[theGrid.Controls[0].Controls.Count - 1] as GridViewRow; // refer to PagerTemplate
            GridViewRow pagerRow = theGrid.BottomPagerRow; //GridView较DataGrid提供了更多的API,获取分页块可以使用BottomPagerRow 或者TopPagerRow,当然还增加了HeaderRow和FooterRow
         
            if (null != pagerRow)
            {
                txtNewPageIndex = pagerRow.FindControl("txtNewPageIndex") as TextBox;   // refer to the TextBox with the NewPageIndex value
            }
            if (null != txtNewPageIndex)
            {
                newPageIndex = int.Parse(txtNewPageIndex.Text) - 1; // get the NewPageIndex
            }
        }
        else
        { // when click the first, last, previous and next Button
            newPageIndex = e.NewPageIndex;
        }
        // check to prevent form the NewPageIndex out of the range
        newPageIndex = newPageIndex < 0 ? 0 : newPageIndex;
        newPageIndex = newPageIndex >= theGrid.PageCount ? theGrid.PageCount - 1 : newPageIndex;
        // specify the NewPageIndex
        theGrid.PageIndex = newPageIndex;
        // rebind the control
        // in this case of retrieving the data using the xxxDataSoucr control,
        // just do nothing, because the asp.net engine binds the data automatically

        BindData();
    }


private void BindData()
    {


        OleDbConnection conn = new OleDbConnection(ConfigurationManager.ConnectionStrings["baoxiuConn"].ConnectionString);
        try
        {
            conn.Open();
            OleDbDataAdapter myadapter = new OleDbDataAdapter("select * from baoxiu order by ID desc", conn);
            myadapter.Fill(mydataset, "baoxiu");
            GridView1.DataSource = mydataset;
           
            GridView1.DataBind();
               }
        }
        catch (Exception ex)
        {
            Response.Redirect("error.aspx?myerror=" + ex.Message.Replace("/n", ""));

        }
        finally
        {
            conn.Close();
        }
    }

 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值