為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上留言。