<%@ Page Language="c#" CodeFile="DataGridPaging.aspx.cs" AutoEventWireup="false" Inherits="Jacky.Paging.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" style="font-size: 9pt" 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" BackColor="Silver"></HeaderStyle>
<FooterStyle Font-Size="9pt"></FooterStyle>
<PagerStyle Visible="False" Font-Size="9pt" Mode="NumericPages" BackColor="#8080FF"></PagerStyle>
</asp:DataGrid></td>
</tr>
</table>
<table id="Table2" style="font-size: 9pt" cellspacing="1" cellpadding="1" width="450"
align="center" border="1" bgcolor="#ccccff">
<tr>
<td style="width: 207px">
<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 style="width: 234px">
第
<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>
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
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.Configuration;
namespace Jacky.Paging
{
public partial class DataGridPaging : System.Web.UI.Page
{
private static string connString = ConfigurationSettings.AppSettings["mySQL"];
private int recordCount;
private int pageCount;
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; }
}
}
}