GridView自定义分页

本文详细介绍了如何在GridView控件中实现分页与导航功能,包括设置分页参数、创建分页模板以及处理分页事件。通过实例代码演示了如何获取当前页数、总页数、跳转到指定页等功能。

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

第一步:设置GridView的AllowPaging="True" 和 PageSize="20"
第二步:创建GridView的分页模板
  1. <PagerTemplate>
  2.                 第<asp:Label ID="LabelCurrentPage" runat="server" Text="<%# ((GridView)Container.NamingContainer).PageIndex + 1 %>"></asp:Label>
  3.                 页/共<asp:Label ID="LabelPageCount" runat="server" Text="<%# ((GridView)Container.NamingContainer).PageCount %>"></asp:Label>页
  4.                 <asp:LinkButton ID="LinkButtonFirstPage" runat="server" CommandArgument="First" CommandName="Page"
  5.                     Visible='<%#((GridView)Container.NamingContainer).PageIndex != 0 %>'>首页</asp:LinkButton>
  6.                 <asp:LinkButton ID="LinkButtonPreviousPage" runat="server" CommandArgument="Prev"
  7.                     CommandName="Page" Visible='<%# ((GridView)Container.NamingContainer).PageIndex != 0 %>'>上一页</asp:LinkButton>
  8.                 <asp:LinkButton ID="LinkButtonNextPage" runat="server" CommandArgument="Next" CommandName="Page"
  9.                     Visible='<%# ((GridView)Container.NamingContainer).PageIndex != ((GridView)Container.NamingContainer).PageCount - 1 %>'>下一页</asp:LinkButton>
  10.                 <asp:LinkButton ID="LinkButtonLastPage" runat="server" CommandArgument="Last" CommandName="Page"
  11.                     Visible='<%# ((GridView)Container.NamingContainer).PageIndex != ((GridView)Container.NamingContainer).PageCount - 1 %>'>尾页</asp:LinkButton>
  12.                 转到第
  13.                 <asp:TextBox ID="txtNewPageIndex" runat="server" Width="25px" BorderColor="gray"  BorderWidth="1px" Height="15" Text='<%# ((GridView)Container.Parent.Parent).PageIndex + 1 %>' />页
  14.                 <asp:LinkButton ID="btnGo" runat="server" CausesValidation="False" CommandArgument="-2"
  15.                     CommandName="Page" Text="GO" />
  16.             </PagerTemplate>
第三步:在PageIndexChanging事件中添加如下代码
  1. protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
  2.     {
  3.         // 得到该控件
  4.         GridView theGrid = sender as GridView;
  5.         int newPageIndex = 0;
  6.         if (e.NewPageIndex == -3)
  7.         {
  8.             //点击了Go按钮
  9.             TextBox txtNewPageIndex = null;
  10.             //GridView较DataGrid提供了更多的API,获取分页块可以使用BottomPagerRow 或者TopPagerRow,当然还增加了HeaderRow和FooterRow
  11.             GridViewRow pagerRow = theGrid.BottomPagerRow;
  12.             if (pagerRow != null)
  13.             {
  14.                 //得到text控件
  15.                 txtNewPageIndex = pagerRow.FindControl("txtNewPageIndex") as TextBox;
  16.             }
  17.             if (txtNewPageIndex != null)
  18.             {
  19.                 //得到索引
  20.                 newPageIndex = int.Parse(txtNewPageIndex.Text) - 1;
  21.             }
  22.         }
  23.         else
  24.         {
  25.             //点击了其他的按钮
  26.             newPageIndex = e.NewPageIndex;
  27.         }
  28.         //防止新索引溢出
  29.         newPageIndex = newPageIndex < 0 ? 0 : newPageIndex;
  30.         newPageIndex = newPageIndex >= theGrid.PageCount ? theGrid.PageCount - 1 : newPageIndex;
  31.         //得到新的值
  32.         theGrid.PageIndex = newPageIndex;
  33.         //重新绑定数据
  34.         this.prBindToDataGird();
  35.               
  36.   }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值