为DataGrid自定义分页添加自定义导航和分页信息

datagrid|分页

在上一篇文章中我讲到了对DataGrid实行自定义分页,这可以避免为了显示一页数据而获取整个数据记录集,从而提高分页效率,不过使用的导航还是DataGrid自带的数字连接或简单的上一页,下一页,而且看不到总页数、总记录数之类的信息。下面就为他增加我们所需要的部分。

先来看看修改后的分页显示,截图如下:

(图一)

使用的数据源同上一篇文章(Asp.net中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 * 1.0 / @PageSize)

DECLARE @SQLSTR NVARCHAR(1000)

IF @PageIndex = 0 OR @PageCount <= 1

SET @SQLSTR =N'SELECT TOP '+STR( @PageSize )+

' CustomerID, CompanyName,Address,Phone FROM Customers ORDER BY CustomerID DESC

ELSE IF @PageIndex = @PageCount - 1

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 "-//W3C//DTD HTML 4.0 Transitional//EN" >

<HTML>

<HEAD>

<title>DataGridPaging</title>

<meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">

<meta content="C#" name="CODE_LANGUAGE">

<meta content="JavaScript" name="vs_defaultClientScript">

<meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema">

</HEAD>

<body>

<form id="Form1" method="post" runat="server">

<TABLE id="Table1" style="FONT-SIZE: 9pt" cellSpacing="1" cellPadding="1" width="450" align="center"

border="1">

<TR>

<TD><asp:datagrid id="DataGrid1" runat="server" AllowPaging="True" AllowCustomPaging="True" Width="100%">

<FooterStyle Font-Size="9pt"></FooterStyle>

<HeaderStyle Font-Size="9pt"></HeaderStyle>

<PagerStyle Visible="False" Font-Size="9pt" Mode="NumericPages"></PagerStyle>

</asp:datagrid></TD>

</TR>

<TR>

<TD>

<TABLE id="Table2" style="FONT-SIZE: 9pt" cellSpacing="1" cellPadding="1" width="100%"

align="center" border="1">

<TR>

<TD style="WIDTH: 150px"><asp:linkbutton id="LBtnFirst" runat="server" CommandName="First">首页</asp:linkbutton> 

<asp:linkbutton id="LBtnPrev" runat="server" CommandName="Prev">上一页</asp:linkbutton> 

<asp:linkbutton id="LBtnNext" runat="server" CommandName="Next">下一页</asp:linkbutton> 

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

<TD>第<asp:literal id="LtlPageIndex" runat="server"></asp:literal>页  共<asp:literal id="LtlPageCount" runat="server"></asp:literal>页 

每页<asp:Literal id="LtlPageSize" runat="server"></asp:Literal>条  共<asp:Literal id="LtlRecordCount" runat="server"></asp:Literal>条 

</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 DataGrid1;

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.DataGrid1.VirtualItemCount = RecordCount;

this.DataGrid1.DataSource = ds;

this.DataGrid1.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 comm = new SqlCommand("GetCustomersDataPage",conn);

comm.Parameters.Add(new SqlParameter("@PageIndex",SqlDbType.Int));

comm.Parameters[0].Value = pageIndex;

comm.Parameters.Add(new SqlParameter("@PageSize",SqlDbType.Int));

comm.Parameters[1].Value = pageSize;

comm.Parameters.Add(new SqlParameter("@RecordCount",SqlDbType.Int));

comm.Parameters[2].Direction = ParameterDirection.Output;

comm.Parameters.Add(new SqlParameter("@PageCount",SqlDbType.Int));

comm.Parameters[3].Direction = ParameterDirection.Output;

comm.CommandType = CommandType.StoredProcedure;

SqlDataAdapter dataAdapter = new SqlDataAdapter(comm);

DataSet ds = new DataSet();

dataAdapter.Fill(ds);

recordCount = (int)comm.Parameters[2].Value;

pageCount = (int)comm.Parameters[3].Value;

return ds;

}

private void LBtnNavigation_Click(object sender, System.EventArgs e)

{

LinkButton btn = (LinkButton)sender;

switch(btn.CommandName)

{

case "First":

PageIndex = 0;

break;

case "Prev"://if( PageIndex > 0 )

PageIndex = PageIndex - 1;

break;

case "Next"://if( PageIndex < PageCount -1)

PageIndex = PageIndex + 1;

break;

case "Last":

PageIndex = PageCount - 1;

break;

}

DataGridDataBind();

}

/// <summary>

/// 控制导航按钮或数字的状态

/// </summary>

public void SetPagingState()

{

if( PageCount <= 1 )//( RecordCount <= PageSize )//小于等于一页

{

this.LBtnFirst.Enabled = false;

this.LBtnPrev.Enabled = false;

this.LBtnNext.Enabled = false;

this.LBtnLast.Enabled = false;

}

else //有多页

{

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

{

this.LBtnFirst.Enabled = false;

this.LBtnPrev.Enabled = false;

this.LBtnNext.Enabled = true;

this.LBtnLast.Enabled = true;

}

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

{

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 == 0)

{

this.LtlPageCount.Text = "0";

this.LtlPageIndex.Text = "0";

}

else

{

this.LtlPageCount.Text = PageCount.ToString();

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

}

}

public int PageCount

{

get

{

return this.DataGrid1.PageCount;

}

}

public int PageSize

{

get

{

return this.DataGrid1.PageSize;

}

}

public int PageIndex

{

get

{

return this.DataGrid1.CurrentPageIndex;

}

set

{

this.DataGrid1.CurrentPageIndex = value;

}

}

public int RecordCount

{

get

{

return recordCount;

}

}

}

}

上面的代码比较简单,也就不用分析了,如果有什么好的建议或有问题可以在Blog上留言,很高兴同大家交流。

时间: 2024-11-03 19:02:14

为DataGrid自定义分页添加自定义导航和分页信息的相关文章

PHP封装分页函数实现文本分页和数字分页_php实例

最近,在项目中要用到分页.分页功能是经常使用的一个功能,所以,对其以函数形式进行了封装. // 分页分装/*** $pageType 分页类型 1是数字分页 2是文本分页* 可以将$pageTotal,$page,$total等数据作为参数传递,或者在paging作为全局变量(推荐)*/function paging($pageType){ global $pageTotal,$page,$total; if($pageType == 1) { echo '<div id="pagenum

thinkPHP 分页写成自己分页类程序代码

我们行来看我自己做的一个实例  代码如下 复制代码 CREATE TABLE IF NOT EXISTS `think_form` (   `id` smallint(4) unsigned NOT NULL AUTO_INCREMENT,   `title` varchar(255) NOT NULL,   `content` varchar(255) NOT NULL,   `create_time` int(11) unsigned NOT NULL,   PRIMARY KEY (`id

Word文档中插入分页符并对分页方式怎么设置

  Word文档中插入分页符并对分页方式怎么设置           1.打开需要处理的文档,将插入点光标放置到需要分页的位置.在功能区的"页面布局"选项卡中单击"页面设置"组中的"插入分页符和分节符"按钮,在打开的下拉列表中选择"分页符"选项,如图1所示.此时,文档将从插入点光标处插入分页符,同时完成分页,如图2所示. 图1 选择"分页符"选项 图2 插入分页符 2.打开"开始"选项卡

Android百度地图自定义公交路线导航_Android

一.问题描述 基于百度地图实现检索指定城市指定公交的交通路线图,效果如图所示 二.通用组件Application类,主要创建并初始化BMapManager public class App extends Application { static App mDemoApp; //百度MapAPI的管理类 public BMapManager mBMapMan = null; // 授权Key // 申请地址:http://dev.baidu.com/wiki/static/imap/key/ p

smartforms强制分页时,每分页一次就少显示一条记录

问题描述 smartforms强制分页时,每分页一次就少显示一条记录我是在table中最后一个文本下面建的programlines和commandprogram中输入gv_pagelines"一页显示的记录数输出gv_modgv_count"循环到第几条记录gv_count=gv_count+1.gv_mod=gv_countmodgv_pagelines.command中分页的条件为:gv_mod=0这样处理所分的页数是正确的,但是分页的时候,每分一次页就会多一条记录显示不出来比喻一

ios-各位高手。。怎样自定义写一个导航栏??

问题描述 各位高手..怎样自定义写一个导航栏?? 各位高手..怎样自定义写一个导航栏??我不想用系统自带的导航,自己写一二个导航栏,请高手指点

repater 分页-Repater 无法实现分页效果

问题描述 Repater 无法实现分页效果 public DataSet getArticle(string sql) { CheckConnection(); try { SqlDataAdapter sda = new SqlDataAdapter(sql con); DataSet ds = new DataSet(); sda.Fill(ds); return ds; } catch (Exception ex) { throw new Exception(ex.Message); }

wpf-WPF DATAGRID 自定义列

问题描述 WPF DATAGRID 自定义列 datagrid 自定义列 DataTemplate 中,在xaml中怎么根据数据源的值做更改,例如: <Button Text="{Binding Path=state}" /> 中 state的值是1或0,我想显示的是冻结或正常 解决方案 http://www.cnblogs.com/zlgcool/archive/2008/10/22/1316605.html 可以添加转换器进行实现

link中如何实现自定义的数据导航函数?

问题描述 link中如何实现自定义的数据导航函数? link中如何实现自定义的数据导航函数? 解决方案 不知道你说的"数据导航函数"是什么,翻遍google都没找到