为DataGrid自定义分页添加自定义导航和分页信息
2024-07-21 02:23:10
供稿:网友
在上一篇文章中我讲到了对datagrid实行自定义分页,这可以避免为了显示一页数据而获取整个数据记录集,从而提高分页效率,不过使用的导航还是datagrid自带的数字连接或简单的上一页,下一页,而且看不到总页数、总记录数之类的信息。下面就为他增加我们所需要的部分。
先来看看修改后的分页显示,截图如下:
(图一)
使用的数据源同上一篇文章(asp.net中datagrid控件的自定义分页)相同,都是访问northwind库,为了独立开来这里还是把存储过程列了一下,
create procedure [getcustomersdatapage]
@pageindex int,
@pagesize int,
@recordcount int out,
@pagecount int out
as
select @recordcount = count(*) from customers
set @pagecount = ceiling(@recordcount * 1.0 / @pagesize)
declare @sqlstr nvarchar(1000)
if @pageindex = 0 or @pagecount <= 1
set @sqlstr =n'select top '+str( @pagesize )+
' customerid, companyname,address,phone from customers order by customerid desc
else if @pageindex = @pagecount - 1
set @sqlstr =n' select * from ( select top '+str( @recordcount - @pagesize * @pageindex )+
' customerid, companyname,address,phone from customers order by customerid asc ) temptable order by customerid desc'
else
set @sqlstr =n' select top '+str( @pagesize )+' * from ( select top '+str( @recordcount - @pagesize * @pageindex )+
' customerid, companyname,address,phone from customers order by customerid asc ) temptable order by customerid desc'
exec (@sqlstr)
go
下面就就把代码贴了一下,
aspx文件代码如下:
<%@ page language="c#" codebehind="datagridcustompaging.aspx.cs" autoeventwireup="false" inherits="zz.aspnetpaging.datagridcustompaging" %>
<!doctype html public "-//w3c//dtd html 4.0 transitional//en" >
<html>
<head>
<title>datagridpaging</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">
<table id="table1" style="font-size: 9pt" cellspacing="1" cellpadding="1" width="450" align="center"
border="1">
<tr>
<td><asp:datagrid id="datagrid1" runat="server" allowpaging="true" allowcustompaging="true" width="100%">
<footerstyle font-size="9pt"></footerstyle>
<headerstyle font-size="9pt"></headerstyle>
<pagerstyle visible="false" font-size="9pt" mode="numericpages"></pagerstyle>
</asp:datagrid></td>
</tr>
<tr>
<td>
<table id="table2" style="font-size: 9pt" cellspacing="1" cellpadding="1" width="100%"
align="center" border="1">
<tr>
<td style="width: 150px"><asp:linkbutton id="lbtnfirst" runat="server" commandname="first">首页</asp:linkbutton>
<asp:linkbutton id="lbtnprev" runat="server" commandname="prev">上一页</asp:linkbutton>
<asp:linkbutton id="lbtnnext" runat="server" commandname="next">下一页</asp:linkbutton>
<asp:linkbutton id="lbtnlast" runat="server" commandname="last">尾页</asp:linkbutton></td>
<td>第<asp:literal id="ltlpageindex" runat="server"></asp:literal>页 共<asp:literal id="ltlpagecount" runat="server"></asp:literal>页
每页<asp:literal id="ltlpagesize" runat="server"></asp:literal>条 共<asp:literal id="ltlrecordcount" runat="server"></asp:literal>条
</td>
</tr>
</table>
</td>
</tr>
</table>
</form>
</body>
</html>
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 zz.aspnetpaging
{
public class datagridcustompaging : system.web.ui.page
{
private int pagecount;
private int recordcount;
protected system.web.ui.webcontrols.linkbutton lbtnfirst;
protected system.web.ui.webcontrols.linkbutton lbtnprev;
protected system.web.ui.webcontrols.linkbutton lbtnnext;
protected system.web.ui.webcontrols.linkbutton lbtnlast;
protected system.web.ui.webcontrols.literal ltlpageindex;
protected system.web.ui.webcontrols.literal ltlpagecount;
protected system.web.ui.webcontrols.literal ltlpagesize;
protected system.web.ui.webcontrols.literal ltlrecordcount;
protected system.web.ui.webcontrols.datagrid datagrid1;
private void page_load(object sender, system.eventargs e)
{
if(!page.ispostback)
{
datagriddatabind();
}
}
//绑定数据
private void datagriddatabind()
{
dataset ds = getcustomersdata(pageindex,pagesize,ref recordcount,ref pagecount);
this.datagrid1.virtualitemcount = recordcount;
this.datagrid1.datasource = ds;
this.datagrid1.databind();
setpagingstate();
}
#region web 窗体设计器生成的代码
override protected void oninit(eventargs e)
{
initializecomponent();
base.oninit(e);
}
private void initializecomponent()
{
this.lbtnfirst.click += new system.eventhandler(this.lbtnnavigation_click);
this.lbtnprev.click += new system.eventhandler(this.lbtnnavigation_click);
this.lbtnnext.click += new system.eventhandler(this.lbtnnavigation_click);
this.lbtnlast.click += new system.eventhandler(this.lbtnnavigation_click);
this.load += new system.eventhandler(this.page_load);
}
#endregion
private static dataset getcustomersdata(int pageindex,int pagesize,ref int recordcount,ref int pagecount)
{
string connstring = configurationsettings.appsettings["connstring"];
sqlconnection conn = new sqlconnection(connstring);
sqlcommand comm = new sqlcommand("getcustomersdatapage",conn);
comm.parameters.add(new sqlparameter("@pageindex",sqldbtype.int));
comm.parameters[0].value = pageindex;
comm.parameters.add(new sqlparameter("@pagesize",sqldbtype.int));
comm.parameters[1].value = pagesize;
comm.parameters.add(new sqlparameter("@recordcount",sqldbtype.int));
comm.parameters[2].direction = parameterdirection.output;
comm.parameters.add(new sqlparameter("@pagecount",sqldbtype.int));
comm.parameters[3].direction = parameterdirection.output;
comm.commandtype = commandtype.storedprocedure;
sqldataadapter dataadapter = new sqldataadapter(comm);
dataset ds = new dataset();
dataadapter.fill(ds);
recordcount = (int)comm.parameters[2].value;
pagecount = (int)comm.parameters[3].value;
return ds;
}
private void lbtnnavigation_click(object sender, system.eventargs e)
{
linkbutton btn = (linkbutton)sender;
switch(btn.commandname)
{
case "first":
pageindex = 0;
break;
case "prev"://if( pageindex > 0 )
pageindex = pageindex - 1;
break;
case "next"://if( pageindex < pagecount -1)
pageindex = pageindex + 1;
break;
case "last":
pageindex = pagecount - 1;
break;
}
datagriddatabind();
}
/// <summary>
/// 控制导航按钮或数字的状态
/// </summary>
public void setpagingstate()
{
if( pagecount <= 1 )//( recordcount <= pagesize )//小于等于一页
{
this.lbtnfirst.enabled = false;
this.lbtnprev.enabled = false;
this.lbtnnext.enabled = false;
this.lbtnlast.enabled = false;
}
else //有多页
{
if( pageindex == 0 )//当前为第一页
{
this.lbtnfirst.enabled = false;
this.lbtnprev.enabled = false;
this.lbtnnext.enabled = true;
this.lbtnlast.enabled = true;
}
else if( pageindex == pagecount - 1 )//当前为最后页
{
this.lbtnfirst.enabled = true;
this.lbtnprev.enabled = true;
this.lbtnnext.enabled = false;
this.lbtnlast.enabled = false;
}
else //中间页
{
this.lbtnfirst.enabled = true;
this.lbtnprev.enabled = true;
this.lbtnnext.enabled = true;
this.lbtnlast.enabled = true;
}
}
this.ltlpagesize.text = pagesize.tostring();
this.ltlrecordcount.text = recordcount.tostring();
if(recordcount == 0)
{
this.ltlpagecount.text = "0";
this.ltlpageindex.text = "0";
}
else
{
this.ltlpagecount.text = pagecount.tostring();
this.ltlpageindex.text = (pageindex + 1).tostring();
}
}
public int pagecount
{
get
{
return this.datagrid1.pagecount;
}
}
public int pagesize
{
get
{
return this.datagrid1.pagesize;
}
}
public int pageindex
{
get
{
return this.datagrid1.currentpageindex;
}
set
{
this.datagrid1.currentpageindex = value;
}
}
public int recordcount
{
get
{
return recordcount;
}
}
}
}
上面的代码比较简单,也就不用分析了,如果有什么好的建议或有问题可以在blog上留言,很高兴同大家交流。