一個ASP.Net的DataGrid分頁控制項

來源:互聯網
上載者:User
寫的不算好,僅供參考。
  
  GridPager.ascx:
  <%@ Control Language="c#" AutoEventWireup="false" Codebehind="GridPager.ascx.cs" Inherits="Test.BaseClass.GridPager" TargetSchema="http://schemas.microsoft.com/intellisense/ie5"%><?xml:namespace prefix = asp /><asp:linkbutton id=lbtnPre style="BORDER-RIGHT: white 2px solid; BORDER-LEFT: white 1px solid" runat="server" Text="..." Visible="False"></asp:linkbutton><asp:linkbutton id=lbtnPage1 style="BORDER-RIGHT: white 2px solid; BORDER-LEFT: white 1px solid" runat="server" Text="1" Visible="False" Enabled="False" Font-Size="Small">1</asp:linkbutton> <asp:linkbutton id=lbtnPage2 style="BORDER-RIGHT: white 2px solid; BORDER-LEFT: white 1px solid" runat="server" Text="2" Visible="False"></asp:linkbutton><asp:linkbutton id=lbtnPage3 style="BORDER-RIGHT: white 2px solid; BORDER-LEFT: white 1px solid" runat="server" Text="3" Visible="False"></asp:linkbutton><asp:linkbutton id=lbtnPage4 style="BORDER-RIGHT: white 2px solid; BORDER-LEFT: white 1px solid" runat="server" Text="4" Visible="False"></asp:linkbutton><asp:linkbutton id=lbtnPage5 style="BORDER-RIGHT: white 2px solid; BORDER-LEFT: white 1px solid" runat="server" Text="5" Visible="False"></asp:linkbutton><asp:linkbutton id=lbtnPage6 style="BORDER-RIGHT: white 2px solid; BORDER-LEFT: white 1px solid" runat="server" Text="6" Visible="False"></asp:linkbutton><asp:linkbutton id=lbtnPage7 style="BORDER-RIGHT: white 2px solid; BORDER-LEFT: white 1px solid" runat="server" Text="7" Visible="False"></asp:linkbutton><asp:linkbutton id=lbtnPage8 style="BORDER-RIGHT: white 2px solid; BORDER-LEFT: white 1px solid" runat="server" Text="8" Visible="False"></asp:linkbutton><asp:linkbutton id=lbtnPage9 style="BORDER-RIGHT: white 2px solid; BORDER-LEFT: white 1px solid" runat="server" Text="9" Visible="False"></asp:linkbutton><asp:linkbutton id=lbtnPage10 style="BORDER-RIGHT: white 2px solid; BORDER-LEFT: white 1px solid" runat="server" Text="10" Visible="False"></asp:linkbutton><asp:linkbutton id=lbtnNext style="BORDER-RIGHT: white 2px solid; BORDER-LEFT: white 1px solid" runat="server" Text="..." Visible="False"></asp:linkbutton>
  ///////////////////////////////////////////////////////////////////////////////////////
  GridPager.ascx.cs:
  using System;
  using System.Data;
  using System.Drawing;
  using System.Web;
  using System.Web.UI.WebControls;
  namespace Test.BaseClass
  {
  public class GridPager : System.Web.UI.UserControl
  {
  #region 類變數
  protected System.Web.UI.WebControls.LinkButton lbtnNext;
  protected System.Web.UI.WebControls.LinkButton lbtnPage10;
  protected System.Web.UI.WebControls.LinkButton lbtnPage9;
  protected System.Web.UI.WebControls.LinkButton lbtnPage8;
  protected System.Web.UI.WebControls.LinkButton lbtnPage7;
  protected System.Web.UI.WebControls.LinkButton lbtnPage6;
  protected System.Web.UI.WebControls.LinkButton lbtnPage5;
  protected System.Web.UI.WebControls.LinkButton lbtnPage4;
  protected System.Web.UI.WebControls.LinkButton lbtnPage3;
  protected System.Web.UI.WebControls.LinkButton lbtnPage2;
  protected System.Web.UI.WebControls.LinkButton lbtnPage1;
  protected System.Web.UI.WebControls.LinkButton lbtnPre;
  #endregion
  
  #region
  /// <summary>
  /// 編號:01
  /// 內容摘要:翻頁的對象:DataGrid控制項。
  /// </summary>
  public DataGrid TheGrid;
  
  /// <summary>
  /// 編號:02
  /// 內容摘要:顯示查詢結果記錄數的標籤,可以不設定,表示無需顯示。
  /// </summary>
  public Label LabelRowsCount;
  
  /// <summary>
  /// 編號:03
  /// 內容摘要:顯示查詢結果總頁數的標籤,可以不設定,表示無需顯示。
  /// </summary>
  public Label LabelPageCount;
  
  /// <summary>
  /// 編號:04
  /// 內容摘要:顯示當前頁碼的標籤,可以不設定,表示無需顯示。
  /// </summary>
  public Label LabelPageIndex;
  
  /// <summary>
  /// 編號:05
  /// 內容摘要:查詢資料的SQL語句。
  /// </summary>
  private string SelectSQL
  {
  get
  {
  if(ViewState[this.ClientID+"_SelectSQL"] != null)
  return ViewState[this.ClientID+"_SelectSQL"].ToString();
  else
  return string.Empty;
  }
  set
  {
  ViewState[this.ClientID+"_SelectSQL"] = value;
  }
  }//SelectSQL結束
  
  /// <summary>
  /// 編號:06
  /// 內容摘要:每頁顯示的行數。
  /// </summary>
  public int PageSizes
  {
  get
  {
  if(ViewState[this.ClientID+"_PageSizes"] != null)
  return (int)ViewState[this.ClientID+"_PageSizes"];
  else
  return 20;
  }
  set
  {
  ViewState[this.ClientID+"_PageSizes"] = value;
  }
  }//PageSizes結束
  
  /// <summary>
  /// 編號:07
  /// 內容摘要:總頁數。
  /// </summary>
  private int PageCount
  {
  get
  {
  if(ViewState[this.ClientID+"_PageCount"] != null)
  return (int)ViewState[this.ClientID+"_PageCount"];
  else
  return 0;
  }
  set
  {
  ViewState[this.ClientID+"_PageCount"] = value;
  }
  }//PageCount結束
  
  /// <summary>
  /// 編號:08
  /// 內容摘要:當前頁碼。
  /// </summary>
  private int CurrentPageIndex
  {
  get
  {
  if(ViewState[this.ClientID+"_CurrentPageIndex"] != null)
  return (int)ViewState[this.ClientID+"_CurrentPageIndex"];
  else
  return 0;
  }
  set
  {
  ViewState[this.ClientID+"_CurrentPageIndex"] = value;
  if(this.LabelPageIndex!= null)
  this.LabelPageIndex.Text = (value+1).ToString();
  }
  }//CurrentPageIndex結束
  
  private LinkButton[] PageButtons
  {
  get
  {
  return new LinkButton[]{lbtnPage1,lbtnPage2,lbtnPage3,lbtnPage4,lbtnPage5,
  lbtnPage6,lbtnPage7,lbtnPage8,lbtnPage9,lbtnPage10};
  }
  }
  #endregion
  
  #region 方法區開始
  /// <summary>
  /// 方法編號:01
  /// 內容摘要:初始化頁面。
  /// </summary>
  private void Page_Load(object sender, System.EventArgs e)
  {
  }
  
  /// <summary>
  /// 方法編號:02
  /// 內容摘要:設定與本控制項關聯的控制項。
  /// </summary>
  /// <param name="mDataGrid">DataGrid控制項,必須提供</param>
  /// <param name="lblRowsCount">顯示查詢結果記錄數的標籤,可以輸入null,表示無需顯示</param>
  /// <param name="lblPageCount">顯示查詢結果總頁數的標籤,可以輸入null,表示無需顯示</param>
  /// <param name="lblPageIndex">顯示當前頁碼的標籤,可以輸入null,表示無需顯示</param>
  public void InitPagerControls(DataGrid mDataGrid,Label lblRowsCount,Label lblPageCount,Label lblPageIndex)
  {
  mDataGrid.AllowPaging = false;
  this.TheGrid = mDataGrid;
  this.LabelRowsCount = lblRowsCount;
  this.LabelPageCount = lblPageCount;
  this.LabelPageIndex = lblPageIndex;
  }
  
  /// <summary>
  /// 方法編號:03
  /// 內容摘要:改變查詢條件重新查詢資料。
  /// </summary>
  /// <param name="selectSQL">查詢的SQL語句,翻頁的時候始終使用該語句獲得資料集,直到下一次調用本方法</param>
  public void LoadData(string selectSQL)
  {
  //儲存新的SQL語句
  this.SelectSQL = selectSQL;
  
  //統計合格資料的條數
  int mRowCount = 0;
  string tmpSQL = "SELECT COUNT(*) FROM (" + selectSQL + ") t";
  Database db = new Database();
  System.Data.OleDb.OleDbDataReader tmpDr = db.GetDataReaderFromSQL(tmpSQL);
  if(tmpDr.Read())
  mRowCount = Convert.ToInt32(tmpDr[0]);
  
  //釋放資料連線
  tmpDr.Close();
  db.Dispose();
  
  //如果需要則顯示資料總行數
  if(this.LabelRowsCount!=null)
  this.LabelRowsCount.Text = mRowCount.ToString();
  
  //統計總頁數,如果需要則顯示總頁數
  this.PageCount = (int)Math.Ceiling(((double)mRowCount)/((double)PageSizes));
  if(this.LabelPageCount != null)
  this.LabelPageCount.Text = this.PageCount.ToString();
  
  //DataGrid的當前頁回到第一頁,按鈕也返回第一頁的狀態
  this.CurrentPageIndex = 0;
  this.ChangePageButtons(0);
  
  //查詢第一頁資料
  this.lbtnPages_Click(this.PageButtons[0],null);
  
  }
  
  /// <summary>
  /// 方法編號:04
  /// 內容摘要:查詢合格資料並綁定DataGrid顯示。
  /// </summary>
  private void BindData()
  {
  string selectSQL = this.SelectSQL;
  
  if(selectSQL != string.Empty)
  {
  Database db = new Database();//這是我自己的一個資料操作類
  
  int rowStart = (CurrentPageIndex * PageSizes) + 1;//當前頁的起始序號
  int rowEnd = (CurrentPageIndex+1) * PageSizes;//當前頁的結束序號
  
  string querySQL = "SELECT * FROM (SELECT t1.*,rownum sn FROM (" + selectSQL + ") t1) t2 " +
  " WHERE t2.sn BETWEEN "+rowStart.ToString()+" AND " + rowEnd.ToString();
  //查詢資料庫讀取合格資料
  DataTable tb = db.GetDataTableFromSQL(querySQL);
  
  //綁定DataGrid顯示
  TheGrid.DataSource = tb;
  TheGrid.DataBind();
  
  db.Dispose();
  }
  
  }
  
  /// <summary>
  /// 方法編號:05
  /// 內容摘要:改變翻頁按鈕的狀態,如顯示 21、22 、 ... 30 的頁碼。
  /// </summary>
  private void ChangePageButtons(int mIndex)
  {
  int tmpRem = 0;
  int tmpDiv = Math.DivRem(this.PageCount,10,out tmpRem);
  if(tmpRem == 0) tmpRem=10;
  
  //所有按鈕恢複預設的狀態
  for(int i = 0;i<10;i++)
  {
  int pageNum = mIndex*10 + i + 1;
  this.PageButtons[i].Text = pageNum.ToString();
  this.PageButtons[i].Visible = true;
  this.PageButtons[i].Enabled = true;
  this.PageButtons[i].Font.Size = System.Web.UI.WebControls.FontUnit.XSmall;
  }//end of for(int i = 0;i<10;i++)
  
  this.lbtnPre.Visible = true;
  this.lbtnNext.Visible = true;
  
  //如果目前是1~10頁,那麼不顯示1前面的“...”
  if(mIndex == 0)
  lbtnPre.Visible = false;
  
  //如果目前是最後的一頁,則不顯示後面的“...”;並且保證顯示的翻頁按鈕不超過最大頁數
  if(mIndex == tmpDiv)
  {
  lbtnNext.Visible = false;
  for(int i = 9;i>tmpRem-1;i=i-1)
  this.PageButtons[i].Visible = false;
  }
  
  }
  
  /// <summary>
  /// 方法編號:06
  /// 內容摘要:顯示前十個頁碼。
  /// </summary>
  private void lbtnPre_Click(object sender, System.EventArgs e)
  {
  this.ChangePageButtons((int)Math.Floor(Math.Max(this.CurrentPageIndex - 10,0)/10));
  this.lbtnPages_Click(this.PageButtons[9],null);
  }
  
  /// <summary>
  /// 方法編號:07
  /// 內容摘要:顯示後十個頁碼。
  /// </summary>
  private void lbtnNext_Click(object sender, System.EventArgs e)
  {
  this.ChangePageButtons((int)Math.Floor(Math.Max(this.CurrentPageIndex + 10,0)/10));
  this.lbtnPages_Click(this.PageButtons[0],null);
  }
  
  /// <summary>
  /// 方法編號:08
  /// 內容摘要:查詢繫結新的一頁。
  /// </summary>
  private void lbtnPages_Click(object sender, System.EventArgs e)
  {
  LinkButton tmpLBtn = sender as LinkButton;
  int pageIndex = Convert.ToInt32(tmpLBtn.Text.Trim()) - 1;
  this.CurrentPageIndex = pageIndex;
  
  foreach(LinkButton mLBtn in this.PageButtons)
  {
  mLBtn.Enabled = true;
  mLBtn.Font.Size = System.Web.UI.WebControls.FontUnit.XSmall;
  }
  
  tmpLBtn.Enabled = false;
  tmpLBtn.Font.Size = System.Web.UI.WebControls.FontUnit.Small;
  
  this.BindData();
  }
  
  #region Web Form設計器產生的程式碼
  override protected void OnInit(EventArgs e)
  {
  //
  // CODEGEN: 該調用是 ASP.NET Web Form設計器所必需的。
  //
  InitializeComponent();
  base.OnInit(e);
  }
  
  /// <summary>
  /// 設計器支援所需的方法 - 不要使用代碼編輯器
  /// 修改此方法的內容。
  /// </summary>
  private void InitializeComponent()
  {
  this.lbtnPre.Click += new System.EventHandler(this.lbtnPre_Click);
  this.lbtnPage1.Click += new System.EventHandler(this.lbtnPages_Click);
  this.lbtnPage2.Click += new System.EventHandler(this.lbtnPages_Click);
  this.lbtnPage3.Click += new System.EventHandler(this.lbtnPages_Click);
  this.lbtnPage4.Click += new System.EventHandler(this.lbtnPages_Click);
  this.lbtnPage5.Click += new System.EventHandler(this.lbtnPages_Click);
  this.lbtnPage6.Click += new System.EventHandler(this.lbtnPages_Click);
  this.lbtnPage7.Click += new System.EventHandler(this.lbtnPages_Click);
  this.lbtnPage8.Click += new System.EventHandler(this.lbtnPages_Click);
  this.lbtnPage9.Click += new System.EventHandler(this.lbtnPages_Click);
  this.lbtnPage10.Click += new System.EventHandler(this.lbtnPages_Click);
  this.lbtnNext.Click += new System.EventHandler(this.lbtnNext_Click);
  this.Load += new System.EventHandler(this.Page_Load);
  
  }
  #endregion
  
  #endregion
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.