分页技术之GridView控件

  GridView控件实现分页技术
  第一步:设置GridView控件的属性,跟分页相关的属性设置如下:
  AllowPaging="true":允许分页, PageSize="大小":每一页显示的信息条数,
  OnPageIndexChanging="GridView1_PageIndexChanging":页数改变时触发的事件。
  第二步:设置GridView控件的PagerTemplate页模板属性。PageTemplate是用来获取或设置 GridView 控件中页导航行的自定义内容。代码实现如下:
  1 <PagerTemplate>
  2
  3 <table width="100%">
  4
  5 <tr>
  6
  7 <td style="text-align: center">
  8
  9
  10
  11 第<asp:Label ID="lblPageIndex" runat="server"
  12
  13 Text="<%#((GridView)Container.Parent.Parent)。PageIndex + 1 %>" ></asp:Label>页
  14
  15
  16
  17 共<asp:Label ID="lblPageCount" runat="server" Text="<%# ((GridView)Container.Parent.Parent)。PageCount %>"></asp:Label>页
  18
  19
  20
  21 <asp:LinkButton ID="btnFirst" runat="server" CausesValidation="False" CommandArgument="First"
  22 CommandName="Page" Text="首页"></asp:LinkButton>
  23
  24
  25
  26 <asp:LinkButton ID="btnPrev" runat="server"
  27 CausesValidation="False" CommandArgument="Prev"
  28 CommandName="Page" Text="上一页"></asp:LinkButton>
  29
  30
  31
  32 <asp:LinkButton ID="btnNext" runat="server"
  33 CausesValidation="False" CommandArgument="Next"
  34 CommandName="Page" Text="下一页"></asp:LinkButton>
  35
  36
  37
  38 <asp:LinkButton ID="btnLast" runat="server"
  39 CausesValidation="False" CommandArgument="Last"
  40 CommandName="Page" Text="尾页"></asp:LinkButton>
  41
  42
  43
  44 <asp:TextBox ID="txtNewPageIndex" runat="server"
  45 Text="<%# ((GridView)Container.Parent.Parent)。PageIndex + 1%>" Width="20px"></asp:TextBox>
  46
  47
  48
  49 <asp:LinkButton ID="btnGo" runat="server" CausesValidation="False" CommandArgument="-1" CommandName="Page" Text="GO"></asp:LinkButton>
  50
  51
  52
  53 </td>
  54
  55 </tr>
  56
  57 </table>
  58
  59 </PagerTemplate>
  View Code
  ((GridView)Container.Parent.Parent)。PageIndex + 1获取当前控件显示的页面数;((GridView)Container.Parent.Parent)。PageCount获取当前控件总的页面数;CausesValidation设置按钮提交时不执行验证;CommandArgument设置与关联的 CommandName 属性一起传递到 Command 事件处理程序的可选参数;CommandName设置命令名,该命令名与传递给 Command 事件的 Button 控件相关联 www.yztrans.com
  第三步:后台GridView1_PageIndexChanging事件的实现,代码如下:
  protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
  {
  //通过类型转换得到当前的gridview控件
  GridView gvw = (GridView)sender;
  if (e.NewPageIndex < 0)
  {
  TextBox PageNum = (TextBox)GridView1.BottomPagerRow.FindControl("txtNewPageIndex");
  int Pa = int.Parse(PageNum.Text);
  if (Pa <= 0)//如果前往分页的index小于或等于0则转向0
  {
  gvw.PageIndex = 0;
  }
  else
  {
  gvw.PageIndex = Pa - 1;
  }
  }
  else
  {
  gvw.PageIndex = e.NewPageIndex;
  }
  bind();//自定义的GridView控件绑定数据函数
  } www.tygj123.com
 

转载于:https://www.cnblogs.com/tianchaoy/p/3659291.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值