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

為DataGrid自定義分頁添加自定義導航和分頁信息

2022-06-13   來源: .NET編程 
在上一篇文章中我講到了對DataGrid實行自定義分頁這可以避免為了顯示一頁數據而獲取整個數據記錄集從而提高分頁效率不過使用的導航還是DataGrid自帶的數字連接或簡單的上一頁下一頁而且看不到總頁數總記錄數之類的信息下面就為他增加我們所需要的部分

  先來看看修改後的分頁顯示截圖如下

  

  
(圖一)

  使用的數據源同上一篇文章(中DataGrid控件的自定義分頁)相同都是訪問Northwind庫為了獨立開來這裡還是把存儲過程列了一下

  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

   

  下面就就把代碼貼了一下

  Aspx文件代碼如下

  <%@ Page language=c# Codebehind=DataGridCustomPagingaspxcs AutoEventWireup=false Inherits=ZZAspnetPagingDataGridCustomPaging %>

  <!DOCTYPE HTML PUBLIC //WC//DTD HTML Transitional//EN >

  <HTML>

  <HEAD>

  <title>DataGridPaging</title>

  <meta content=Microsoft Visual Studio NET name=GENERATOR>

  <meta content=C# name=CODE_LANGUAGE>

  <meta content=JavaScript name=vs_defaultClientScript>

  <meta content= name=vs_targetSchema>

  </HEAD>

  <body>

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

  <TABLE id=Table 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 AllowPaging=True AllowCustomPaging=True Width=%>

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

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

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

  </asp:datagrid></TD>

  </TR>

  <TR>

  <TD>

  <TABLE id=Table NumberType= Negative=False HasSpace=False SourceValue= UnitName=pt w:st=on>pt:chmetcnv> cellSpacing= cellPadding= width=%

  align=center border=>

  <TR>

  <TD ><asp:linkbutton id=LBtnFirst runat=server CommandName=First>首頁</asp:linkbutton>&nbsp;

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

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

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

  <TD>第<asp:literal id=LtlPageIndex runat=server></asp:literal>頁&nbsp; 共<asp:literal id=LtlPageCount runat=server></asp:literal>頁&nbsp;

  每頁<asp:Literal id=LtlPageSize runat=server></asp:Literal>條&nbsp; 共<asp:Literal id=LtlRecordCount runat=server></asp:Literal>條&nbsp;

  </TD>

  </TR>

  </TABLE>

  </TD>

  </TR>

  </TABLE>

  </form>

  </body>

  </HTML>

   

  Aspxcs文件代碼如下

  using System;

  using SystemCollections;

  using SystemComponentModel;

  using SystemData;

  using SystemDrawing;

  using SystemWeb;

  using SystemWebSessionState;

  using SystemWebUI;

  using SystemWebUIWebControls;

  using SystemWebUIHtmlControls;

  using SystemDataSqlClient;

  using SystemConfiguration;

   

  namespace ZZAspnetPaging

  {

  public class DataGridCustomPaging : SystemWebUIPage

  {

  private int pageCount;

  private int recordCount;

   

  protected SystemWebUIWebControlsLinkButton LBtnFirst;

  protected SystemWebUIWebControlsLinkButton LBtnPrev;

  protected SystemWebUIWebControlsLinkButton LBtnNext;

  protected SystemWebUIWebControlsLinkButton LBtnLast;

  protected SystemWebUIWebControlsLiteral LtlPageIndex;

  protected SystemWebUIWebControlsLiteral LtlPageCount;

  protected SystemWebUIWebControlsLiteral LtlPageSize;

  protected SystemWebUIWebControlsLiteral LtlRecordCount;

  protected SystemWebUIWebControlsDataGrid DataGrid;

  

  private void Page_Load(object sender SystemEventArgs e)

  {

  if(!PageIsPostBack)

  {

  DataGridDataBind();

  }

  }

   

  //綁定數據

  private void DataGridDataBind()

  {

  DataSet ds = GetCustomersData(PageIndexPageSizeref recordCountref pageCount);

  thisDataGridVirtualItemCount = RecordCount;

  thisDataGridDataSource = ds;

  thisDataGridDataBind();

  SetPagingState();

  }

   

  #region Web 窗體設計器生成的代碼

  override protected void OnInit(EventArgs e)

  {

  InitializeComponent();

  baseOnInit(e);

  }

  

  private void InitializeComponent()

  {   

  thisLBtnFirstClick += new SystemEventHandler(thisLBtnNavigation_Click);

  thisLBtnPrevClick += new SystemEventHandler(thisLBtnNavigation_Click);

  thisLBtnNextClick += new SystemEventHandler(thisLBtnNavigation_Click);

  thisLBtnLastClick += new SystemEventHandler(thisLBtnNavigation_Click);

  thisLoad += new SystemEventHandler(thisPage_Load);

  }

  #endregion

   

  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;

  }

   

  private void LBtnNavigation_Click(object sender SystemEventArgs e)

  {

  LinkButton btn = (LinkButton)sender;

  switch(btnCommandName)

  {

  case First:

  PageIndex = ;

  break;

  case Prev://if( PageIndex > )

  PageIndex = PageIndex ;

  break;

  case Next://if( PageIndex < PageCount )

  PageIndex = PageIndex + ;

  break;

  case Last:

  PageIndex = PageCount ;

  break;

  }

  DataGridDataBind();             

  }

   

  /// <summary>

  /// 控制導航按鈕或數字的狀態

  /// </summary>

  public void SetPagingState()

  {

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

  {

  thisLBtnFirstEnabled = false;

  thisLBtnPrevEnabled = false;

  thisLBtnNextEnabled = false;

  thisLBtnLastEnabled = false;

  }

  else //有多頁

  {

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

  {

  thisLBtnFirstEnabled = false;

  thisLBtnPrevEnabled = false;

  thisLBtnNextEnabled = true;

  thisLBtnLastEnabled = true;                                   

  }

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

  {

  thisLBtnFirstEnabled = true;

  thisLBtnPrevEnabled = true;

  thisLBtnNextEnabled = false;

  thisLBtnLastEnabled = false;                                  

  }

  else //中間頁

  {

  thisLBtnFirstEnabled = true;

  thisLBtnPrevEnabled = true;

  thisLBtnNextEnabled = true;

  thisLBtnLastEnabled = true;

  }

  }

  

  thisLtlPageSizeText = PageSizeToString();

  thisLtlRecordCountText = RecordCountToString();

  if(RecordCount == )

  {

  thisLtlPageCountText = ;

  thisLtlPageIndexText = ;

  }   

  else

  {

  thisLtlPageCountText = PageCountToString();

  thisLtlPageIndexText = (PageIndex + )ToString();

  }

  }

   

  

  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;

  }            

  }

  }

  }


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