知识大全 为DataGrid自定义分页添加自定义导航和分页信息

Posted

篇首语:知之者不如好之者,好之者不如乐之者。本文由小常识网(cha138.com)小编为大家整理,主要介绍了知识大全 为DataGrid自定义分页添加自定义导航和分页信息相关的知识,希望对你有一定的参考价值。

为DataGrid自定义分页添加自定义导航和分页信息  以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!

在上一篇文章中我讲到了对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 =N SELECT TOP +STR( @PageSize )+

     CustomerID CompanyName Address Phone  FROM   Customers ORDER BY CustomerID DESC

  ELSE IF     @PageIndex = @PageCount             

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

     CustomerID CompanyName Address Phone  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 CompanyName Address Phone  FROM   Customers ORDER BY CustomerID ASC ) TempTable ORDER BY CustomerID DESC

    

  EXEC (@SQLSTR)

  GO

    

  下面就就把代码贴了一下

  Aspx文件代码如下

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

  <!DOCTYPE HTML PUBLIC //W C //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 Font Size= :chmetcnv TCSC= NumberType= Negative= False HasSpace= False SourceValue= UnitName= pt w:st= on > pt :chmetcnv> ></FooterStyle>

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

  <PagerStyle Visible= False Font Size= :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>

    

  Aspx 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 DataGridCustomPaging : System Web UI Page

  

  private int pageCount;

  private int recordCount;

    

  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 DataGrid ;

  

  private void Page_Load(object sender System EventArgs e)

  

  if(!Page IsPostBack)

  

  DataGridDataBind();

  

  

    

  //绑定数据

  private void DataGridDataBind()

  

  DataSet ds = GetCustomersData(PageIndex PageSize ref recordCount ref pageCount);

  this DataGrid VirtualItemCount = RecordCount;

  this DataGrid DataSource = ds;

  this DataGrid DataBind();

  SetPagingState();

  

    

  #region Web 窗体设计器生成的代码

  override protected void OnInit(EventArgs e)

  

  InitializeComponent();

  base OnInit(e);

  

  

  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 static DataSet GetCustomersData(int pageIndex int pageSize ref int recordCount ref int pageCount)

  

  string connString = ConfigurationSettings AppSettings[ ConnString ];

  SqlConnection conn = new SqlConnection(connString);

  SqlCommand m = new SqlCommand( GetCustomersDataPage conn);

  m Parameters Add(new SqlParameter( @PageIndex SqlDbType Int));

  m Parameters[ ] Value = pageIndex;

  m Parameters Add(new SqlParameter( @PageSize SqlDbType Int));

  m Parameters[ ] Value = pageSize;

  m Parameters Add(new SqlParameter( @RecordCount SqlDbType Int));

  m Parameters[ ] Direction = ParameterDirection Output;

  m Parameters Add(new SqlParameter( @PageCount SqlDbType Int));

  m Parameters[ ] Direction = ParameterDirection Output;

  m CommandType = CommandType StoredProcedure;

  SqlDataAdapter dataAdapter = new SqlDataAdapter(m);

  DataSet ds = new DataSet();

  dataAdapter Fill(ds);

  recordCount = (int)m Parameters[ ] Value;

  pageCount = (int)m Parameters[ ] Value;

  return ds;

  

    

  private void LBtnNavigation_Click(object sender System EventArgs e)

  

  LinkButton btn = (LinkButton)sender;

  switch(btn CommandName)

  

  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 )//小于等于一页

  

  this LBtnFirst Enabled = false;

  this LBtnPrev Enabled = false;

  this LBtnNext Enabled = false;

  this LBtnLast Enabled = false;

  

  else //有多页

  

  if( PageIndex == )//当前为第一页

  

  this LBtnFirst Enabled = false;

  this LBtnPrev Enabled = false;

  this LBtnNext Enabled = true;

  this LBtnLast Enabled = true;                                   

  

  else if( PageIndex == PageCount )//当前为最后页

  

  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;

  

  

  

  this LtlPageSize Text = PageSize ToString();

  this LtlRecordCount Text = RecordCount ToString();

  if(RecordCount == )

  

  this LtlPageCount Text = ;

  this LtlPageIndex Text = ;

     

  else

  

  this LtlPageCount Text = PageCount ToString();

  this LtlPageIndex Text = (PageIndex + ) ToString();

  

  

    

  

  public int PageCount

  

  get

  

  return this DataGrid PageCount;

     

  

    

  public int PageSize

  

  get

  

  return this DataGrid PageSize;

              

  

    

  public int PageIndex

  

  get

  

  return this DataGrid CurrentPageIndex;

  

  set

  

  this DataGrid CurrentPageIndex = value;

  

  

    

  public int RecordCount

  

  get

  

  return recordCount;

              

  

  

cha138/Article/program/net/201311/12572

相关参考

知识大全 ASP.NET中自定义DataGrid分页设置的实现

ASP.NET中自定义DataGrid分页设置的实现  以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!

知识大全 GridView分页的实现以及自定义分页样式功能实例

GridView分页的实现以及自定义分页样式功能实例  以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!

知识大全 ASP.NET存储过程自定义分页详解

ASP.NET存储过程自定义分页详解  以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧! &n

知识大全 基于ASP.NET的自定义分页显示[1]

基于ASP.NET的自定义分页显示[1]  以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!  引言  

知识大全 基于ASP.NET的自定义分页显示[3]

基于ASP.NET的自定义分页显示[3]  以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧! 

知识大全 基于ASP.NET的自定义分页显示[4]

基于ASP.NET的自定义分页显示[4]  以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧! 

知识大全 基于ASP.NET的自定义分页显示[2]

基于ASP.NET的自定义分页显示[2]  以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧! 

知识大全 自定义分页控件源码asp.net(c#)

  可能大家有用得着的地方发出来一起研究下代码如下  Pagercs 服务器控件源代码  usingSystem;   usingSystemWeb; 

知识大全 Asp.NET自定义DataGrid控件

Asp.NET自定义DataGrid控件  以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!  一&nb

知识大全 VB.NET中关于DataGrid颜色的自定义

VB.NET中关于DataGrid颜色的自定义  以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!  近