.NET中使用datagrid实现的简单分页效果
2024-07-10 12:56:08
供稿:网友
<!--前台页面-->
<%@ page language="c#" codebehind="webform1.aspx.cs" autoeventwireup="false" inherits="try1.webform1" %>
<!doctype html public "-//w3c//dtd html 4.0 transitional//en" >
<html>
<head>
<title>webform1</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">
<p><font face="宋体"></font> </p>
<p>
<asp:datagrid id="datagrid1" runat="server" bordercolor="#3366cc" borderstyle="none" borderwidth="1px"
backcolor="white" cellpadding="4" allowpaging="true" pagesize="5" allowsorting="true">
<selecteditemstyle font-bold="true" forecolor="#ccff99" backcolor="#009999"></selecteditemstyle>
<itemstyle forecolor="#003399" backcolor="white"></itemstyle>
<headerstyle font-bold="true" forecolor="#ccccff" backcolor="#003399"></headerstyle>
<footerstyle forecolor="#003399" backcolor="#99cccc"></footerstyle>
<pagerstyle horizontalalign="left" forecolor="#003399" backcolor="#99cccc" pagebuttoncount="6"
mode="numericpages"></pagerstyle>
</asp:datagrid></p>
<p>
<asp:panel id="panel1" runat="server">
<asp:linkbutton id="btnprevious" runat="server" commandname="previous"><=previous</asp:linkbutton>
<font face="宋体">
<asp:linkbutton id="btnnext" runat="server" commandname="next">next=></asp:linkbutton>
<asp:textbox id="txtindex" runat="server" width="24px" columns="2" maxlength="2"></asp:textbox>
<asp:button id="btngo" runat="server" backcolor="gainsboro" borderwidth="1px" borderstyle="solid"
bordercolor="silver" text="go!"></asp:button>
<asp:label id="lblerror" runat="server" forecolor="red" visible="false">no that page!</asp:label></font>
</asp:panel></p>
<font face="宋体"></font>
<asp:panel id="pnlindex" runat="server">panel</asp:panel>
</form>
</body>
</html>
//-------后台代码---------------------------------
//-------使用的数据库是sqlserver2000示例数据库------
using system;
using system.collections;
using system.componentmodel;
using system.configuration;
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;
namespace try1
{
/// <summary>
/// webform1 的摘要说明。
/// </summary>
public class webform1 : system.web.ui.page
{
protected system.web.ui.webcontrols.datagrid datagrid1;
private dataset dsnorth;
protected system.web.ui.webcontrols.panel panel1;
protected system.web.ui.webcontrols.linkbutton btnprevious;
protected system.web.ui.webcontrols.linkbutton btnnext;
//使用dataview,以实现排序效果
private dataview dvnorth;
protected system.web.ui.webcontrols.textbox txtindex;
protected system.web.ui.webcontrols.button btngo;
protected system.web.ui.webcontrols.label lblerror;
protected system.web.ui.webcontrols.panel pnlindex;
private void page_load(object sender, system.eventargs e)
{
// 在此处放置用户代码以初始化页面
loaddataset();
initpnlindex();
}
#region web 窗体设计器生成的代码
override protected void oninit(eventargs e)
{
//
// codegen: 该调用是 asp.net web 窗体设计器所必需的。
//
initializecomponent();
base.oninit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void initializecomponent()
{
this.datagrid1.pageindexchanged += new system.web.ui.webcontrols.datagridpagechangedeventhandler(this.datagrid1_pageindexchanged);
this.datagrid1.sortcommand += new system.web.ui.webcontrols.datagridsortcommandeventhandler(this.datagrid1_sortcommand);
this.btnprevious.click += new system.eventhandler(this.navigationbuttonclick);
this.btnnext.click += new system.eventhandler(this.navigationbuttonclick);
this.btngo.click += new system.eventhandler(this.btngo_click);
this.load += new system.eventhandler(this.page_load);
}
#endregion
//加载数据集
private void loaddataset()
{
sqlconnection cnnorth=new sqlconnection(configurationsettings.appsettings["cnnorth.connectionstring"]);
string cmdstring="select * from customers";
sqldataadapter adnorth=new sqldataadapter(cmdstring,cnnorth);
cnnorth.open();
dsnorth=new dataset();
try
{
adnorth.fill(dsnorth,"north");
}
catch
{
throw;
}
finally
{
cnnorth.close();
}
dvnorth=new dataview();
dvnorth.table=dsnorth.tables["north"];
datagrid1.datasource=dvnorth;
datagrid1.databind();
}
//初始化索引
private void initpnlindex()
{
pnlindex.controls.clear();
int pagecount;
linkbutton btnindex;
for (pagecount=0;pagecount<datagrid1.pagecount;pagecount++)
{
int btntext=pagecount+1;
btnindex=new linkbutton();
btnindex.text=btntext.tostring();
btnindex.commandname=pagecount.tostring();
btnindex.click+=new eventhandler(this.indexbuttonclick);
pnlindex.controls.add(btnindex);
pnlindex.controls.add(new literalcontrol(" "));
}
}
//索引单击效果
private void indexbuttonclick(object sender,system.eventargs e)
{
string direction=((linkbutton)sender).commandname;
int pageindex=convert.toint32(direction);
datagrid1.currentpageindex=pageindex;
datagrid1.databind();
}
//排序效果实现
private void datagrid1_sortcommand(object source, system.web.ui.webcontrols.datagridsortcommandeventargs e)
{
this.dvnorth.sort=e.sortexpression;
datagrid1.databind();
}
//索引单击效果
private void datagrid1_pageindexchanged(object source, system.web.ui.webcontrols.datagridpagechangedeventargs e)
{
datagrid1.currentpageindex=e.newpageindex;
datagrid1.databind();
}
//翻页效果
private void navigationbuttonclick(object sender, system.eventargs e)
{
string direction=((linkbutton)sender).commandname;
switch(direction.toupper())
{
case "previous":
datagrid1.currentpageindex=math.max(datagrid1.currentpageindex-1,0);
break;
case "next":
datagrid1.currentpageindex=math.min(datagrid1.currentpageindex+1,datagrid1.pagecount-1);
break;
default:
break;
}
datagrid1.databind();
}
//去第几页效果
private void btngo_click(object sender, system.eventargs e)
{
int pageindex=0;
try
{
pageindex=convert.toint32(txtindex.text);
}
catch(exception ex)
{
lblerror.visible=true;
lblerror.text=ex.message;
}
finally
{
lblerror.visible=false;
lblerror.text="no that page!";
}
if(pageindex>0&&pageindex<=datagrid1.pagecount)
{
try
{
datagrid1.currentpageindex=math.min(datagrid1.pagecount,pageindex-1);
datagrid1.databind();
}
catch
{
lblerror.visible=true;
}
finally
{
lblerror.visible=false;
}
}
else
{
lblerror.visible=true;
}
}
}
}
注册会员,创建你的web开发资料库,