135-1821-9792

asp.net中实现分页的方式有哪些

这篇文章给大家分享的是有关asp.net中实现分页的方式有哪些的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。

公司主营业务:成都网站设计、网站建设、外贸网站建设、移动网站开发等业务。帮助企业客户真正实现互联网宣传,提高企业的竞争能力。创新互联公司是一支青春激扬、勤奋敬业、活力青春激扬、勤奋敬业、活力澎湃、和谐高效的团队。公司秉承以“开放、自由、严谨、自律”为核心的企业文化,感谢他们对我们的高要求,感谢他们从不同领域给我们带来的挑战,让我们激情的团队有机会用头脑与智慧不断的给客户带来惊喜。创新互联公司推出托克托免费做网站回馈大家。

通常分页有3种方法,分别是asp.net自带的数据显示空间如GridView等自带的分页,第三方分页控件如aspnetpager,存储过程分页等。这里分别做总结。
第一种:使用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的引用
前台:

 
         
 

后台:

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结合存储过程进行分页
这种方法分页稍微复杂一些,但是可以应付比较大的数据量。
前台:

 
 
 

后台:

//绑定方法中需要传递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

感谢各位的阅读!关于“asp.net中实现分页的方式有哪些”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识,如果觉得文章不错,可以把它分享出去让更多的人看到吧!


名称栏目:asp.net中实现分页的方式有哪些
当前路径:http://kswsj.com/article/jjdcpd.html

其他资讯



Copyright © 2009-2022 www.kswsj.com 成都快上网科技有限公司 版权所有 蜀ICP备19037934号