為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上留言,很高興同大家交流。