Usually there are three methods of paging, including asp.net's own data display space such as GridView and other paging, third-party paging controls such as aspnetpager, stored procedure paging, etc. Here are summarized separately.
The first one: Use GridView’s own paging. This is the simplest paging method.
Front-end method
<asp:GridView ID="GridView1" AllowPaging="true" runat="server" onpageindexchanging="GridView1_PageIndexChanging" PageSize="3"> </asp:GridView>
Backend method:
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(); } } }
Second: Use AspNetPager.dll with personalized display for paging
You need to add a reference to aspnetpager.dll here
Front desk:
<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>
Backend:
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(); } } }
Third method: Use AspNetPager combined with stored procedures for paging
This method of paging is slightly more complicated, but it can handle a relatively large amount of data .
Front desk:
<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>
Backend:
//绑定方法中需要传递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();
The stored procedure used here is more complicated, because the SQL statement cannot be placed in the view, and the results cannot be found directly from the table. This The stored procedure is a bit abnormal. If anyone sees it, I hope they can give some advice.
In fact, the core of the stored procedure is:
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
The above is the detailed content of Introducing several paging methods in asp.net. For more information, please follow other related articles on the PHP Chinese website!