DataGrid提供了分頁功能,不過看上去功能有限,但是我們可以通過DataGrid的一些屬性來獲取狀態以及增加首頁、尾頁功能按鈕。這裡沒有使用DataGrid的自定義分頁功能,如果在速度效率不是很講究的情況下,由DataGrid自己管理分頁還是不錯的,付出的代價就是要把整個相關數據取出來後再刪選指定頁的數據。好處就是開發速度快,不需要寫分頁的存儲過程。本文事例使用的是SQL Server中的Northwind數據庫。運行界面如下:
對於前台的顯示界面,我放了一個DataGrid;四個LinkButton導向按鈕;四個Literal來顯示紀錄狀態。
剩下的就是用表格定位。
這裡需要設置DataGrid的AllowPaging屬性為True,同時設置AllowCustomPaging屬性位false(默認為false),設置PagerStyle的Visible屬性為False,使前台不顯示。
前台的代碼如下:
<%@ Page language="c#" Codebehind="DataGridPaging.aspx.cs" AutoEventWireup="false" Inherits="ZZ.ASPnetPaging.DataGridPaging" %>
<!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" cellSpacing="1" cellPadding="1" width="450" align="center"
border="1">
<TR>
<TD><ASP:datagrid id="DataGrid1" runat="server" PageSize="5" Width="100%" AllowPaging="True">
<HeaderStyle Font-Size="9pt"></HeaderStyle>
<FooterStyle Font-Size="9pt"></FooterStyle>
<PagerStyle Visible="False" Font-Size="9pt" Mode="NumericPages"></PagerStyle>
; </ASP:datagrid></TD>
</TR>
</TABLE>
<TABLE id="Table2" cellSpacing="1" cellPadding="1" width="450" align="center"
border="1">
<TR>
<TD >
<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>
</form>
</body>
</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);
}
///<summary>
///設計器支持所需的方法 - 不要使用代碼編輯器修改
///此方法的內容。
///</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 )
PageIndex = PageIndex - 1;
break;
case "Next"://if( PageIndex < PageCount -1)
PageIndex = PageIndex + 1;
break;
case "Last":
PageIndex = PageCount - 1;
break;
}
DataGridDataBind();
}
//數據綁定
public static DataSet GetCustomersData()
{
SqlConnection conn = new SqlConnection(connString);
string sqlStr = "SELECT CustomerID, CompanyName,Address,Phone FROM Customers";
SqlCommand comm = new SqlCommand( sqlStr ,conn);
SqlDataAdapter dataAdapter = new SqlDataAdapter(comm);
DataSet ds = new DataSet();
dataAdapter.Fill(ds);
return ds;
}
///<summary>
///控制導航按鈕或數字的狀態
///</summary>
public void NavigationStateChange()
{
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;
}
}
if(RecordCount == 0)//當沒有紀錄時DataGrid.PageCount會顯示1頁
this.LtlPageCount.Text = "0";
else
this.LtlPageCount.Text = PageCount.ToString();
if(RecordCount == 0)
this.LtlPageIndex.Text = "0";
else
this.LtlPageIndex.Text = (PageIndex + 1).ToString();//在有頁數的情況下前台顯示頁數加1
this.LtlPageSize.Text = PageSize.ToString();
this.LtlRecordCount.Text = RecordCount.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;}
set{recordCount = value;}
}
}
}