当前位置导航:炫浪网>>网络学院>>网页制作>>ASP.NET教程

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 = "无相关记录!";

  }

  }

相关内容
赞助商链接