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

Asp.net中DataGrid控件的自定義分頁

2022-06-13   來源: .NET編程 

  使用DataGrid時自帶的分頁實現起來雖然比較方便但是效率不高每次都需要讀取所有頁(整個記錄集)而加載的只是其中一頁造成了資源的浪費記錄多又會使效率變得很低下面通過DataGrid的自定義分頁功能來減少資源使用和提高效率

  實現的關鍵是設置AllowCustomPaging屬性位True並把VirtualItemCount屬性設置位總的記錄數給分頁提供依據前台的主要代碼如下

  <form id=Form method=post runat=server>

  <TABLE id=Table urn:schemasmicrosoftcom:office:smarttags />:chmetcnv TCSC= NumberType= Negative=False HasSpace=False SourceValue= UnitName=pt w:st=on>pt:chmetcnv> cellSpacing= cellPadding= width= align=center

  border=>

  <TR>

  <TD>

  <asp:datagrid id=DataGrid runat=server Width=% AllowPaging=True AllowCustomPaging=True>

  <PagerStyle FontSize=:chmetcnv TCSC= NumberType= Negative=False HasSpace=False SourceValue= UnitName=pt w:st=on>pt:chmetcnv> Mode=NumericPages></PagerStyle>

  </asp:datagrid></TD>

  </TR>

  </TABLE>

  </form>

  這裡使用的數據源還是假設為Northwind的Customers表

  下面是訪問單頁的存儲過程實現方式很多不過這個是最普通的

  CREATE PROCEDURE [GetCustomersDataPage]

  @PageIndex INT

  @PageSize  INT

  @RecordCount INT OUT

  @PageCount INT OUT

  AS

  SELECT @RecordCount = COUNT(*)  FROM   Customers

  SET @PageCount = CEILING(@RecordCount * / @PageSize)

  DECLARE @SQLSTR NVARCHAR()

  IF @PageIndex = OR @PageCount <=

  SET @SQLSTR =NSELECT TOP +STR( @PageSize )+

    CustomerID CompanyNameAddressPhone  FROM   Customers ORDER BY CustomerID DESC

  ELSE IF     @PageIndex = @PageCount

  SET @SQLSTR =N SELECT * FROM ( SELECT TOP +STR( @RecordCount @PageSize * @PageIndex )+

    CustomerID CompanyNameAddressPhone  FROM   Customers ORDER BY CustomerID ASC ) TempTable  ORDER BY CustomerID DESC

  ELSE

  SET @SQLSTR =N SELECT TOP  +STR( @PageSize )+ * FROM ( SELECT TOP +STR( @RecordCount @PageSize * @PageIndex )+

    CustomerID CompanyNameAddressPhone  FROM   Customers ORDER BY CustomerID ASC ) TempTable ORDER BY CustomerID DESC

  EXEC (@SQLSTR)

  GO

  獲取記錄數和頁數都采用存儲過程的輸出參數

  獲取數據源這裡返回一個DataSet

  先定義了連個數據成員

  private int pageCount;//頁數

  private int recordCount;//記錄數

  //獲取單頁數據

  private static DataSet GetCustomersData(int pageIndexint pageSizeref int recordCountref int pageCount)

  {

  string connString = ConfigurationSettingsAppSettings[ConnString];

  SqlConnection conn = new SqlConnection(connString);

  SqlCommand comm = new SqlCommand(GetCustomersDataPageconn);

  commParametersAdd(new SqlParameter(@PageIndexSqlDbTypeInt));

  commParameters[]Value = pageIndex;

  commParametersAdd(new SqlParameter(@PageSizeSqlDbTypeInt));

  commParameters[]Value = pageSize;

  commParametersAdd(new SqlParameter(@RecordCountSqlDbTypeInt));

  commParameters[]Direction = ParameterDirectionOutput;

  commParametersAdd(new SqlParameter(@PageCountSqlDbTypeInt));

  commParameters[]Direction = ParameterDirectionOutput;

  commCommandType = CommandTypeStoredProcedure;

  SqlDataAdapter dataAdapter = new SqlDataAdapter(comm);

  DataSet ds = new DataSet();

  dataAdapterFill(ds);

  recordCount = (int)commParameters[]Value;

  pageCount = (int)commParameters[]Value;

  return ds;

  }

  //綁定數據到DataGrid同時刷新數據總記錄數

  private void DataGridDataBind()

  {

  DataSet ds = GetCustomersData(PageIndexPageSizeref recordCountref pageCount);

  thisDataGridVirtualItemCount = RecordCount;

  thisDataGridDataSource = ds;

  thisDataGridDataBind();

  }

  下面是分頁的幾個變量屬性

  public int PageCount

  {

  get{return thisDataGridPageCount;}

  }

  public int PageSize

  {

  get{return thisDataGridPageSize;}

  }

  public int PageIndex

  {

  get{return thisDataGridCurrentPageIndex;}

  set{thisDataGridCurrentPageIndex = value;}

  }

  public int RecordCount

  {

  get{return recordCount;}

  }

  注冊DataGrid分頁事件

  //分頁事件處理

  private void DataGrid_PageIndexChanged(object source SystemWebUIWebControlsDataGridPageChangedEventArgs e)

  {

  DataGrid dg = (DataGrid)source;

  dgCurrentPageIndex = eNewPageIndex;

  DataGridDataBind();

  }

  最好判斷當前頁面是否是第一次加載防止重復加載兩次數據

  private void Page_Load(object sender SystemEventArgs e)

  {

  if(!PageIsPostBack)

  {

  DataGridDataBind();

  }

  }

  顯示界面如下

   src=http://imgeducitycn/img_///gif width= border=>

  這個例子中沒有顯示分頁的一些參數我們可以進一步對其進行改進


From:http://tw.wingwit.com/Article/program/net/201311/11473.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.