熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Oracle >> 正文

一個ASP.Net的DataGrid分頁控件

2022-06-13   來源: Oracle 

  寫的不算好僅供參考
  
  GridPagerascx:
  <%@ Control Language=c# AutoEventWireup=false Codebehind=GridPagerascxcs Inherits=TestBaseClassGridPager TargetSchema=%><?xml:namespace prefix = asp /><asp:linkbutton id=lbtnPre runat=server Text= Visible=False></asp:linkbutton><asp:linkbutton id=lbtnPage runat=server Text= Visible=False Enabled=False FontSize=Small></asp:linkbutton> <asp:linkbutton id=lbtnPage runat=server Text= Visible=False></asp:linkbutton><asp:linkbutton id=lbtnPage runat=server Text= Visible=False></asp:linkbutton><asp:linkbutton id=lbtnPage runat=server Text= Visible=False></asp:linkbutton><asp:linkbutton id=lbtnPage runat=server Text= Visible=False></asp:linkbutton><asp:linkbutton id=lbtnPage runat=server Text= Visible=False></asp:linkbutton><asp:linkbutton id=lbtnPage runat=server Text= Visible=False></asp:linkbutton><asp:linkbutton id=lbtnPage runat=server Text= Visible=False></asp:linkbutton><asp:linkbutton id=lbtnPage runat=server Text= Visible=False></asp:linkbutton><asp:linkbutton id=lbtnPage runat=server Text= Visible=False></asp:linkbutton><asp:linkbutton id=lbtnNext runat=server Text= Visible=False></asp:linkbutton>
  ///////////////////////////////////////////////////////////////////////////////////////
  GridPagerascxcs:
  using System;
  using SystemData;
  using SystemDrawing;
  using SystemWeb;
  using SystemWebUIWebControls;
  namespace TestBaseClass
  {
  public class GridPager : SystemWebUIUserControl
  {
  #region 類變量
  protected SystemWebUIWebControlsLinkButton lbtnNext;
  protected SystemWebUIWebControlsLinkButton lbtnPage;
  protected SystemWebUIWebControlsLinkButton lbtnPage;
  protected SystemWebUIWebControlsLinkButton lbtnPage;
  protected SystemWebUIWebControlsLinkButton lbtnPage;
  protected SystemWebUIWebControlsLinkButton lbtnPage;
  protected SystemWebUIWebControlsLinkButton lbtnPage;
  protected SystemWebUIWebControlsLinkButton lbtnPage;
  protected SystemWebUIWebControlsLinkButton lbtnPage;
  protected SystemWebUIWebControlsLinkButton lbtnPage;
  protected SystemWebUIWebControlsLinkButton lbtnPage;
  protected SystemWebUIWebControlsLinkButton lbtnPre;
  #endregion
  
  #region
  /// <summary>
  /// 編號
  /// 內容摘要翻頁的對象DataGrid控件
  /// </summary>
  public DataGrid TheGrid;
  
  /// <summary>
  /// 編號
  /// 內容摘要顯示查詢結果記錄數的標簽可以不設置表示無需顯示
  /// </summary>
  public Label LabelRowsCount;
  
  /// <summary>
  /// 編號
  /// 內容摘要顯示查詢結果總頁數的標簽可以不設置表示無需顯示
  /// </summary>
  public Label LabelPageCount;
  
  /// <summary>
  /// 編號
  /// 內容摘要顯示當前頁碼的標簽可以不設置表示無需顯示
  /// </summary>
  public Label LabelPageIndex;
  
  /// <summary>
  /// 編號
  /// 內容摘要查詢數據的SQL語句
  /// </summary>
  private string SelectSQL
  {
  get
  {
  if(ViewState[thisClientID+_SelectSQL] != null)
  return ViewState[thisClientID+_SelectSQL]ToString();
  else
  return stringEmpty;
  }
  set
  {
  ViewState[thisClientID+_SelectSQL] = value;
  }
  }//SelectSQL結束
  
  /// <summary>
  /// 編號
  /// 內容摘要每頁顯示的行數
  /// </summary>
  public int PageSizes
  {
  get
  {
  if(ViewState[thisClientID+_PageSizes] != null)
  return (int)ViewState[thisClientID+_PageSizes];
  else
  return ;
  }
  set
  {
  ViewState[thisClientID+_PageSizes] = value;
  }
  }//PageSizes結束
  
  /// <summary>
  /// 編號
  /// 內容摘要總頁數
  /// </summary>
  private int PageCount
  {
  get
  {
  if(ViewState[thisClientID+_PageCount] != null)
  return (int)ViewState[thisClientID+_PageCount];
  else
  return ;
  }
  set
  {
  ViewState[thisClientID+_PageCount] = value;
  }
  }//PageCount結束
  
  /// <summary>
  /// 編號
  /// 內容摘要當前頁碼
  /// </summary>
  private int CurrentPageIndex
  {
  get
  {
  if(ViewState[thisClientID+_CurrentPageIndex] != null)
  return (int)ViewState[thisClientID+_CurrentPageIndex];
  else
  return ;
  }
  set
  {
  ViewState[thisClientID+_CurrentPageIndex] = value;
  if(thisLabelPageIndex!= null)
  thisLabelPageIndexText = (value+)ToString();
  }
  }//CurrentPageIndex結束
  
  private LinkButton[] PageButtons
  {
  get
  {
  return new LinkButton[]{lbtnPagelbtnPagelbtnPagelbtnPagelbtnPage
  lbtnPagelbtnPagelbtnPagelbtnPagelbtnPage};
  }
  }
  #endregion
  
  #region 方法區開始
  /// <summary>
  /// 方法編號
  /// 內容摘要初始化頁面
  /// </summary>
  private void Page_Load(object sender SystemEventArgs e)
  {
  }
  
  /// <summary>
  /// 方法編號
  /// 內容摘要設置與本控件關聯的控件
  /// </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 mDataGridLabel lblRowsCountLabel lblPageCountLabel lblPageIndex)
  {
  mDataGridAllowPaging = false;
  thisTheGrid = mDataGrid;
  thisLabelRowsCount = lblRowsCount;
  thisLabelPageCount = lblPageCount;
  thisLabelPageIndex = lblPageIndex;
  }
  
  /// <summary>
  /// 方法編號
  /// 內容摘要改變查詢條件重新查詢數據
  /// </summary>
  /// <param name=selectSQL>查詢的SQL語句翻頁的時候始終使用該語句獲得數據集直到下一次調用本方法</param>
  public void LoadData(string selectSQL)
  {
  //保存新的SQL語句
  thisSelectSQL = selectSQL;
  
  //統計符合條件的數據的條數
  int mRowCount = ;
  string tmpSQL = SELECT COUNT(*) FROM ( + selectSQL + ) t;
  Database db = new Database();
  SystemDataOleDbOleDbDataReader tmpDr = dbGetDataReaderFromSQL(tmpSQL);
  if(tmpDrRead())
  mRowCount = ConvertToInt(tmpDr[]);
  
  //釋放數據連接
  tmpDrClose();
  dbDispose();
  
  //如果需要則顯示數據總行數
  if(thisLabelRowsCount!=null)
  thisLabelRowsCountText = mRowCountToString();
  
  //統計總頁數如果需要則顯示總頁數
  thisPageCount = (int)MathCeiling(((double)mRowCount)/((double)PageSizes));
  if(thisLabelPageCount != null)
  thisLabelPageCountText = thisPageCountToString();
  
  //DataGrid的當前頁回到第一頁按鈕也返回第一頁的狀態
  thisCurrentPageIndex = ;
  thisChangePageButtons();
  
  //查詢第一頁數據
  thislbtnPages_Click(thisPageButtons[]null);
  
  }
  
  /// <summary>
  /// 方法編號
  /// 內容摘要查詢符合條件的數據並綁定DataGrid顯示
  /// </summary>
  private void BindData()
  {
  string selectSQL = thisSelectSQL;
  
  if(selectSQL != stringEmpty)
  {
  Database db = new Database();//這是我自己的一個數據操作類
  
  int rowStart = (CurrentPageIndex * PageSizes) + ;//當前頁的起始序號
  int rowEnd = (CurrentPageIndex+) * PageSizes;//當前頁的結束序號
  
  string querySQL = SELECT * FROM (SELECT t*rownum sn FROM ( + selectSQL + ) t) t +
   WHERE tsn BETWEEN +rowStartToString()+ AND + rowEndToString();
  //查詢數據庫讀取符合條件的數據
  DataTable tb = dbGetDataTableFromSQL(querySQL);
  
  //綁定DataGrid顯示
  TheGridDataSource = tb;
  TheGridDataBind();
  
  dbDispose();
  }
  
  }
  
  /// <summary>
  /// 方法編號
  /// 內容摘要改變翻頁按鈕的狀態如顯示 的頁碼
  /// </summary>
  private void ChangePageButtons(int mIndex)
  {
  int tmpRem = ;
  int tmpDiv = MathDivRem(thisPageCountout tmpRem);
  if(tmpRem == ) tmpRem=;
  
  //所有按鈕恢復默認的狀態
  for(int i = ;i<;i++)
  {
  int pageNum = mIndex* + i + ;
  thisPageButtons[i]Text = pageNumToString();
  thisPageButtons[i]Visible = true;
  thisPageButtons[i]Enabled = true;
  thisPageButtons[i]FontSize = SystemWebUIWebControlsFontUnitXSmall;
  }//end of for(int i = ;i<;i++)
  
  thislbtnPreVisible = true;
  thislbtnNextVisible = true;
  
  //如果目前是~那麼不顯示前面的
  if(mIndex == )
  lbtnPreVisible = false;
  
  //如果目前是最後的一頁則不顯示後面的並且保證顯示的翻頁按鈕不超過最大頁數
  if(mIndex == tmpDiv)
  {
  lbtnNextVisible = false;
  for(int i = ;i>tmpRem;i=i)
  thisPageButtons[i]Visible = false;
  }
  
  }
  
  /// <summary>
  /// 方法編號
  /// 內容摘要顯示前十個頁碼
  /// </summary>
  private void lbtnPre_Click(object sender SystemEventArgs e)
  {
  thisChangePageButtons((int)MathFloor(MathMax(thisCurrentPageIndex )/));
  thislbtnPages_Click(thisPageButtons[]null);
  }
  
  /// <summary>
  /// 方法編號
  /// 內容摘要顯示後十個頁碼
  /// </summary>
  private void lbtnNext_Click(object sender SystemEventArgs e)
  {
  thisChangePageButtons((int)MathFloor(MathMax(thisCurrentPageIndex + )/));
  thislbtnPages_Click(thisPageButtons[]null);
  }
  
  /// <summary>
  /// 方法編號
  /// 內容摘要查詢綁定新的一頁
  /// </summary>
  private void lbtnPages_Click(object sender SystemEventArgs e)
  {
  LinkButton tmpLBtn = sender as LinkButton;
  int pageIndex = ConvertToInt(tmpLBtnTextTrim()) ;
  thisCurrentPageIndex = pageIndex;
  
  foreach(LinkButton mLBtn in thisPageButtons)
  {
  mLBtnEnabled = true;
  mLBtnFontSize = SystemWebUIWebControlsFontUnitXSmall;
  }
  
  tmpLBtnEnabled = false;
  tmpLBtnFontSize = SystemWebUIWebControlsFontUnitSmall;
  
  thisBindData();
  }
  
  #region Web 窗體設計器生成的代碼
  override protected void OnInit(EventArgs e)
  {
  //
  // CODEGEN: 該調用是 ASPNET Web 窗體設計器所必需的
  //
  InitializeComponent();
  baseOnInit(e);
  }
  
  /// <summary>
  /// 設計器支持所需的方法 不要使用代碼編輯器
  /// 修改此方法的內容
  /// </summary>
  private void InitializeComponent()
  {
  thislbtnPreClick += new SystemEventHandler(thislbtnPre_Click);
  thislbtnPageClick += new SystemEventHandler(thislbtnPages_Click);
  thislbtnPageClick += new SystemEventHandler(thislbtnPages_Click);
  thislbtnPageClick += new SystemEventHandler(thislbtnPages_Click);
  thislbtnPageClick += new SystemEventHandler(thislbtnPages_Click);
  thislbtnPageClick += new SystemEventHandler(thislbtnPages_Click);
  thislbtnPageClick += new SystemEventHandler(thislbtnPages_Click);
  thislbtnPageClick += new SystemEventHandler(thislbtnPages_Click);
  thislbtnPageClick += new SystemEventHandler(thislbtnPages_Click);
  thislbtnPageClick += new SystemEventHandler(thislbtnPages_Click);
  thislbtnPageClick += new SystemEventHandler(thislbtnPages_Click);
  thislbtnNextClick += new SystemEventHandler(thislbtnNext_Click);
  thisLoad += new SystemEventHandler(thisPage_Load);
  
  }
  #endregion
  
  #endregion
From:http://tw.wingwit.com/Article/program/Oracle/201311/17308.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.