为DataGrid的自带分页添加首页、尾页及状态功能
2024-07-21 02:23:30
供稿:网友
datagrid提供了分页功能,不过看上去功能有限,但是我们可以通过datagrid的一些属性来获取状态以及增加首页、尾页功能按钮。这里没有使用datagrid的自定义分页功能,如果在速度效率不是很讲究的情况下,由datagrid自己管理分页还是不错的,付出的代价就是要把整个相关数据取出来后再删选指定页的数据。好处就是开发速度快,不需要写分页的存储过程。本文事例使用的是sql server中的northwind数据库。运行界面如下:
对于前台的显示界面,我放了一个datagrid;四个linkbutton导向按钮;四个literal来显示纪录状态。
剩下的就是用表格定位。
这里需要设置datagrid的allowpaging属性为true,同时设置allowcustompaging属性位false(默认为false),设置pagerstyle的visible属性为false,使前台不显示。
前台的代码如下:
<%@ page language="c#" codebehind="datagridpaging.aspx.cs" autoeventwireup="false" inherits="zz.aspnetpaging.datagridpaging" %>
<!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" pagesize="5" width="100%" allowpaging="true">
<headerstyle font-size="9pt"></headerstyle>
<footerstyle font-size="9pt"></footerstyle>
<pagerstyle visible="false" font-size="9pt" mode="numericpages"></pagerstyle>
</asp:datagrid></td>
</tr>
</table>
<table id="table2" style="font-size: 9pt" cellspacing="1" cellpadding="1" width="450" align="center"
border="1">
<tr>
<td style="width: 207px">
<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>
</form>
</body>
</html>
后台cs文件代码,datagridpaging类从system.web.ui.page继承,在数据绑定时需要注意没有数据的情况(0页时),以及当页数减少时避免前台正在反页导致缺页。
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 datagridpaging : system.web.ui.page
{
private static string connstring = configurationsettings.appsettings["connstring"];
private int recordcount;
private int pagecount;
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();
recordcount = ds.tables[0].rows.count;
//获取当前的页数
pagecount = (int)math.ceiling( recordcount * 1.0 / pagesize);
//避免纪录从有到无时,并且已经进行过反页的情况下currentpageindex > pagecount出错
if(recordcount ==0)
{
this.datagrid1.currentpageindex = 0;
}
else if(this.datagrid1.currentpageindex >= pagecount)
{
this.datagrid1.currentpageindex = pagecount - 1;
}
this.datagrid1.datasource = ds;
this.datagrid1.databind();
navigationstatechange();
}
#region web 窗体设计器生成的代码
override protected void oninit(eventargs e)
{
//
// codegen: 该调用是 asp.net web 窗体设计器所必需的。
//
initializecomponent();
base.oninit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
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 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();
}
//数据绑定
public static dataset getcustomersdata()
{
sqlconnection conn = new sqlconnection(connstring);
string sqlstr = "select customerid, companyname,address,phone from customers";
sqlcommand comm = new sqlcommand( sqlstr ,conn);
sqldataadapter dataadapter = new sqldataadapter(comm);
dataset ds = new dataset();
dataadapter.fill(ds);
return ds;
}
/// <summary>
/// 控制导航按钮或数字的状态
/// </summary>
public void navigationstatechange()
{
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;
}
}
if(recordcount == 0)//当没有纪录时datagrid.pagecount会显示1页
this.ltlpagecount.text = "0";
else
this.ltlpagecount.text = pagecount.tostring();
if(recordcount == 0)
this.ltlpageindex.text = "0";
else
this.ltlpageindex.text = (pageindex + 1).tostring();//在有页数的情况下前台显示页数加1
this.ltlpagesize.text = pagesize.tostring();
this.ltlrecordcount.text = recordcount.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;}
set{recordcount = value;}
}
}
}
如果需要追求执行效率,而且数据量比较大的情况下建议使用datagrid的自定义分页功能。存储过程执行只取一页的情况。如果有什么好的建议或发现问题请在blog上留言。