DataGrid提供了分页功能,不过看上去功能有限,但是我们可以通过DataGrid的一些属性来获取状态以及增加首页、尾页功能按钮。这里没有使用DataGrid的自定义分页功能,如果在速度效率不是很讲究的情况下,由DataGrid自己管理分页还是不错的,付出的代价就是要把整个相关数据取出来后再删选指定页的数据。好处就是开发速度快,不需要写分页的存储过程。本文事例使用的是Sql Server中的Northwind数据库。运行界面如下:
<o:p>对于前台的显示界面,我放了一个DataGrid;四个LinkButton导向按钮;四个Literal来显示纪录状态。<o:p></o:p></o:p>
剩下的就是用表格定位。<o:p></o:p>
这里需要设置DataGrid的AllowPaging属性为True,同时设置AllowCustomPaging属性位false(默认为false),设置PagerStyle的Visible属性为False,使前台不显示。<o:p></o:p>
前台的代码如下:<o:p></o:p>
<%@ Page language="c#" Codebehind="DataGridPaging.aspx.cs" AutoEventWireup="false" Inherits="ZZ.AspnetPaging.DataGridPaging" %><o:p></o:p>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" ><o:p></o:p>
<HTML><o:p></o:p>
<HEAD><o:p></o:p>
<title>DataGridPaging</title><o:p></o:p>
<meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR"><o:p></o:p>
<meta content="C#" name="CODE_LANGUAGE"><o:p></o:p>
<meta content="JavaScript" name="vs_defaultClientScript"><o:p></o:p>
<meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema"><o:p></o:p>
</HEAD><o:p></o:p>
<body><o:p></o:p>
<form id="Form1" method="post" runat="server"><o:p></o:p>
<TABLE id="Table1" style="FONT-SIZE: 9pt" cellSpacing="1" cellPadding="1" width="450" align="center"<o:p></o:p>
border="1"><o:p></o:p>
<TR><o:p></o:p>
<TD><asp:datagrid id="DataGrid1" runat="server" PageSize="5" Width="100%" AllowPaging="True"><o:p></o:p>
<HeaderStyle Font-Size="9pt"></HeaderStyle><o:p></o:p>
<FooterStyle Font-Size="9pt"></FooterStyle><o:p></o:p>
<PagerStyle Visible="False" Font-Size="9pt" Mode="NumericPages"></PagerStyle><o:p></o:p>
</asp:datagrid></TD><o:p></o:p>
</TR><o:p></o:p>
</TABLE><o:p></o:p>
<TABLE id="Table2" style="FONT-SIZE: 9pt" cellSpacing="1" cellPadding="1" width="450" align="center"<o:p></o:p>
border="1"><o:p></o:p>
<TR><o:p></o:p>
<TD style="WIDTH: 207px"><o:p></o:p>
<asp:linkbutton id="LBtnFirst" runat="server" CommandName="First">首页</asp:linkbutton> <o:p></o:p>
<asp:linkbutton id="LBtnPrev" runat="server" CommandName="Prev">上一页</asp:linkbutton> <o:p></o:p>
<asp:linkbutton id="LBtnNext" runat="server" CommandName="Next">下一页</asp:linkbutton><o:p></o:p>
<asp:linkbutton id="LBtnLast" runat="server" CommandName="Last">尾页</asp:linkbutton> </TD><o:p></o:p>
<TD>第<o:p></o:p>
<asp:literal id="LtlPageIndex" runat="server"></asp:literal>页 共 <o:p></o:p>
<asp:literal id="LtlPageCount" runat="server"></asp:literal>页 每页<o:p></o:p>
<asp:literal id="LtlPageSize" runat="server"></asp:literal>条 共<o:p></o:p>
<asp:literal id="LtlRecordCount" runat="server"></asp:literal>条<o:p></o:p>
</TD><o:p></o:p>
</TR><o:p></o:p>
</TABLE><o:p></o:p>
</form><o:p></o:p>
</body><o:p></o:p>
</HTML><o:p></o:p>
后台cs文件代码,DataGridPaging类从System.Web.UI.Page继承,在数据绑定时需要注意没有数据的情况(0页时),以及当页数减少时避免前台正在反页导致缺页。<o:p></o:p>
using System;<o:p></o:p>
using System.Collections;<o:p></o:p>
using System.ComponentModel;<o:p></o:p>
using System.Data;<o:p></o:p>
using System.Drawing;<o:p></o:p>
using System.Web;<o:p></o:p>
using System.Web.SessionState;<o:p></o:p>
using System.Web.UI;<o:p></o:p>
using System.Web.UI.WebControls;<o:p></o:p>
using System.Web.UI.HtmlControls;<o:p></o:p>
using System.Data.SqlClient;<o:p></o:p>
using System.Configuration;<o:p></o:p>
<o:p> </o:p>
namespace ZZ.AspnetPaging<o:p></o:p>
{<o:p></o:p>
public class DataGridPaging : System.Web.UI.Page<o:p></o:p>
{<o:p></o:p>
private static string connString = ConfigurationSettings.AppSettings["ConnString"];<o:p></o:p>
private int recordCount;<o:p></o:p>
private int pageCount;<o:p></o:p>
<o:p> </o:p>
protected System.Web.UI.WebControls.LinkButton LBtnFirst;<o:p></o:p>
protected System.Web.UI.WebControls.LinkButton LBtnPrev;<o:p></o:p>
protected System.Web.UI.WebControls.LinkButton LBtnNext;<o:p></o:p>
protected System.Web.UI.WebControls.LinkButton LBtnLast;<o:p></o:p>
protected System.Web.UI.WebControls.Literal LtlPageIndex;<o:p></o:p>
protected System.Web.UI.WebControls.Literal LtlPageCount;<o:p></o:p>
protected System.Web.UI.WebControls.Literal LtlPageSize;<o:p></o:p>
protected System.Web.UI.WebControls.Literal LtlRecordCount;<o:p></o:p>
protected System.Web.UI.WebControls.DataGrid DataGrid1;<o:p></o:p>
<o:p></o:p>
private void Page_Load(object sender, System.EventArgs e)<o:p></o:p>
{<o:p></o:p>
if(!Page.IsPostBack)<o:p></o:p>
{<o:p></o:p>
DataGridDataBind();<o:p></o:p>
}<o:p></o:p>
}<o:p></o:p>
<o:p> </o:p>
//绑定数据<o:p></o:p>
private void DataGridDataBind()<o:p></o:p>
{<o:p></o:p>
DataSet ds = GetCustomersData();<o:p></o:p>
recordCount = ds.Tables[0].Rows.Count;<o:p></o:p>
//获取当前的页数<o:p></o:p>
pageCount = (int)Math.Ceiling( recordCount * 1.0 / PageSize);<o:p></o:p>
//避免纪录从有到无时,并且已经进行过反页的情况下CurrentPageIndex > PageCount出错<o:p></o:p>
if(recordCount ==0)<o:p></o:p>
{<o:p></o:p>
this.DataGrid1.CurrentPageIndex = 0;<o:p></o:p>
}<o:p></o:p>
else if(this.DataGrid1.CurrentPageIndex >= pageCount)<o:p></o:p>
{<o:p></o:p>
this.DataGrid1.CurrentPageIndex = pageCount - 1;<o:p></o:p>
}<o:p></o:p>
this.DataGrid1.DataSource = ds;<o:p></o:p>
this.DataGrid1.DataBind();<o:p></o:p>
NavigationStateChange();<o:p></o:p>
}<o:p></o:p>
<o:p> </o:p>
#region Web 窗体设计器生成的代码<o:p></o:p>
override protected void OnInit(EventArgs e)<o:p></o:p>
{<o:p></o:p>
//<o:p></o:p>
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。<o:p></o:p>
//<o:p></o:p>
InitializeComponent();<o:p></o:p>
base.OnInit(e);<o:p></o:p>
}<o:p></o:p>
<o:p></o:p>
/// <summary><o:p></o:p>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改<o:p></o:p>
/// 此方法的内容。<o:p></o:p>
/// </summary><o:p></o:p>
private void InitializeComponent()<o:p></o:p>
{ <o:p></o:p>
this.LBtnFirst.Click += new System.EventHandler(this.LBtnNavigation_Click);<o:p></o:p>
this.LBtnPrev.Click += new System.EventHandler(this.LBtnNavigation_Click);<o:p></o:p>
this.LBtnNext.Click += new System.EventHandler(this.LBtnNavigation_Click);<o:p></o:p>
this.LBtnLast.Click += new System.EventHandler(this.LBtnNavigation_Click);<o:p></o:p>
this.Load += new System.EventHandler(this.Page_Load);<o:p></o:p>
<o:p> </o:p>
}<o:p></o:p>
#endregion<o:p></o:p>
<o:p> </o:p>
private void LBtnNavigation_Click(object sender, System.EventArgs e)<o:p></o:p>
{<o:p></o:p>
LinkButton btn = (LinkButton)sender;<o:p></o:p>
switch(btn.CommandName)<o:p></o:p>
{<o:p></o:p>
case "First":<o:p></o:p>
PageIndex = 0;<o:p></o:p>
break;<o:p></o:p>
case "Prev"://if( PageIndex > 0 )<o:p></o:p>
PageIndex = PageIndex - 1;<o:p></o:p>
break;<o:p></o:p>
case "Next"://if( PageIndex < PageCount -1)<o:p></o:p>
PageIndex = PageIndex + 1;<o:p></o:p>