GridView实现分页

本文介绍了如何在ASP.NET中使用GridView控件实现分页功能。通过在前台创建PagerTemplate,结合后台事件处理,实现了包括首页、上一页、下一页、尾页的分页跳转,以及直接输入页码跳转的功能。

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

1、前台在GridView里写实现分页 

<PagerTemplate>
      <table width="100%">
          <tr>
              <td style="text-align: right">第

                 <asp:Label ID="Lab_CurrentPage" runat="server"

                          Text="<%#((GridView)Container.NamingContainer).PageIndex + 1 %>">

                 </asp:Label>页/共

                 <asp:Label ID="Lab_PageCount" runat="server"

                          Text="<%# ((GridView)Container.NamingContainer).PageCount %>">

                 </asp:Label>页
                  <asp:LinkButton ID="LBtn_FirstPage" runat="server" CommandArgument="First"

                           CommandName="Page"  Visible="<%# ((GridView)Container.NamingContainer).PageIndex 

                           != 0 %>">首页
                  </asp:LinkButton>
                  <asp:LinkButton ID="LBtn_PreviousPage" runat="server" CommandArgument="Prev"  

                           CommandName="Page" Visible="<%# ((GridView)Container.NamingContainer).PageIndex 

                           != 0 %>">上一页

                 </asp:LinkButton>
                  <asp:LinkButton ID="LBtn_NextPage" runat="server" CommandArgument="Next" CommandName="Page"
                            Visible="<%# ((GridView)Container.NamingContainer).PageIndex !=

                            ((GridView)Container.NamingContainer).PageCount - 1 %>">下一页

                  </asp:LinkButton>
                  <asp:LinkButton ID="LBtn_LastPage" runat="server" CommandArgument="Last" CommandName="Page"
                           Visible="<%# ((GridView)Container.NamingContainer).PageIndex !=

                           ((GridView)Container.NamingContainer).PageCount - 1 %>">尾页

                  </asp:LinkButton>
                  <asp:TextBox ID="txtNewPageIndex" onkeypress="if (event.keyCode < 48 || event.keyCode >57) 

                            event.returnValue = false;"runat="server" Text='<%# 

                            ((GridView)Container.Parent.Parent).PageIndex + 1  %>'Width="20px">

                 </asp:TextBox>
                 <asp:Button ID="btn1Go" runat="server" CausesValidation="False" OnClick="btn1Go_Click"
                            CommandName="Page" Text="跳转">

                 </asp:Button>
               </td>
             </tr>
         </table>
</PagerTemplate>          

 

2、后台

//绑定GridView显示内容

private void GridViewBind1()
    {
        string strTemp = "SELECT * FROM Scms_bigclass";

        DataSet ds1 = Scms.SystemBase.SQLHelper.DataAdapter(strTemp, Scms.SystemBase.SQLHelper.SDACmd.select, "sqlTemp");
        GridView1.DataSource = ds1;
        GridView1.DataBind();
    }     

//点击下一页、上一页

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

//点击页面跳转到第几页

 protected void btn1Go_Click(object sender, EventArgs e)
    {
        GridViewRow pagerRow = GridView1.BottomPagerRow;
        TextBox txtPage = (TextBox)pagerRow.Cells[0].FindControl("txtNewPageIndex");
        if (txtPage.Text == "")
        {
            txtPage.Text = "1";
        }

        int numPage = 0;

        try
        {
            numPage = Convert.ToInt32(txtPage.Text);
        }
        catch
        {
            numPage = 1;
            lblPage_01.Text = "输入错误,请输入数字!";
        }

        if (numPage < 1)
        {
            numPage = 1;
            lblPage_01.Text = "输入的页数过小当前显示第一页!";
        }

        if (numPage > GridView1.PageCount)
        {
            numPage = GridView1.PageCount;
            lblPage_01.Text = "输入的页数过大当前显示最后一页!";
        }
        GridView1.PageIndex = numPage - 1;
        GridViewBind1();
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值