首頁 >後端開發 >C#.Net教程 >Asp.Net中的三種分頁方式總結

Asp.Net中的三種分頁方式總結

高洛峰
高洛峰原創
2017-01-10 14:02:451364瀏覽

通常分頁有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結合預存程序進行分頁 

這種方法分頁稍微複雜一些,但是可以應付比較大的資料量。

前台: 

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

這裡用的預存程序比較複雜,因為SQL語句沒有能夠放到視圖中,也無法直接從表中查出結果,這個預存程序有點變態,如果有朋友看到了,希望能指點一下。 

其實預存程序的核心在於: 

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

更多Asp.Net中的三種分頁方式總結相關文章請關注PHP中文網!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn