pagechange.ascx
================================================================
<%@ control language="c#" autoeventwireup="false" codebehind="pagechange.ascx.cs" inherits="ex_test.pagechange" targetschema="http://schemas.microsoft.com/intellisense/ie5"%>
<font face="宋体">
<table id="table1" cellspacing="1" cellpadding="1" width="98%" border="0">
<tr>
<td align="right"><asp:linkbutton id="firstpage" runat="server">[首 页]</asp:linkbutton>
<asp:linkbutton id="prevpage" runat="server">[上一页]</asp:linkbutton>
<asp:linkbutton id="nextpage" runat="server">[下一页]</asp:linkbutton>
<asp:linkbutton id="lastpage" runat="server">[末 页]</asp:linkbutton>
<asp:literal id="literal1" runat="server" text="转到第"></asp:literal><asp:textbox id="newpageindex" runat="server" width="31px"></asp:textbox><asp:literal id="literal2" runat="server" text="页"></asp:literal>
<asp:button id="newpagego" runat="server" text="go"></asp:button> </td>
</tr>
</table>
</font>
=======================================================================
pagechange.ascx.cs
===================================================================
namespace ex_test
{
using system;
using system.data;
using system.drawing;
using system.web;
using system.web.ui.webcontrols;
using system.web.ui.htmlcontrols;
using system.data.sqlclient;
/// <summary>
/// pagechange 的摘要说明。
/// </summary>
public abstract class pagechange : system.web.ui.usercontrol
{
protected system.web.ui.webcontrols.button newpagego;
protected system.web.ui.webcontrols.literal literal2;
protected system.web.ui.webcontrols.textbox newpageindex;
protected system.web.ui.webcontrols.literal literal1;
protected system.web.ui.webcontrols.linkbutton lastpage;
protected system.web.ui.webcontrols.linkbutton nextpage;
protected system.web.ui.webcontrols.linkbutton prevpage;
protected system.web.ui.webcontrols.linkbutton firstpage;
protected int currentpage;
protected int pagesize;
protected string proc;
protected system.web.ui.webcontrols.datagrid datagrid;
public int _currentpage
{
get
{
return currentpage;
}
set
{
currentpage = value;
}
}
public int _pagesize
{
get
{
return pagesize;
}
set
{
pagesize = value;
}
}
public string _proc
{
get
{
return proc;
}
set
{
proc = value;
}
}
public datagrid _datagrid
{
get
{
return datagrid;
}
set
{
datagrid = value;
}
}
protected int rowcount;
private void page_load(object sender, system.eventargs e)
{
// 在此处放置用户代码以初始化页面
if(!ispostback)
{
using(sqlconnection conn = new sqlconnection("user id=sa;password=admin;server=server-mk;initial catalog=pubs;timeout=90")
{
sqlcommand cmd = new sqlcommand("select count(*) as expr1 from authors",conn);
cmd.connection.open();
rowcount = (int)cmd.executescalar();
cmd.connection.close();
viewstate["rowscount"] = rowcount;
}
viewstate["currentpage"] = currentpage;
fillgrid(proc,currentpage,pagesize,datagrid);
}
}
#region web form designer generated code
override protected void oninit(eventargs e)
{
//
// codegen:该调用是 asp.net web 窗体设计器所必需的。
//
initializecomponent();
base.oninit(e);
}
/// 设计器支持所需的方法 - 不要使用
/// 代码编辑器修改此方法的内容。
/// </summary>
private void initializecomponent()
{
this.firstpage.click += new system.eventhandler(this.firstpage_click);
this.prevpage.click += new system.eventhandler(this.prevpage_click);
this.nextpage.click += new system.eventhandler(this.nextpage_click);
this.lastpage.click += new system.eventhandler(this.lastpage_click);
this.newpagego.click += new system.eventhandler(this.newpagego_click);
this.load += new system.eventhandler(this.page_load);
}
#endregion
private void fillgrid(string proc,int currentpage,int pagesize,datagrid datagrid)
{
using(sqlconnection conn = new sqlconnection("user id=sa;password=admin;server=server-mk;initial catalog=pubs;timeout=90")
{
sqlcommand cmd = new sqlcommand(proc,conn);
cmd.commandtype = commandtype.storedprocedure;
cmd.parameters.add("@currentpage",currentpage);
cmd.parameters.add("@pagesize",pagesize);
cmd.connection.open();
sqldatareader sdr = cmd.executereader();
datagrid.datasource = sdr;
datagrid.databind();
sdr.close();
cmd.connection.close();
}
}
//首页
private void firstpage_click(object sender, system.eventargs e)
{
//disabled首页按钮和上一页按钮
firstpage.enabled = false;
prevpage.enabled = false;
currentpage = 0;
viewstate["currentpage"] = currentpage;
fillgrid(proc,currentpage,pagesize,datagrid);
//如果不止一页
if((int)viewstate["rowscount"]>((int)viewstate["currentpage"]+1)*pagesize)
{
nextpage.enabled = true;
}
if((int)viewstate["rowscount"]>((int)viewstate["currentpage"]+1)*pagesize)
{
lastpage.enabled = true;
}
}
//上一页
private void prevpage_click(object sender, system.eventargs e)
{
nextpage.enabled = true;
lastpage.enabled = true;
currentpage = (int)viewstate["currentpage"]-1;
viewstate["currentpage"] = currentpage;
fillgrid(proc,currentpage,pagesize,datagrid);
//如果到首页则disabled首页和上一页按钮
if((int)viewstate["currentpage"]==0)
{
prevpage.enabled = false;
firstpage.enabled = false;
//return;
}
}
//下一页
private void nextpage_click(object sender, system.eventargs e)
{
viewstate["currentpage"] = (int)viewstate["currentpage"]+1;
currentpage = (int)viewstate["currentpage"];
fillgrid(proc,currentpage,pagesize,datagrid);
prevpage.enabled = true;
firstpage.enabled = true;
//如果已经到了最后一页
if(((int)viewstate["currentpage"]+1)*pagesize>(int)viewstate["rowscount"])
{
nextpage.enabled = false;
lastpage.enabled = false;
}
}
//末页
private void lastpage_click(object sender, system.eventargs e)
{
lastpage.enabled = false;
nextpage.enabled = false;
viewstate["currentpage"] = (int)math.ceiling((int)viewstate["rowscount"]/pagesize);
currentpage = (int)viewstate["currentpage"];
fillgrid(proc,currentpage,pagesize,datagrid);
//如果有不止一页的纪录
if((int)viewstate["currentpage"]>1)
{
firstpage.enabled = true;
prevpage.enabled = true;
}
//如果只有一页的纪录
else
{
firstpage.enabled = false;
prevpage.enabled = false;
}
}
//跳转
private void newpage_go(string i)
{
try
{
int pageindex = int32.parse(i);
if (pageindex<=0)
{
pageindex = 0;
}
else
{
if(pageindex>(int)math.ceiling((int)viewstate["rowscount"]/pagesize))
{
pageindex = (int)math.ceiling((int)viewstate["rowscount"]/pagesize);
}
else
{
pageindex--;
}
}
//简单起见,将所有的linkbutton全部改为enable=true
firstpage.enabled = true;
nextpage.enabled = true;
lastpage.enabled = true;
prevpage.enabled = true;
viewstate["currentpage"] = pageindex;
fillgrid(proc,(int)viewstate["currentpage"],pagesize,datagrid);
}
catch(exception)
{
return;
}
}
private void newpagego_click(object sender, system.eventargs e)
{
newpage_go(newpageindex.text.trim());
}
}
}
pageform.aspx
==================================================================
<%@ page language="c#" codebehind="pageform.aspx.cs" autoeventwireup="false" inherits="ex_test.pageform" %>
<%@ register tagprefix="mk" tagname="pagechange" src="pagechange.ascx"%>
<!doctype html public "-//w3c//dtd html 4.0 transitional//en" >
<html>
<head>
<title>pageform</title>
<meta name="generator" content="microsoft visual studio 7.0">
<meta name="code_language" content="c#">
<meta name="vs_defaultclientscript" content="javascript">
<meta name="vs_targetschema" content="http://schemas.microsoft.com/intellisense/ie5">
</head>
<body ms_positioning="gridlayout">
<form id="pageform" method="post" runat="server">
<aspatagrid id="datagrid1" style="z-index: 101; left: 57px; position: absolute; top: 54px" runat="server" allowcustompaging="true" allowpaging="true">
<pagerstyle visible="false"></pagerstyle>
</aspatagrid>
<aspanel id="panel1" style="z-index: 102; left: 60px; position: absolute; top: 20px" runat="server" width="634px">
<mkagechange id="pc" runat="server"></mkagechange>
</aspanel><font face="宋体"></font>
</form>
</body>
</html>
=================================================================
pageform.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;
namespace ex_test
{
/// <summary>
/// pageform 的摘要说明。
/// </summary>
public class pageform : system.web.ui.page
{
protected system.web.ui.webcontrols.datagrid datagrid1;
protected system.web.ui.webcontrols.panel panel1;
protected ex_test.pagechange pc;
private void page_load(object sender, system.eventargs e)
{
// 在此处放置用户代码以初始化页面
//if(!ispostback)
//{
pc._currentpage = 0;
pc._datagrid = datagrid1;
pc._pagesize =7;
pc._proc = "page_change";
//}
}
#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
}
}
=================================================================
存储过程:
create procedure dbo.page_change
/*
(
@parameter1 datatype = default value,
@parameter2 datatype output
)
*/
(
@pagesize int,
@currentpage int
)
as
/* set nocount on */
select *,identity(int,1,1) as num into #tempauthors from authors
select * from #tempauthors where num > (@pagesize*@currentpage) and num < (@pagesize*@[email protected]+1)
return