欢迎光临
我们一直在努力

一个功能齐全的DataGrid分页例子-ASP教程,数据库相关

建站超值云服务器,限时71元/月

下面的这个例子提供了datagrid的分页功能。aspx代码和cohe behind代码分别如下,注意更改数据录连接字符串。结果如下:

c#版本 例子

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>&nbsp;

<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>&nbsp;

<asp:linkbutton id="btnprev" onclick="pagerbuttonclick" runat="server" font-name="verdana"

font-size="8pt" forecolor="navy" commandargument="prev"></asp:linkbutton>&nbsp;

<asp:linkbutton id="btnnext" onclick="pagerbuttonclick" runat="server" font-name="verdana"

font-size="8pt" forecolor="navy" commandargument="next"></asp:linkbutton>&nbsp;

<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

}

}

vb.net版本

datagridpaging.aspx

<%@ page language="vb" autoeventwireup="false" codebehind="datagridpaging.aspx.vb" inherits="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" 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"></headerstyle>

<pagerstyle horizontalalign="right" mode="numericpages"></pagerstyle>

</asp:datagrid>

<p style="font-size:9pt">

<asp:label id="lblpagecount" runat="server"></asp:label>&nbsp;

<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>&nbsp;

<asp:linkbutton id="btnprev" onclick="pagerbuttonclick" runat="server"

font-name="verdana" font-size="8pt" forecolor="navy" commandargument="prev">

</asp:linkbutton>&nbsp;

<asp:linkbutton id="btnnext" onclick="pagerbuttonclick" runat="server"

font-name="verdana" font-size="8pt" forecolor="navy" commandargument="next">

</asp:linkbutton>&nbsp;

<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.vb

imports system.data.sqlclient

imports system.data

imports system.web.ui

public class datagridpaging

inherits system.web.ui.page

protected withevents mydatagrid as system.web.ui.webcontrols.datagrid

protected withevents btnfirst as system.web.ui.webcontrols.linkbutton

protected withevents btnprev as system.web.ui.webcontrols.linkbutton

protected withevents btnnext as system.web.ui.webcontrols.linkbutton

protected withevents btnlast as system.web.ui.webcontrols.linkbutton

protected withevents lblcurrentindex as system.web.ui.webcontrols.label

protected withevents lblpagecount as system.web.ui.webcontrols.label

protected withevents datagrid1 as system.web.ui.webcontrols.datagrid

#region " web form designer generated code "

this call is required by the web form designer.

<system.diagnostics.debuggerstepthrough()> private sub initializecomponent()

end sub

private sub page_init(byval sender as system.object,_

byval e as system.eventargs) handles mybase.init

codegen: this method call is required by the web form designer

do not modify it using the code editor.

initializecomponent()

end sub

#end region

private sub page_load(byval sender as system.object,_

byval e as system.eventargs) handles mybase.load

btnfirst.text = "最首页"

btnprev.text = "前一页"

btnnext.text = "下一页"

btnlast.text = "最后页"

opendatabase()

bindgrid()

end sub

sub mydatagrid_page(byval sender as object, byval e as datagridpagechangedeventargs)

dim startindex as integer

startindex = mydatagrid.currentpageindex * mydatagrid.pagesize

mydatagrid.currentpageindex = e.newpageindex

bindgrid()

showstats()

end sub

sub bindgrid()

dim myconnection as sqlconnection = cn

dim ds as dataset = new dataset()

dim adapter as sqldataadapter = new sqldataadapter("select * from orders", myconnection)

adapter.fill(ds, "orders")

mydatagrid.datasource = ds.tables("orders").defaultview

mydatagrid.databind()

showstats()

end sub

sub pagerbuttonclick(byval sender as object, byval e as eventargs)

used by external paging ui

dim arg as string = sender.commandargument

select case arg

case "next"

if (mydatagrid.currentpageindex < (mydatagrid.pagecount – 1)) then

mydatagrid.currentpageindex += 1

end if

case "prev"

if (mydatagrid.currentpageindex > 0) then

mydatagrid.currentpageindex -= 1

end if

case "last"

mydatagrid.currentpageindex = (mydatagrid.pagecount – 1)

case else

page number

mydatagrid.currentpageindex = system.convert.toint32(arg)

end select

bindgrid()

showstats()

end sub

sub showstats()

lblcurrentindex.text = "第 " + (mydatagrid.currentpageindex + 1).tostring() + " 页"

lblpagecount.text = "总共 " + mydatagrid.pagecount.tostring() + " 页"

end sub

public cn as new sqlclient.sqlconnection()

public sub opendatabase()

cn.connectionstring = "server=.;database=northwind;user id=sa;password=;"

cn.open()

end sub

end class

——————————————————————————–

方法2:完全后代码写法:例子

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>&nbsp;

<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"/>&nbsp;

<asp:linkbutton id="btnprev" runat="server" font-size="9pt" forecolor="navy" commandargument="prev"/>&nbsp;

<asp:linkbutton id="btnnext" runat="server" font-size="9pt" forecolor="navy" commandargument="next"/>&nbsp;

<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();

}

}

}

赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » 一个功能齐全的DataGrid分页例子-ASP教程,数据库相关
分享到: 更多 (0)