深入DataGrid分頁樣式執行個體

來源:互聯網
上載者:User

      DataGrid提供了分頁功能,不過看上去功能有限,但是我們可以通過DataGrid的一些屬性來擷取狀態以及增加首頁、尾頁功能按鈕。這裡沒有使用DataGrid的自訂分頁功能,如果在速度效率不是很講究的情況下,由DataGrid自己管理分頁還是不錯的,付出的代價就是要把整個相關資料取出來後再刪選指定頁的資料。好處就是開發速度快,不需要寫分頁的預存程序。本文案例使用的是Sql Server中的Northwind資料庫。運行介面如下:


對於前台的顯示介面,我放了一個DataGrid;四個LinkButton導向按鈕;四個Literal來顯示紀錄狀態。

剩下的就是用表格定位。

這裡需要設定DataGrid的AllowPaging屬性為True,同時設定AllowCustomPaging屬性位false(預設為false),設定PagerStyle的Visible屬性為False,使前台不顯示。

HTML:

-------------------------------------------------------------------------------------------------------------------------

<%@ Page language="c#" Codebehind="DataGridPaging.aspx.cs" AutoEventWireup="false" Inherits="ZZ.AspnetPaging.DataGridPaging" %>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >

<HTML>

 <HEAD>

  <title>DataGridPaging</title>

  <meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">

  <meta content="C#" name="CODE_LANGUAGE">

  <meta content="JavaScript" name="vs_defaultClientScript">

  <meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema">

 </HEAD>

 <body>

  <form id="Form1" method="post" runat="server">

   <TABLE id="Table1" style="FONT-SIZE: 9pt" cellSpacing="1" cellPadding="1" width="450" align="center"

    border="1">

    <TR>

     <TD>

      <asp:datagrid id="DataGrid1" runat="server" AllowPaging="True" Width="100%" PageSize="5">

       <HeaderStyle Font-Size="9pt"></HeaderStyle>

       <FooterStyle Font-Size="9pt"></FooterStyle>

       <PagerStyle Visible="False" Font-Size="9pt" Mode="NumericPages"></PagerStyle>

      </asp:datagrid></TD>

    </TR>

   </TABLE>

   <TABLE id="Table2" style="FONT-SIZE: 9pt" cellSpacing="1" cellPadding="1" width="450" align="center"

    border="1">

    <TR>

     <TD style="WIDTH: 207px">

      <asp:linkbutton id="LBtnFirst" runat="server" CommandName="First">首頁</asp:linkbutton>

      <asp:linkbutton id="LBtnPrev" runat="server" CommandName="Prev">上一頁</asp:linkbutton>

      <asp:linkbutton id="LBtnNext" runat="server" CommandName="Next">下一頁</asp:linkbutton>

      <asp:linkbutton id="LBtnLast" runat="server" CommandName="Last">尾頁</asp:linkbutton>

     </TD>

     <TD>第

      <asp:literal id="LtlPageIndex" runat="server"></asp:literal>頁 共

      <asp:literal id="LtlPageCount" runat="server"></asp:literal>頁 每頁

      <asp:literal id="LtlPageSize" runat="server"></asp:literal>條 共

      <asp:literal id="LtlRecordCount" runat="server"></asp:literal>條

     </TD>

    </TR>

   </TABLE>

  </form>

 </body>

</HTML>

.cs代碼:

----------------------------------------------------------------------------------------------------------------------------------------

using System;

using System.Collections;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Web;

using System.Web.SessionState;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Web.UI.HtmlControls;

using System.Data.SqlClient;

using System.Configuration;

 

namespace ZZ.AspnetPaging

{

 public class DataGridPaging : System.Web.UI.Page

 {

  private static string connString = ConfigurationSettings.AppSettings["ConnString"];

  private int recordCount;

  private int pageCount;

 

  protected System.Web.UI.WebControls.LinkButton LBtnFirst;

  protected System.Web.UI.WebControls.LinkButton LBtnPrev;

  protected System.Web.UI.WebControls.LinkButton LBtnNext;

  protected System.Web.UI.WebControls.LinkButton LBtnLast;

  protected System.Web.UI.WebControls.Literal LtlPageIndex;

  protected System.Web.UI.WebControls.Literal LtlPageCount;

  protected System.Web.UI.WebControls.Literal LtlPageSize;

  protected System.Web.UI.WebControls.Literal LtlRecordCount;

  protected System.Web.UI.WebControls.DataGrid DataGrid1;

    

  private void Page_Load(object sender, System.EventArgs e)

  {

   if(!Page.IsPostBack)

   {

    DataGridDataBind();

   }

  }

 

  //綁定資料

  private void DataGridDataBind()

  {

   DataSet ds = GetCustomersData();

   recordCount = ds.Tables[0].Rows.Count;

   //擷取當前的頁數

   pageCount = (int)Math.Ceiling( recordCount * 1.0 / PageSize);

   //避免紀錄從有到無時,並且已經進行過反頁的情況下CurrentPageIndex > PageCount出錯

   if(recordCount ==0)

   {

    this.DataGrid1.CurrentPageIndex = 0;

   }

   else if(this.DataGrid1.CurrentPageIndex >= pageCount)

   {

    this.DataGrid1.CurrentPageIndex = pageCount - 1;

   }

   this.DataGrid1.DataSource = ds;

   this.DataGrid1.DataBind();

   NavigationStateChange();

  }

 

  #region Web Form設計器產生的程式碼

  override protected void OnInit(EventArgs e)

  {

   //

   // CODEGEN: 該調用是 ASP.NET Web Form設計器所必需的。

   //

   InitializeComponent();

   base.OnInit(e);

  }

        

  /// <summary>

  /// 設計器支援所需的方法 - 不要使用代碼編輯器修改

  /// 此方法的內容。

  /// </summary>

  private void InitializeComponent()

  {   

   this.LBtnFirst.Click += new System.EventHandler(this.LBtnNavigation_Click);

   this.LBtnPrev.Click += new System.EventHandler(this.LBtnNavigation_Click);

   this.LBtnNext.Click += new System.EventHandler(this.LBtnNavigation_Click);

   this.LBtnLast.Click += new System.EventHandler(this.LBtnNavigation_Click);

   this.Load += new System.EventHandler(this.Page_Load);

 

  }

  #endregion

 

  private void LBtnNavigation_Click(object sender, System.EventArgs e)

  {

   LinkButton btn = (LinkButton)sender;

   switch(btn.CommandName)

   {

    case "First":

     PageIndex = 0;

     break;

    case "Prev"://if( PageIndex > 0 )

     PageIndex = PageIndex - 1;

     break;

    case "Next"://if( PageIndex < PageCount -1)

     PageIndex = PageIndex + 1;

     break;

    case "Last":

     PageIndex = PageCount - 1;

     break;

   }

   DataGridDataBind();             

  }

  //資料繫結

  public static DataSet GetCustomersData()

  {

   SqlConnection conn = new SqlConnection("server=.;database=northwind;uid=sa");

   string sqlStr = "SELECT CustomerID, CompanyName,Address,Phone FROM Customers";

   SqlCommand comm = new SqlCommand( sqlStr ,conn);

   SqlDataAdapter dataAdapter = new SqlDataAdapter(comm);

   DataSet ds = new DataSet();

   dataAdapter.Fill(ds);

   return ds;

  }

 

  /// <summary>

  /// 控制導覽按鈕或數位狀態

  /// </summary>

  public void NavigationStateChange()

  {

   if( PageCount <= 1 )//( RecordCount <= PageSize )//小於等於一頁

   {

    this.LBtnFirst.Enabled = false;

    this.LBtnPrev.Enabled = false;

    this.LBtnNext.Enabled = false;

    this.LBtnLast.Enabled = false;

   }

   else //有多頁

   {

    if( PageIndex == 0 )//當前為第一頁

    {

     this.LBtnFirst.Enabled = false;

     this.LBtnPrev.Enabled = false;

     this.LBtnNext.Enabled = true;

     this.LBtnLast.Enabled = true;

                                    

    }

    else if( PageIndex == PageCount - 1 )//當前為最後頁

    {

     this.LBtnFirst.Enabled = true;

     this.LBtnPrev.Enabled = true;

     this.LBtnNext.Enabled = false;

     this.LBtnLast.Enabled = false;

                                    

    }

    else //中間頁

    {

     this.LBtnFirst.Enabled = true;

     this.LBtnPrev.Enabled = true;

     this.LBtnNext.Enabled = true;

     this.LBtnLast.Enabled = true;

    }

                  

   }

   if(RecordCount == 0)//當沒有紀錄時DataGrid.PageCount會顯示1頁

    this.LtlPageCount.Text = "0";

   else

    this.LtlPageCount.Text = PageCount.ToString();

   if(RecordCount == 0)

    this.LtlPageIndex.Text = "0";

   else

    this.LtlPageIndex.Text = (PageIndex + 1).ToString();//在有頁數的情況下前台顯示頁數加1

   this.LtlPageSize.Text = PageSize.ToString();

   this.LtlRecordCount.Text = RecordCount.ToString();

  }

 

        

  // 總頁數

  public int PageCount

  {

   get{return this.DataGrid1.PageCount;}

  }

  //頁大小

  public int PageSize

  {

   get{return this.DataGrid1.PageSize;}

  }

  //頁索引,從零開始

  public int PageIndex

  {

   get{return this.DataGrid1.CurrentPageIndex;}

   set{this.DataGrid1.CurrentPageIndex = value;}

  }

  // 紀錄總數

  public int RecordCount

  {

   get{return recordCount;}

   set{recordCount = value;}

  }

 }

}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.