DataGridPager.aspx
<%
@ Page language
=
"
c#
"
Codebehind
=
"
DataGridPager.aspx.cs
"
AutoEventWireup
=
"
false
"
Inherits
=
"
CommonFunction.DataGridPager
"
%>
<! DOCTYPE HTML PUBLIC " -//W3C//DTD HTML 4.0 Transitional//EN " >
< HTML >
< HEAD >
< meta content = " Visual Basic 7.0 " name = " CODE_LANGUAGE " >
< meta content = " JavaScript " name = " vs_defaultClientScript " >
< meta content = " http://schemas.microsoft.com/intellisense/ie5 " name = " vs_targetSchema " >
</ HEAD >
< body MS_POSITIONING = " GridLayout " >
< form id = " Form1 " runat = " server " >
< TABLE id = " Table1 " style = " Z-INDEX: 101; LEFT: 32px; WIDTH: 480px; POSITION: absolute; TOP: 24px; HEIGHT: 279px "
cellSpacing = " 1 " cellPadding = " 1 " width = " 480 " border = " 0 " >
< tr >
< td align = " center " >
< h2 > DataGrid分页的例子 </ h2 >
</ td >
</ tr >
< TR >
< TD style = " HEIGHT: 206px " vAlign = " top " >< asp:datagrid id = " DataGridPage " runat = " server " GridLines = " Horizontal " BackColor = " White " BorderStyle = " None "
Width = " 480px " AllowPaging = " True " PageSize = " 5 " PagerStyle - Mode = " NumericPages " PagerStyle - HorizontalAlign = " Right " BorderColor = " #E7E7FF "
BorderWidth = " 1px " CellPadding = " 3 " Font - Name = " Verdana " HeaderStyle - BackColor = " #aaaadd " AlternatingItemStyle - BackColor = " #eeeeee "
HorizontalAlign = " Center " AutoGenerateColumns = " False " >
< SelectedItemStyle Font - Bold = " True " ForeColor = " #F7F7F7 " BackColor = " #738A9C " ></ SelectedItemStyle >
< AlternatingItemStyle BackColor = " #F7F7F7 " ></ AlternatingItemStyle >
< ItemStyle ForeColor = " #4A3C8C " BackColor = " #E7E7FF " ></ ItemStyle >
< HeaderStyle Font - Bold = " True " HorizontalAlign = " Center " ForeColor = " #F7F7F7 " BackColor = " #4A3C8C " ></ HeaderStyle >
< FooterStyle ForeColor = " #4A3C8C " BackColor = " #B5C7DE " ></ FooterStyle >
< Columns >
< asp:BoundColumn DataField = " lastname " HeaderText = " lastname " >
< HeaderStyle Width = " 480px " ></ HeaderStyle >
</ asp:BoundColumn >
< asp:BoundColumn DataField = " firstname " HeaderText = " firstname " ></ asp:BoundColumn >
</ Columns >
< PagerStyle HorizontalAlign = " Right " ForeColor = " #4A3C8C " BackColor = " #E7E7FF " Mode = " NumericPages " ></ PagerStyle >
</ asp:datagrid ></ TD >
</ TR >
< TR >
< TD align = " right " >< asp:label id = " lblPageCount " runat = " server " ></ asp:label >< asp:label id = " lblCurrentIndex " runat = " server " ></ asp:label >< asp:linkbutton id = " btnFirst " onclick = " PageButtonClick " runat = " server " Font - Name = " verdana " CommandArgument = " 0 " > 最首页 </ asp:linkbutton >< asp:linkbutton id = " btnPrev " onclick = " PageButtonClick " runat = " server " CommandArgument = " prev " > 前一页 </ asp:linkbutton >< asp:linkbutton id = " btnNext " onclick = " PageButtonClick " runat = " server " CommandArgument = " next " > 下一页 </ asp:linkbutton >< asp:linkbutton id = " btnLast " onclick = " PageButtonClick " runat = " server " CommandArgument = " last " > 最后页 </ asp:linkbutton ></ TD >
</ TR >
</ TABLE >
</ form >
</ body >
</ HTML >
<! DOCTYPE HTML PUBLIC " -//W3C//DTD HTML 4.0 Transitional//EN " >
< HTML >
< HEAD >
< meta content = " Visual Basic 7.0 " name = " CODE_LANGUAGE " >
< meta content = " JavaScript " name = " vs_defaultClientScript " >
< meta content = " http://schemas.microsoft.com/intellisense/ie5 " name = " vs_targetSchema " >
</ HEAD >
< body MS_POSITIONING = " GridLayout " >
< form id = " Form1 " runat = " server " >
< TABLE id = " Table1 " style = " Z-INDEX: 101; LEFT: 32px; WIDTH: 480px; POSITION: absolute; TOP: 24px; HEIGHT: 279px "
cellSpacing = " 1 " cellPadding = " 1 " width = " 480 " border = " 0 " >
< tr >
< td align = " center " >
< h2 > DataGrid分页的例子 </ h2 >
</ td >
</ tr >
< TR >
< TD style = " HEIGHT: 206px " vAlign = " top " >< asp:datagrid id = " DataGridPage " runat = " server " GridLines = " Horizontal " BackColor = " White " BorderStyle = " None "
Width = " 480px " AllowPaging = " True " PageSize = " 5 " PagerStyle - Mode = " NumericPages " PagerStyle - HorizontalAlign = " Right " BorderColor = " #E7E7FF "
BorderWidth = " 1px " CellPadding = " 3 " Font - Name = " Verdana " HeaderStyle - BackColor = " #aaaadd " AlternatingItemStyle - BackColor = " #eeeeee "
HorizontalAlign = " Center " AutoGenerateColumns = " False " >
< SelectedItemStyle Font - Bold = " True " ForeColor = " #F7F7F7 " BackColor = " #738A9C " ></ SelectedItemStyle >
< AlternatingItemStyle BackColor = " #F7F7F7 " ></ AlternatingItemStyle >
< ItemStyle ForeColor = " #4A3C8C " BackColor = " #E7E7FF " ></ ItemStyle >
< HeaderStyle Font - Bold = " True " HorizontalAlign = " Center " ForeColor = " #F7F7F7 " BackColor = " #4A3C8C " ></ HeaderStyle >
< FooterStyle ForeColor = " #4A3C8C " BackColor = " #B5C7DE " ></ FooterStyle >
< Columns >
< asp:BoundColumn DataField = " lastname " HeaderText = " lastname " >
< HeaderStyle Width = " 480px " ></ HeaderStyle >
</ asp:BoundColumn >
< asp:BoundColumn DataField = " firstname " HeaderText = " firstname " ></ asp:BoundColumn >
</ Columns >
< PagerStyle HorizontalAlign = " Right " ForeColor = " #4A3C8C " BackColor = " #E7E7FF " Mode = " NumericPages " ></ PagerStyle >
</ asp:datagrid ></ TD >
</ TR >
< TR >
< TD align = " right " >< asp:label id = " lblPageCount " runat = " server " ></ asp:label >< asp:label id = " lblCurrentIndex " runat = " server " ></ asp:label >< asp:linkbutton id = " btnFirst " onclick = " PageButtonClick " runat = " server " Font - Name = " verdana " CommandArgument = " 0 " > 最首页 </ asp:linkbutton >< asp:linkbutton id = " btnPrev " onclick = " PageButtonClick " runat = " server " CommandArgument = " prev " > 前一页 </ asp:linkbutton >< asp:linkbutton id = " btnNext " onclick = " PageButtonClick " runat = " server " CommandArgument = " next " > 下一页 </ asp:linkbutton >< asp:linkbutton id = " btnLast " onclick = " PageButtonClick " runat = " server " CommandArgument = " last " > 最后页 </ asp:linkbutton ></ TD >
</ TR >
</ TABLE >
</ form >
</ body >
</ HTML >
DataGridPager.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 CommonFunction
... {
/**//// <summary>
/// testDataGridPager 的摘要说明。
/// </summary>
public class DataGridPager : System.Web.UI.Page
...{
protected System.Web.UI.WebControls.Label lblPageCount;
protected System.Web.UI.WebControls.Label lblCurrentIndex;
protected System.Web.UI.WebControls.LinkButton btnFirst;
protected System.Web.UI.WebControls.LinkButton btnPrev;
protected System.Web.UI.WebControls.LinkButton btnNext;
protected System.Web.UI.WebControls.DataGrid DataGridPage;
protected System.Web.UI.WebControls.LinkButton btnLast;
private void Page_Load(object sender, System.EventArgs e)
...{
//页面初试化时进行数据绑定
if(!IsPostBack)
BindGrid();
}
//显示当前分页信息
private void ShowStats()
...{
//显示当前页面是第几页
lblCurrentIndex.Text = "第 " + (DataGridPage.CurrentPageIndex + 1).ToString() + " 页";
//显示总页数
lblPageCount.Text = "总共 " + DataGridPage.PageCount.ToString() + " 页";
}
Web 窗体设计器生成的代码#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
...{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/**//// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
...{
this.DataGridPage.PageIndexChanged += new System.Web.UI.WebControls.DataGridPageChangedEventHandler(this.DataGridPage_Page);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
//分别处理”最首页“、“前一页”、“下一页”和“最后页”四个按钮单击时设置DataGrid控件的当前页的索引
public void PageButtonClick(object sender, EventArgs e)
...{
//取得按钮单击时传递的命令参数
string arg = ((LinkButton)sender).CommandArgument.ToString();
switch(arg)
...{
//如果点击的是“下一页”
case "next":
//如果当前页不是最后一页
if (DataGridPage.CurrentPageIndex < (DataGridPage.PageCount - 1))
...{
//设置DataGrid控件的当前页索引为下一页面
DataGridPage.CurrentPageIndex += 1;
}
break;
//如果点击的是“前一页”
case "prev":
//如果当前页不是首页
if (DataGridPage.CurrentPageIndex > 0)
...{
//设置DataGrid控件的当前页索引为上一页面
DataGridPage.CurrentPageIndex -= 1;
}
break;
//如果点击的是“最后页”
case "last":
//设置当前页的索引为最后一页
DataGridPage.CurrentPageIndex = (DataGridPage.PageCount - 1);
break;
//默认为”最首页“
default:
//设置当前页的索引为首页
DataGridPage.CurrentPageIndex = System.Convert.ToInt32(arg);
break;
}
BindGrid();
ShowStats();
}
private void BindGrid()
...{
//定义数据连接对象,其中数据库连接字符串是在Web.Config文件中定义的
SqlConnection cnn = new SqlConnection(ConfigurationSettings.AppSettings["ConnectionSqlServer"].ToString());
//创建数据适配器对象
SqlDataAdapter da = new SqlDataAdapter("select employeeid,lastname,firstname from employees",cnn);
//创建DataSet对象
DataSet ds = new DataSet();
try
...{
//填充数据集
da.Fill(ds, "testTable");
//进行数据绑定
DataGridPage.DataSource = ds;
DataGridPage.DataBind();
}
catch(Exception error)
...{
Response.Write(error.ToString());
}
}
public void DataGridPage_Page(object sender, DataGridPageChangedEventArgs e)
...{
//设置DataGrid当前页的索引值为用户选择的页的索引
DataGridPage.CurrentPageIndex = e.NewPageIndex;
//重新绑定数据
BindGrid();
//显示当前分页信息
ShowStats();
}
}
}
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 CommonFunction
... {
/**//// <summary>
/// testDataGridPager 的摘要说明。
/// </summary>
public class DataGridPager : System.Web.UI.Page
...{
protected System.Web.UI.WebControls.Label lblPageCount;
protected System.Web.UI.WebControls.Label lblCurrentIndex;
protected System.Web.UI.WebControls.LinkButton btnFirst;
protected System.Web.UI.WebControls.LinkButton btnPrev;
protected System.Web.UI.WebControls.LinkButton btnNext;
protected System.Web.UI.WebControls.DataGrid DataGridPage;
protected System.Web.UI.WebControls.LinkButton btnLast;
private void Page_Load(object sender, System.EventArgs e)
...{
//页面初试化时进行数据绑定
if(!IsPostBack)
BindGrid();
}
//显示当前分页信息
private void ShowStats()
...{
//显示当前页面是第几页
lblCurrentIndex.Text = "第 " + (DataGridPage.CurrentPageIndex + 1).ToString() + " 页";
//显示总页数
lblPageCount.Text = "总共 " + DataGridPage.PageCount.ToString() + " 页";
}
Web 窗体设计器生成的代码#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
...{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/**//// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
...{
this.DataGridPage.PageIndexChanged += new System.Web.UI.WebControls.DataGridPageChangedEventHandler(this.DataGridPage_Page);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
//分别处理”最首页“、“前一页”、“下一页”和“最后页”四个按钮单击时设置DataGrid控件的当前页的索引
public void PageButtonClick(object sender, EventArgs e)
...{
//取得按钮单击时传递的命令参数
string arg = ((LinkButton)sender).CommandArgument.ToString();
switch(arg)
...{
//如果点击的是“下一页”
case "next":
//如果当前页不是最后一页
if (DataGridPage.CurrentPageIndex < (DataGridPage.PageCount - 1))
...{
//设置DataGrid控件的当前页索引为下一页面
DataGridPage.CurrentPageIndex += 1;
}
break;
//如果点击的是“前一页”
case "prev":
//如果当前页不是首页
if (DataGridPage.CurrentPageIndex > 0)
...{
//设置DataGrid控件的当前页索引为上一页面
DataGridPage.CurrentPageIndex -= 1;
}
break;
//如果点击的是“最后页”
case "last":
//设置当前页的索引为最后一页
DataGridPage.CurrentPageIndex = (DataGridPage.PageCount - 1);
break;
//默认为”最首页“
default:
//设置当前页的索引为首页
DataGridPage.CurrentPageIndex = System.Convert.ToInt32(arg);
break;
}
BindGrid();
ShowStats();
}
private void BindGrid()
...{
//定义数据连接对象,其中数据库连接字符串是在Web.Config文件中定义的
SqlConnection cnn = new SqlConnection(ConfigurationSettings.AppSettings["ConnectionSqlServer"].ToString());
//创建数据适配器对象
SqlDataAdapter da = new SqlDataAdapter("select employeeid,lastname,firstname from employees",cnn);
//创建DataSet对象
DataSet ds = new DataSet();
try
...{
//填充数据集
da.Fill(ds, "testTable");
//进行数据绑定
DataGridPage.DataSource = ds;
DataGridPage.DataBind();
}
catch(Exception error)
...{
Response.Write(error.ToString());
}
}
public void DataGridPage_Page(object sender, DataGridPageChangedEventArgs e)
...{
//设置DataGrid当前页的索引值为用户选择的页的索引
DataGridPage.CurrentPageIndex = e.NewPageIndex;
//重新绑定数据
BindGrid();
//显示当前分页信息
ShowStats();
}
}
}