为DataGrid的自带分页添加首页、尾页及状态功能

版权声明:CSDN是本Blog托管服务提供商。如本文牵涉版权问题,CSDN不承担相关责任,请版权拥有者直接与文章作者联系解决。

** 为 DataGrid ** ** 的自带分页添加首页、尾页及状态功能 **

郑 佐 2004-9-20

DataGrid 提供了分页功能,不过看上去功能有限,但是我们可以通过 DataGrid 的一些属性来获取状态以及增加首页、尾页功能按钮。这里没有使用 DataGrid 的自定义分页功能,如果在速度效率不是很讲究的情况下,由 DataGrid 自己管理分页还是不错的,付出的代价就是要把整个相关数据取出来后再删选指定页的数据。好处就是开发速度快,不需要写分页的存储过程。本文事例使用的是 Sql Server 中的 Northwind 数据库。运行界面如下:


对于前台的显示界面,我放了一个 DataGrid ;四个 LinkButton 导向按钮;四个 Literal 来显示纪录状态。

剩下的就是用表格定位。

这里需要设置 DataGrid 的 AllowPaging 属性为 True ,同时设置 AllowCustomPaging 属性位 false( 默认为 false), 设置 PagerStyle 的 Visible 属性为 False ,使前台不显示。

前台的代码如下:

1@ Page language="c#" Codebehind="DataGridPaging.aspx.cs" AutoEventWireup="false" Inherits="ZZ.AspnetPaging.DataGridPaging" 
 1<html>
 2<head>
 3<title>DataGridPaging</title>
 4<meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR"/>
 5<meta content="C#" name="CODE_LANGUAGE"/>
 6<meta content="JavaScript" name="vs_defaultClientScript"/>
 7<meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema"/>
 8</head>
 9<body>
10<form id="Form1" method="post" runat="server">
11<table align="center" border="1" cellpadding="1" cellspacing="1" id="Table1" style="FONT-SIZE: 9pt" width="450">
12<tr>
13<td><asp:datagrid allowpaging="True" id="DataGrid1" pagesize="5" runat="server" width="100%">
14<headerstyle font-size="9pt"></headerstyle>
15<footerstyle font-size="9pt"></footerstyle>
16<pagerstyle font-size="9pt" mode="NumericPages" visible="False"></pagerstyle>
17</asp:datagrid></td>
18</tr>
19</table>
20<table align="center" border="1" cellpadding="1" cellspacing="1" id="Table2" style="FONT-SIZE: 9pt" width="450">
21<tr>
22<td style="WIDTH: 207px">
23<asp:linkbutton commandname="First" id="LBtnFirst" runat="server"> 首页  </asp:linkbutton>
24<asp:linkbutton commandname="Prev" id="LBtnPrev" runat="server"> 上一页  </asp:linkbutton>
25<asp:linkbutton commandname="Next" id="LBtnNext" runat="server"> 下一页  </asp:linkbutton>
26<asp:linkbutton commandname="Last" id="LBtnLast" runat="server"> 尾页  </asp:linkbutton> </td>
27<td> 第 
28
29<asp:literal id="LtlPageIndex" runat="server"></asp:literal> 页 共 
30
31<asp:literal id="LtlPageCount" runat="server"></asp:literal> 页 每页 
32
33<asp:literal id="LtlPageSize" runat="server"></asp:literal> 条 共 
34
35<asp:literal id="LtlRecordCount" runat="server"></asp:literal> 条 
36
37</td>
38</tr>
39</table>
40</form>
41</body>
42</html>

后台 cs 文件代码, DataGridPaging 类从 System.Web.UI.Page 继承,在数据绑定时需要注意没有数据的情况( 0 页时),以及当页数减少时避免前台正在反页导致缺页。

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 DataGridPaging : System.Web.UI.Page

{

private static string connString = ConfigurationSettings.AppSettings["ConnString"];

private int recordCount;

private int pageCount;

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();

recordCount = ds.Tables[0].Rows.Count;

// 获取当前的页数

pageCount = ( int )Math.Ceiling( recordCount * 1.0 / PageSize);

// 避免纪录从有到无时,并且已经进行过反页的情况下 CurrentPageIndex > PageCount 出错

if (recordCount ==0)

{

this .DataGrid1.CurrentPageIndex = 0;

}

else if ( this .DataGrid1.CurrentPageIndex >= pageCount)

{

this .DataGrid1.CurrentPageIndex = pageCount - 1;

}

this .DataGrid1.DataSource = ds;

this .DataGrid1.DataBind();

NavigationStateChange();

}

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

override protected void OnInit(EventArgs e)

{

//

// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。

//

InitializeComponent();

base .OnInit(e);

}

///

1<summary>
2
3///  设计器支持所需的方法  \-  不要使用代码编辑器修改 
4
5///  此方法的内容。 
6
7///  </summary>

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

<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-layout-grid

Published At
Categories with Web编程
Tagged with
comments powered by Disqus