控件中国网现已改版,您看到的是老版本网站的镜像,系统正在为您跳转到新网站首页,请稍候.......
中国最专业的商业控件资讯网产品咨询电话:023-67870900 023-67871946
产品咨询EMAIL:SALES@COMPONENTCN.COM

ASP.NET GridView自定义分页的方法

作者:佚名 出处:互联网 2010年03月29日 阅读:

本文介绍ASP.NET GridView自定义分页的方法。
 
<asp:GridView ID="GridView2" runat="server" AllowPaging="True"> 
  <PagerTemplate>
  <div id="main">
  <table>
  <tr>
  <td style="text-align:left;">
  <div id="info">&nbsp;&nbsp;页次:<asp:Label ID="lblPageCurrent" runat="server" Text="1" CssClass="txtInfo"></asp:Label>
  /<asp:Label ID="lblPageCount" runat="server" Text="1"></asp:Label>&nbsp;&nbsp;
  共&nbsp;<asp:Label ID="lblPageRow" runat="server" Text="1" CssClass="txtInfo"></asp:Label>&nbsp;条记录
  </div>
  </td>
  <td style="text-align:right;">
  <div id="page">
  <asp:LinkButton ID="btnFirst" runat="server" CssClass="link" CommandName="Pager" CommandArgument="First" OnCommand="NavigateToPage">[首页]</asp:LinkButton>&nbsp;
  <asp:LinkButton ID="btnPrev" runat="server" CssClass="link" CommandName="Pager" CommandArgument="Prev" OnCommand ="NavigateToPage">[前一页]</asp:LinkButton>&nbsp;
  <asp:LinkButton ID="btnNext" runat="server" CssClass="link" CommandName="Pager" CommandArgument="Next" OnCommand="NavigateToPage">[下一页]</asp:LinkButton>&nbsp;
  <asp:LinkButton ID="btnLast" runat="server" CssClass="link" CommandName="Pager" CommandArgument="Last" OnCommand="NavigateToPage">[尾页]</asp:LinkButton>&nbsp;&nbsp;
  </div>
  </td>
  </tr>
  </table>
  </div>
  </PagerTemplate>
  <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
  <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
  <SelectedRowStyle BackColor="#D1DDF1" ForeColor="#333333" Font-Bold="True" />
  <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
  <AlternatingRowStyle BackColor="White" />
  <RowStyle BackColor="#EFF3FB" />
  <EditRowStyle BackColor="#2461BF" />
  </asp:GridView>
  <asp:Label ID="labmessage" runat="server"></asp:Label>
  后台代码为:
  protected void GridViewBinding()
  {
  rowcount = ds.Tables[0].Rows.Count;
  gridview1.DataSource = ds;
  gridview1.DataBind();
  }
  protected void NavigateToPage(object sender, CommandEventArgs e)
  {
  switch (e.CommandArgument.ToString())
  {
  case "First":
  this.gridview1.PageIndex = 0;
  break;
  case "Prev":
  if (this.gridview1.PageIndex > 0)
  this.gridview1.PageIndex -= 1;
  break;
  case "Next":
  if (this.gridview1.PageIndex < (this.gridview1.PageCount - 1))
  this.gridview1.PageIndex += 1;
  break;
  case "Last":
  this.gridview1.PageIndex = this.gridview1.PageCount - 1;
  break;
  }
  GridViewBinding();
  }
  protected void gridview1_DataBound(object sender, EventArgs e)
  {
  if (ds.Tables[0].Rows.Count!=0)
  {
  GridViewRow pagerRow = gridview1.BottomPagerRow;
  gridview1.BottomPagerRow.Visible = true;
  //获取Label实例,显示页次信息
  Label lblCurrent = (Label) pagerRow.Cells[0].FindControl("lblPageCurrent");
  Label lblCount = (Label) pagerRow.Cells[0].FindControl("lblPageCount");
  Label lblRow = (Label) pagerRow.Cells[0].FindControl("lblPageRow");
  //获取按钮实例,为了控制其是否可用
  LinkButton btnFirstTem = (LinkButton) pagerRow.Cells[0].FindControl("btnFirst");
  LinkButton btnPrevTem = (LinkButton) pagerRow.Cells[0].FindControl("btnPrev");
  LinkButton btnNextTem = (LinkButton) pagerRow.Cells[0].FindControl("btnNext");
  LinkButton btnLastTem = (LinkButton) pagerRow.Cells[0].FindControl("btnLast");
  if (lblCurrent != null)
  lblCurrent.Text = (this.gridview1.PageIndex + 1).ToString();
  if (lblCount != null)
  lblCount.Text = this.gridview1.PageCount.ToString();
  if (lblRow != null)
  lblRow.Text = rowcount.ToString();
  if (this.gridview1.PageIndex == 0)
  {
  btnFirstTem.Enabled = false;
  btnPrevTem.Enabled = false;
  //只有一页,所有分页按钮不可用
  if (this.gridview1.PageCount == 1)
  {
  btnNextTem.Enabled = false;
  btnLastTem.Enabled = false;
  }
  }
  else if (this.gridview1.PageIndex == (this.gridview1.PageCount - 1))
  {
  btnNextTem.Enabled = false;
  btnLastTem.Enabled = false;
  }
  }
  else
  {
  labmessage.Text = "无相关记录!";
  }
  }
 

热推产品

  • ActiveReport... 强大的.NET报表设计、浏览、打印、转换控件,可以同时用于WindowsForms谀坔攀戀Forms平台下......
  • AnyChart AnyChart使你可以创建出绚丽的交互式的Flash和HTML5的图表和仪表控件。可以用于仪表盘的创......
首页 | 新闻中心 | 产品中心 | 技术文档 | 友情连接 | 关于磐岩 | 技术支持中心 | 联系我们 | 帮助中心 Copyright-2006 ComponentCN.com all rights reserved.重庆磐岩科技有限公司(控件中国网) 版权所有 电话:023 - 67870900 传真:023 - 67870270 产品咨询:sales@componentcn.com 渝ICP备12000264号 法律顾问:元炳律师事务所 重庆市江北区塔坪36号维丰创意绿苑A座28-5 邮编:400020
在线客服
在线客服系统
在线客服
在线客服系统