Asp.Net中的三种分页方式总结
程序员文章站
2024-03-08 14:41:22
通常分页有3种方法,分别是asp.net自带的数据显示空间如gridview等自带的分页,第三方分页控件如aspnetpager,存储过程分页等。这里分别做总结。 第一种:...
通常分页有3种方法,分别是asp.net自带的数据显示空间如gridview等自带的分页,第三方分页控件如aspnetpager,存储过程分页等。这里分别做总结。
第一种:使用gridview自带分页,这种是最简单的分页方法。
前台的方法:
<asp:gridview id="gridview1" allowpaging="true" runat="server"
onpageindexchanging="gridview1_pageindexchanging" pagesize="3">
</asp:gridview>
后台方法:
代码
using system;
using system.collections.generic;
using system.linq;
using system.web;
using system.web.ui;
using system.web.ui.webcontrols;
using jxsoft.ticketmanage.model;
using jxsoft.ticketmanage.bll;
using system.text.regularexpressions;
using system.data;
namespace jxsoft.ticketmanage.web
{
public partial class test : system.web.ui.page
{
protected void page_load(object sender, eventargs e)
{
if(!ispostback)
{
binddata();
}
}
protected void binddata()
{
datatable dt=new datatable();
dt.columns.add("id");
dt.columns.add("name");
for (int i = 0; i < 10;i++ )
{
dt.rows.add(i.tostring(), i.tostring());
}
this.gridview1.datasource = dt;
this.gridview1.databind();
}
protected void gridview1_pageindexchanging(object sender, gridviewpageeventargs e)
{
this.gridview1.pageindex = e.newpageindex;
binddata();
}
}
}
第二种:使用个性化显示的aspnetpager.dll进行分页
此处需要添加aspnetpager.dll的引用
前台:
<form id="form1" runat="server">
<div>
<asp:gridview id="gridview1" runat="server" >
</asp:gridview>
<webdiyer:aspnetpager id="aspnetpager1" runat="server"
custominfohtml="第%currentpageindex%页,共%pagecount%页,每页%pagesize%条"
firstpagetext="首页" lastpagetext="尾页" layouttype="table" nextpagetext="下一页"
onpagechanging="aspnetpager1_pagechanging" pageindexboxtype="dropdownlist"
pagingbuttonlayouttype="span" prevpagetext="上一页" showcustominfosection="left"
showpageindexbox="always" submitbuttontext="go" pagesize="4" textafterpageindexbox="页"
textbeforepageindexbox="转到">
</webdiyer:aspnetpager>
</div>
</form>
后台:
using system;
using system.collections.generic;
using system.linq;
using system.web;
using system.web.ui;
using system.web.ui.webcontrols;
using jxsoft.ticketmanage.model;
using jxsoft.ticketmanage.bll;
using system.text.regularexpressions;
using system.data;
namespace jxsoft.ticketmanage.web
{
public partial class test : system.web.ui.page
{
protected void page_load(object sender, eventargs e)
{
if(!ispostback)
{
binddata();
}
}
protected void binddata()
{
datatable dt=new datatable();
dt.columns.add("id");
dt.columns.add("name");
for (int i = 0; i < 10;i++ )
{
dt.rows.add(i.tostring(), i.tostring());
}
dataset ds = new dataset();
ds.tables.add(dt);
pager(this.gridview1, this.aspnetpager1, ds);
}
protected void pager(gridview dl, wuqi.webdiyer.aspnetpager anp, system.data.dataset dst)
{
pageddatasource pds = new pageddatasource();
pds.datasource = dst.tables[0].defaultview;
pds.allowpaging = true;
anp.recordcount = dst.tables[0].defaultview.count;
pds.currentpageindex = anp.currentpageindex - 1;
pds.pagesize = anp.pagesize;
dl.datasource = pds;
dl.databind();
}
protected void aspnetpager1_pagechanging(object src, wuqi.webdiyer.pagechangingeventargs e)
{
aspnetpager1.currentpageindex = e.newpageindex;
binddata();
}
}
}
第三种:使用aspnetpager结合存储过程进行分页
这种方法分页稍微复杂一些,但是可以应付比较大的数据量。
前台:
<asp:gridview id="gridview1" runat="server" cssclass="gridtable" autogeneratecolumns="false" onrowdatabound="gridview1_rowdatabound" >
</asp:gridview>
<webdiyer:aspnetpager id="aspnetpager1" runat="server"
custominfohtml="第%currentpageindex%页,共%pagecount%页,每页%pagesize%条"
firstpagetext="首页" lastpagetext="尾页" layouttype="table" nextpagetext="下一页"
onpagechanged="aspnetpager1_pagechanged" pageindexboxtype="dropdownlist"
pagingbuttonlayouttype="span" prevpagetext="上一页" showcustominfosection="left"
showpageindexbox="always" submitbuttontext="go" pagesize="4" textafterpageindexbox="页"
textbeforepageindexbox="转到">
</webdiyer:aspnetpager>
后台:
//绑定方法中需要传递aspnetpager的两个属性
protected void databind(){
dataset ds = reportquerybll.gettcikdetailreport(this.txtstartdate.text,this.txtenddate.text,int.parse( this.dropdownlistpartment1.selectedvalue),
this.txtpayperson1.text,this.txtticketnum.text,this.txtticketno.text,
aspnetpager1.startrecordindex,aspnetpager1.endrecordindex);//注意最后两个参数是aspnetpager的属性。
this.gridview1.datasource = ds;
this.gridview1.databind();
}
//分页控件的页索引变化事件
protected void aspnetpager1_pagechanged(object src, eventargs e)
{
binddetailreporttogv();
}
//page_base中需要加载首次的数据条数
dataset ds = reportquerybll.getdetail(this.txtstartdate.text, this.txtenddate.text, int.parse(this.dropdownlistpartment1.selectedvalue), this.txtpayperson1.text, this.txtticketnum.text, this.txtticketno.text);
this.aspnetpager1.recordcount = ds.tables[0].rows.count;
binddetailreporttogv();
这里用的存储过程比较复杂,因为sql语句没有能够放到视图中,也无法直接从表中查出结果,这个存储过程有点变态,如果有朋友看到了,希望能指点一下。
其实存储过程的核心在于:
create procedure [dbo].[p_getpagedorders2005]
(@startindex int,
@endindex int
)
as
select * from (select row_number() over(order by ipid desc) as rownum,
[ipid],[ipfrom],[ipto],[iplocation],[ipcity],[iptonumber],[ipfromnumber] from ipinfo) as u
where rownum between @startindex and @endindex
go
代码
--下方可以忽略
--我用到的是存储过程:
set ansi_nulls on
set quoted_identifier on
go
create procedure [dbo].[pro_pager]
(@startindex int,
@endindex int,
@strwhere varchar(200)
)
as
select tb_on_tick_info.on_tick_id_int,tb_on_tick_info.on_tick_selldatetime_dtm,tb_on_tick_info.on_tick_ticketsnum_str, tb_department_info.dept_name_str, tb_user_info.user_name_str,
tb_on_tick_info.on_tick_sellnumber_str, tb_on_tick_info.on_tick_shouldpay_dec, tb_on_tick_info.on_tick_count_int,
tb_on_tick_info.on_tick_discount_dec, tb_on_tick_details.on_tick_details_startno_int, charindex(n'a',
tb_on_tick_info.on_tick_note_text) as expr3, tb_user_info_1.user_name_str as expr1, tb_ticket_type.tickettype_name_dec,
count( tb_on_tick_details.on_tick_details_id_int) as expr2 ,tb_department_info.dept_id_int
into #temp
from tb_user_info inner join
tb_on_tick_info on tb_user_info.user_id_int = tb_on_tick_info.on_tick_sellperson_int inner join
tb_department_info on tb_user_info.user_departid_int = tb_department_info.dept_id_int inner join
tb_user_info as tb_user_info_1 on tb_on_tick_info.on_tick_payperson_int = tb_user_info_1.user_id_int inner join
tb_on_tick_details on tb_on_tick_info.on_tick_sellnumber_str = tb_on_tick_details.on_tick_sellnumber_str inner join
tb_ticket_type on tb_on_tick_details.on_tick_details_ticketstype_int = tb_ticket_type.tickettype_id_int
where 1=1 +@strwhere
group by tb_on_tick_info.on_tick_selldatetime_dtm,tb_on_tick_info.on_tick_ticketsnum_str, tb_department_info.dept_name_str, tb_user_info.user_name_str,
tb_on_tick_info.on_tick_sellnumber_str, tb_on_tick_info.on_tick_shouldpay_dec, tb_on_tick_info.on_tick_count_int,
tb_on_tick_info.on_tick_discount_dec, charindex(n'a', tb_on_tick_info.on_tick_note_text), tb_user_info_1.user_name_str,
tb_ticket_type.tickettype_name_dec, tb_on_tick_details.on_tick_details_startno_int ,tb_department_info.dept_id_int,tb_on_tick_info.on_tick_id_int
declare @sql varchar(8000)
set @sql = 'select convert(varchar(12) , on_tick_selldatetime_dtm, 111 ) as on_tick_selldatetime_dtm,dept_name_str,user_name_str,on_tick_sellnumber_str,convert(varchar(15), on_tick_shouldpay_dec) as on_tick_shouldpay_dec,on_tick_count_int,on_tick_discount_dec'
select @sql=@sql+',sum(case tickettype_name_dec when '''+tickettype_name_dec+''' then expr2 else 0 end) ['+tickettype_name_dec+']'
from (select distinct tickettype_name_dec from tb_ticket_type ) as a
set @sql=@sql+' ,expr3,expr1,on_tick_ticketsnum_str,dept_id_int,on_tick_id_int into ##t from #temp
group by on_tick_selldatetime_dtm,dept_name_str,on_tick_ticketsnum_str,user_name_str,on_tick_sellnumber_str,on_tick_shouldpay_dec,on_tick_count_int,
on_tick_discount_dec ,expr3,expr1,dept_id_int,on_tick_id_int order by on_tick_selldatetime_dtm '
exec( @sql )
--select * from ##t
select * from (select row_number() over(order by on_tick_id_int desc) as rownum,
* from ##t) as u
where rownum between @startindex and @endindex
drop table ##t
第一种:使用gridview自带分页,这种是最简单的分页方法。
前台的方法:
复制代码 代码如下:
<asp:gridview id="gridview1" allowpaging="true" runat="server"
onpageindexchanging="gridview1_pageindexchanging" pagesize="3">
</asp:gridview>
后台方法:
代码
复制代码 代码如下:
using system;
using system.collections.generic;
using system.linq;
using system.web;
using system.web.ui;
using system.web.ui.webcontrols;
using jxsoft.ticketmanage.model;
using jxsoft.ticketmanage.bll;
using system.text.regularexpressions;
using system.data;
namespace jxsoft.ticketmanage.web
{
public partial class test : system.web.ui.page
{
protected void page_load(object sender, eventargs e)
{
if(!ispostback)
{
binddata();
}
}
protected void binddata()
{
datatable dt=new datatable();
dt.columns.add("id");
dt.columns.add("name");
for (int i = 0; i < 10;i++ )
{
dt.rows.add(i.tostring(), i.tostring());
}
this.gridview1.datasource = dt;
this.gridview1.databind();
}
protected void gridview1_pageindexchanging(object sender, gridviewpageeventargs e)
{
this.gridview1.pageindex = e.newpageindex;
binddata();
}
}
}
第二种:使用个性化显示的aspnetpager.dll进行分页
此处需要添加aspnetpager.dll的引用
前台:
复制代码 代码如下:
<form id="form1" runat="server">
<div>
<asp:gridview id="gridview1" runat="server" >
</asp:gridview>
<webdiyer:aspnetpager id="aspnetpager1" runat="server"
custominfohtml="第%currentpageindex%页,共%pagecount%页,每页%pagesize%条"
firstpagetext="首页" lastpagetext="尾页" layouttype="table" nextpagetext="下一页"
onpagechanging="aspnetpager1_pagechanging" pageindexboxtype="dropdownlist"
pagingbuttonlayouttype="span" prevpagetext="上一页" showcustominfosection="left"
showpageindexbox="always" submitbuttontext="go" pagesize="4" textafterpageindexbox="页"
textbeforepageindexbox="转到">
</webdiyer:aspnetpager>
</div>
</form>
后台:
复制代码 代码如下:
using system;
using system.collections.generic;
using system.linq;
using system.web;
using system.web.ui;
using system.web.ui.webcontrols;
using jxsoft.ticketmanage.model;
using jxsoft.ticketmanage.bll;
using system.text.regularexpressions;
using system.data;
namespace jxsoft.ticketmanage.web
{
public partial class test : system.web.ui.page
{
protected void page_load(object sender, eventargs e)
{
if(!ispostback)
{
binddata();
}
}
protected void binddata()
{
datatable dt=new datatable();
dt.columns.add("id");
dt.columns.add("name");
for (int i = 0; i < 10;i++ )
{
dt.rows.add(i.tostring(), i.tostring());
}
dataset ds = new dataset();
ds.tables.add(dt);
pager(this.gridview1, this.aspnetpager1, ds);
}
protected void pager(gridview dl, wuqi.webdiyer.aspnetpager anp, system.data.dataset dst)
{
pageddatasource pds = new pageddatasource();
pds.datasource = dst.tables[0].defaultview;
pds.allowpaging = true;
anp.recordcount = dst.tables[0].defaultview.count;
pds.currentpageindex = anp.currentpageindex - 1;
pds.pagesize = anp.pagesize;
dl.datasource = pds;
dl.databind();
}
protected void aspnetpager1_pagechanging(object src, wuqi.webdiyer.pagechangingeventargs e)
{
aspnetpager1.currentpageindex = e.newpageindex;
binddata();
}
}
}
第三种:使用aspnetpager结合存储过程进行分页
这种方法分页稍微复杂一些,但是可以应付比较大的数据量。
前台:
复制代码 代码如下:
<asp:gridview id="gridview1" runat="server" cssclass="gridtable" autogeneratecolumns="false" onrowdatabound="gridview1_rowdatabound" >
</asp:gridview>
<webdiyer:aspnetpager id="aspnetpager1" runat="server"
custominfohtml="第%currentpageindex%页,共%pagecount%页,每页%pagesize%条"
firstpagetext="首页" lastpagetext="尾页" layouttype="table" nextpagetext="下一页"
onpagechanged="aspnetpager1_pagechanged" pageindexboxtype="dropdownlist"
pagingbuttonlayouttype="span" prevpagetext="上一页" showcustominfosection="left"
showpageindexbox="always" submitbuttontext="go" pagesize="4" textafterpageindexbox="页"
textbeforepageindexbox="转到">
</webdiyer:aspnetpager>
后台:
复制代码 代码如下:
//绑定方法中需要传递aspnetpager的两个属性
protected void databind(){
dataset ds = reportquerybll.gettcikdetailreport(this.txtstartdate.text,this.txtenddate.text,int.parse( this.dropdownlistpartment1.selectedvalue),
this.txtpayperson1.text,this.txtticketnum.text,this.txtticketno.text,
aspnetpager1.startrecordindex,aspnetpager1.endrecordindex);//注意最后两个参数是aspnetpager的属性。
this.gridview1.datasource = ds;
this.gridview1.databind();
}
//分页控件的页索引变化事件
protected void aspnetpager1_pagechanged(object src, eventargs e)
{
binddetailreporttogv();
}
//page_base中需要加载首次的数据条数
dataset ds = reportquerybll.getdetail(this.txtstartdate.text, this.txtenddate.text, int.parse(this.dropdownlistpartment1.selectedvalue), this.txtpayperson1.text, this.txtticketnum.text, this.txtticketno.text);
this.aspnetpager1.recordcount = ds.tables[0].rows.count;
binddetailreporttogv();
这里用的存储过程比较复杂,因为sql语句没有能够放到视图中,也无法直接从表中查出结果,这个存储过程有点变态,如果有朋友看到了,希望能指点一下。
其实存储过程的核心在于:
复制代码 代码如下:
create procedure [dbo].[p_getpagedorders2005]
(@startindex int,
@endindex int
)
as
select * from (select row_number() over(order by ipid desc) as rownum,
[ipid],[ipfrom],[ipto],[iplocation],[ipcity],[iptonumber],[ipfromnumber] from ipinfo) as u
where rownum between @startindex and @endindex
go
代码
复制代码 代码如下:
--下方可以忽略
--我用到的是存储过程:
set ansi_nulls on
set quoted_identifier on
go
create procedure [dbo].[pro_pager]
(@startindex int,
@endindex int,
@strwhere varchar(200)
)
as
select tb_on_tick_info.on_tick_id_int,tb_on_tick_info.on_tick_selldatetime_dtm,tb_on_tick_info.on_tick_ticketsnum_str, tb_department_info.dept_name_str, tb_user_info.user_name_str,
tb_on_tick_info.on_tick_sellnumber_str, tb_on_tick_info.on_tick_shouldpay_dec, tb_on_tick_info.on_tick_count_int,
tb_on_tick_info.on_tick_discount_dec, tb_on_tick_details.on_tick_details_startno_int, charindex(n'a',
tb_on_tick_info.on_tick_note_text) as expr3, tb_user_info_1.user_name_str as expr1, tb_ticket_type.tickettype_name_dec,
count( tb_on_tick_details.on_tick_details_id_int) as expr2 ,tb_department_info.dept_id_int
into #temp
from tb_user_info inner join
tb_on_tick_info on tb_user_info.user_id_int = tb_on_tick_info.on_tick_sellperson_int inner join
tb_department_info on tb_user_info.user_departid_int = tb_department_info.dept_id_int inner join
tb_user_info as tb_user_info_1 on tb_on_tick_info.on_tick_payperson_int = tb_user_info_1.user_id_int inner join
tb_on_tick_details on tb_on_tick_info.on_tick_sellnumber_str = tb_on_tick_details.on_tick_sellnumber_str inner join
tb_ticket_type on tb_on_tick_details.on_tick_details_ticketstype_int = tb_ticket_type.tickettype_id_int
where 1=1 +@strwhere
group by tb_on_tick_info.on_tick_selldatetime_dtm,tb_on_tick_info.on_tick_ticketsnum_str, tb_department_info.dept_name_str, tb_user_info.user_name_str,
tb_on_tick_info.on_tick_sellnumber_str, tb_on_tick_info.on_tick_shouldpay_dec, tb_on_tick_info.on_tick_count_int,
tb_on_tick_info.on_tick_discount_dec, charindex(n'a', tb_on_tick_info.on_tick_note_text), tb_user_info_1.user_name_str,
tb_ticket_type.tickettype_name_dec, tb_on_tick_details.on_tick_details_startno_int ,tb_department_info.dept_id_int,tb_on_tick_info.on_tick_id_int
declare @sql varchar(8000)
set @sql = 'select convert(varchar(12) , on_tick_selldatetime_dtm, 111 ) as on_tick_selldatetime_dtm,dept_name_str,user_name_str,on_tick_sellnumber_str,convert(varchar(15), on_tick_shouldpay_dec) as on_tick_shouldpay_dec,on_tick_count_int,on_tick_discount_dec'
select @sql=@sql+',sum(case tickettype_name_dec when '''+tickettype_name_dec+''' then expr2 else 0 end) ['+tickettype_name_dec+']'
from (select distinct tickettype_name_dec from tb_ticket_type ) as a
set @sql=@sql+' ,expr3,expr1,on_tick_ticketsnum_str,dept_id_int,on_tick_id_int into ##t from #temp
group by on_tick_selldatetime_dtm,dept_name_str,on_tick_ticketsnum_str,user_name_str,on_tick_sellnumber_str,on_tick_shouldpay_dec,on_tick_count_int,
on_tick_discount_dec ,expr3,expr1,dept_id_int,on_tick_id_int order by on_tick_selldatetime_dtm '
exec( @sql )
--select * from ##t
select * from (select row_number() over(order by on_tick_id_int desc) as rownum,
* from ##t) as u
where rownum between @startindex and @endindex
drop table ##t
下一篇: 冒泡排序的原理及java代码实现