Asp.net中DataGrid控件的自定義分頁
2024-07-10 12:57:11
供稿:網友
asp.net中datagrid控件的自定義分頁
鄭 佐 2004-10-28
使用datagrid時自帶的分頁實現起來雖然比較方便,但是效率不高,每次都需要讀取所有頁(整個記錄集),而加載的只是其中一頁,造成了資源的浪費,記錄多又會使效率變得很低。下面通過datagrid的自定義分頁功能來減少資源使用和提高效率。
實現的關鍵是設置allowcustompaging屬性位true,并把virtualitemcount屬性設置位總的記錄數,給分頁提供依據,前臺的主要代碼如下:
<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" width="100%" allowpaging="true" allowcustompaging="true">
<pagerstyle font-size="9pt" mode="numericpages"></pagerstyle>
</asp:datagrid></td>
</tr>
</table>
</form>
這里使用的數據源還是假設為northwind的customers表。
下面是訪問單頁的存儲過程,實現方式很多,不過這個是最普通的,
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
獲取記錄數和頁數都采用存儲過程的輸出參數。
獲取數據源,這里返回一個dataset。
先定義了連個數據成員,
private int pagecount;//頁數
private int recordcount;//記錄數
//獲取單頁數據
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;
}
//綁定數據到datagrid,同時刷新數據總記錄數
private void datagriddatabind()
{
dataset ds = getcustomersdata(pageindex,pagesize,ref recordcount,ref pagecount);
this.datagrid1.virtualitemcount = recordcount;
this.datagrid1.datasource = ds;
this.datagrid1.databind();
}
下面是分頁的幾個變量屬性
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;}
}
注冊datagrid分頁事件
//分頁事件處理
private void datagrid1_pageindexchanged(object source, system.web.ui.webcontrols.datagridpagechangedeventargs e)
{
datagrid dg = (datagrid)source;
dg.currentpageindex = e.newpageindex;
datagriddatabind();
}
最好判斷當前頁面是否是第一次加載,防止重復加載兩次數據,
private void page_load(object sender, system.eventargs e)
{
if(!page.ispostback)
{
datagriddatabind();
}
}
顯示界面如下:
這個例子中沒有顯示分頁的一些參數,我們可以進一步對其進行改進。