方法一:
datagridpaging.aspx
<%@ page language=”c#” enableviewstate = “true” codebehind=”datagridpaging.aspx.cs”
autoeventwireup=”false” inherits=”emeng.exam.datagridpaging.datagridpaging” %>
<!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”>
<asp:datagrid id=”mydatagrid” runat=”server” autogeneratecolumns=”false”
horizontalalign=”center” alternatingitemstyle-backcolor=”#eeeeee”
headerstyle-backcolor=”#aaaadd” font-size=”8pt” font-name=”verdana”
cellspacing=”0″ cellpadding=”3″ gridlines=”both” borderwidth=”1″
bordercolor=”black” onpageindexchanged=”mydatagrid_page” pagerstyle-horizontalalign=”right”
pagerstyle-mode=”numericpages” pagesize=”5″ allowpaging=”true”>
<alternatingitemstyle backcolor=”#eeeeee”></alternatingitemstyle>
<headerstyle backcolor=”#aaaadd” font-bold=”true” horizontalalign=”center”></headerstyle>
<pagerstyle horizontalalign=”right” mode=”numericpages”></pagerstyle>
<columns>
<asp:boundcolumn headertext=”标题” datafield=”title” headerstyle-width=”480px”>
</asp:boundcolumn>
<asp:boundcolumn headertext=”发表日期” datafield=”createdate” dataformatstring=”{0:yyyy-mm-dd hh:mm:ss}”>
</asp:boundcolumn>
</columns>
</asp:datagrid>
<p style=”font-size:9pt” align=”center”>
<asp:label id=”lblpagecount” runat=”server”></asp:label>
<asp:label id=”lblcurrentindex” runat=”server”></asp:label>
<asp:linkbutton id=”btnfirst” onclick=”pagerbuttonclick” runat=”server” font-name=”verdana”
font-size=”8pt” forecolor=”navy” commandargument=”0″></asp:linkbutton>
<asp:linkbutton id=”btnprev” onclick=”pagerbuttonclick” runat=”server” font-name=”verdana”
font-size=”8pt” forecolor=”navy” commandargument=”prev”></asp:linkbutton>
<asp:linkbutton id=”btnnext” onclick=”pagerbuttonclick” runat=”server” font-name=”verdana”
font-size=”8pt” forecolor=”navy” commandargument=”next”></asp:linkbutton>
<asp:linkbutton id=”btnlast” onclick=”pagerbuttonclick” runat=”server” font-name=”verdana”
font-size=”8pt” forecolor=”navy” commandargument=”last”></asp:linkbutton>
</p>
</form>
</body>
</html>
datagridpaging.aspx.cs
using system;
using system.collections;
using system.componentmodel;
using system.data;
using system.data.oledb;
using system.drawing;
using system.web;
using system.web.sessionstate;
using system.web.ui;
using system.web.ui.webcontrols;
using system.web.ui.htmlcontrols;
namespace emeng.exam.datagridpaging
{
/// <summary>
/// datagridpaging 的摘要说明。
/// </summary>
public class datagridpaging : system.web.ui.page
{
protected system.web.ui.webcontrols.datagrid mydatagrid;
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.linkbutton btnlast;
private oledbconnection cn = new oledbconnection();
private void page_load(object sender, system.eventargs e)
{
// 在此处放置用户代码以初始化页面
btnfirst.text = “最首页”;
btnprev.text = “前一页”;
btnnext.text = “下一页”;
btnlast.text = “最后页”;
opendatabase();
bindgrid();
}
private void opendatabase()
{
cn.connectionstring = “provider=microsoft.jet.oledb.4.0;data source=” + server.mappath(“xxxx.mdb”);
cn.open();
}
private void showstats()
{
lblcurrentindex.text = “第 ” + (mydatagrid.currentpageindex + 1).tostring() + ” 页”;
lblpagecount.text = “总共 ” + mydatagrid.pagecount.tostring() + ” 页”;
}
public void pagerbuttonclick(object sender, eventargs e)
{
string arg = ((linkbutton)sender).commandargument.tostring();
switch(arg)
{
case “next”:
if (mydatagrid.currentpageindex < (mydatagrid.pagecount – 1))
{
mydatagrid.currentpageindex += 1;
}
break;
case “prev”:
if (mydatagrid.currentpageindex > 0)
{
mydatagrid.currentpageindex -= 1;
}
break;
case “last”:
mydatagrid.currentpageindex = (mydatagrid.pagecount – 1);
break;
default:
mydatagrid.currentpageindex = system.convert.toint32(arg);
break;
}
bindgrid();
showstats();
}
public void bindgrid()
{
oledbconnection myconnection = cn;
dataset ds = new dataset();
oledbdataadapter adapter = new oledbdataadapter(“select title,createdate from document”, myconnection);
adapter.fill(ds, “document”);
mydatagrid.datasource = ds.tables[“document”].defaultview;
mydatagrid.databind();
showstats();
}
public void mydatagrid_page(object sender, datagridpagechangedeventargs e)
{
int startindex ;
startindex = mydatagrid.currentpageindex * mydatagrid.pagesize;
mydatagrid.currentpageindex = e.newpageindex;
bindgrid();
showstats();
}
#region web form designer generated code
override protected void oninit(eventargs e)
{
//
// codegen:该调用是 asp.net web 窗体设计器所必需的。
//
initializecomponent();
base.oninit(e);
}
/// <summary>
/// 设计器支持所需的方法 – 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void initializecomponent()
{
this.load += new system.eventhandler(this.page_load);
}
#endregion
}
}
方法二:
datagridpaging2.aspx
<%@ page language=”c#” enableviewstate = “true” codebehind=”datagridpaging2.aspx.cs” autoeventwireup=”false”
inherits=”emeng.exam.datagridpaging2.datagridpaging2″ %>
<!doctype html public “-//w3c//dtd html 4.0 transitional//en”>
<html>
<head>
<title>功能齐全的分页的例子【2】</title>
<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”>
<asp:datagrid id=”mydatagrid” runat=”server” autogeneratecolumns=”false” horizontalalign=”center”
alternatingitemstyle-backcolor=”#eeeeee” headerstyle-backcolor=”#aaaadd” font-size=”8pt”
font-name=”verdana” cellspacing=”0″ cellpadding=”3″ gridlines=”both” borderwidth=”1″
bordercolor=”black” pagerstyle-horizontalalign=”right” pagerstyle-mode=”numericpages”
pagesize=”5″ allowpaging=”true”>
<alternatingitemstyle backcolor=”#eeeeee”></alternatingitemstyle>
<headerstyle backcolor=”#aaaadd” font-bold=”true” horizontalalign=”center”></headerstyle>
<pagerstyle horizontalalign=”right” mode=”numericpages”></pagerstyle>
<columns>
<asp:boundcolumn headertext=”标题” datafield=”title” headerstyle-width=”480px”></asp:boundcolumn>
<asp:boundcolumn headertext=”发表日期” datafield=”createdate”
dataformatstring=”{0:yyyy-mm-dd hh:mm:ss}”></asp:boundcolumn>
</columns>
</asp:datagrid>
<p align=”center”>
<asp:label id=”lblpagecount” runat=”server” font-size=”9pt” font-bold=”true”></asp:label>
<asp:label id=”lblcurrentindex” runat=”server” font-size=”9pt” font-bold=”true”></asp:label>
<asp:linkbutton id=”btnfirst” runat=”server” font-size=”9pt” forecolor=”navy” commandargument=”0″/>
<asp:linkbutton id=”btnprev” runat=”server” font-size=”9pt” forecolor=”navy” commandargument=”prev”/>
<asp:linkbutton id=”btnnext” runat=”server” font-size=”9pt” forecolor=”navy” commandargument=”next”/>
<asp:linkbutton id=”btnlast” runat=”server” font-size=”9pt” forecolor=”navy” commandargument=”last”/>
</p>
</form>
</body>
</html>
datagridpaging2.aspx.cs
using system;
using system.collections;
using system.componentmodel;
using system.data;
using system.data.oledb;
using system.drawing;
using system.web;
using system.web.sessionstate;
using system.web.ui;
using system.web.ui.webcontrols;
using system.web.ui.htmlcontrols;
namespace emeng.exam.datagridpaging2
{
/// <summary>
/// datagridpaging 的摘要说明。
/// </summary>
public class datagridpaging2 : system.web.ui.page
{
protected system.web.ui.webcontrols.datagrid mydatagrid;
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.linkbutton btnlast;
private oledbconnection cn = new oledbconnection();
private void page_load(object sender, system.eventargs e)
{
// 在此处放置用户代码以初始化页面
btnfirst.text = “最首页”;
btnprev.text = “前一页”;
btnnext.text = “下一页”;
btnlast.text = “最后页”;
opendatabase();
bindgrid();
}
private void opendatabase()
{
cn.connectionstring = “provider=microsoft.jet.oledb.4.0;data source=”
+ httpcontext.current.server.mappath(“../../aspxweb.mdb.ascx”);
cn.open();
}
private void showstats()
{
lblcurrentindex.text = “第 ” + (mydatagrid.currentpageindex + 1).tostring() + ” 页”;
lblpagecount.text = “总共 ” + mydatagrid.pagecount.tostring() + ” 页”;
}
public void pagerbuttonclick(object sender, eventargs e)
{
string arg = ((linkbutton)sender).commandargument.tostring();
switch(arg)
{
case “next”:
if (mydatagrid.currentpageindex < (mydatagrid.pagecount – 1))
{
mydatagrid.currentpageindex += 1;
}
break;
case “prev”:
if (mydatagrid.currentpageindex > 0)
{
mydatagrid.currentpageindex -= 1;
}
break;
case “last”:
mydatagrid.currentpageindex = (mydatagrid.pagecount – 1);
break;
default:
mydatagrid.currentpageindex = system.convert.toint32(arg);
break;
}
bindgrid();
showstats();
}
public void bindgrid()
{
oledbconnection myconnection = cn;
dataset ds = new dataset();
oledbdataadapter adapter = new oledbdataadapter(“select title,createdate from document”, myconnection);
adapter.fill(ds, “document”);
mydatagrid.datasource = ds.tables[“document”].defaultview;
mydatagrid.databind();
showstats();
}
#region web form designer generated code
override protected void oninit(eventargs e)
{
//
// codegen:该调用是 asp.net web 窗体设计器所必需的。
//
initializecomponent();
base.oninit(e);
}
/// <summary>
/// 设计器支持所需的方法 – 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void initializecomponent()
{
this.mydatagrid.pageindexchanged += new system.web.ui.webcontrols.datagridpagechangedeventhandler(this.onpageindexchanged);
this.btnfirst.click += new system.eventhandler(this.pagerbuttonclick);
this.btnprev.click += new system.eventhandler(this.pagerbuttonclick);
this.btnnext.click += new system.eventhandler(this.pagerbuttonclick);
this.btnlast.click += new system.eventhandler(this.pagerbuttonclick);
this.load += new system.eventhandler(this.page_load);
}
#endregion
private void onpageindexchanged(object source, system.web.ui.webcontrols.datagridpagechangedeventargs e)
{
int startindex ;
startindex = mydatagrid.currentpageindex * mydatagrid.pagesize;
mydatagrid.currentpageindex = e.newpageindex;
bindgrid();
showstats();
}
}
}