首页 > 编程 > .NET > 正文

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();

}

}

显示界面如下:



这个例子中没有显示分页的一些参数,我们可以进一步对其进行改进。


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表