Home Backend Development C#.Net Tutorial Summary of three paging methods in Asp.Net

Summary of three paging methods in Asp.Net

Jan 10, 2017 pm 02:02 PM

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 summaries.
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>
Copy after login

Backend method:
Code

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

Second: Use AspNetPager.dll with personalized display for paging
Aspnetpager needs to be added here Reference to .dll
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>
Copy after login

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

Third method: Use AspNetPager combined with stored procedures for paging
This method of paging is slightly more complicated, but it can be dealt with 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>
Copy after login

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();
Copy after login

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
Copy after login

Code

--下方可以忽略 
--我用到的是存储过程: 
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&#39;a&#39;, 
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&#39;a&#39;, 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 = &#39;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&#39; 
select @sql=@sql+&#39;,sum(case tickettype_name_dec when &#39;&#39;&#39;+tickettype_name_dec+&#39;&#39;&#39; then expr2 else 0 end) [&#39;+tickettype_name_dec+&#39;]&#39; 
from (select distinct tickettype_name_dec from tb_Ticket_Type ) as a 
set @sql=@sql+&#39; ,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 &#39; 
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
Copy after login

For more related articles summarizing the three paging methods in Asp.Net, please pay attention to the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What are the alternatives to NULL in C language What are the alternatives to NULL in C language Mar 03, 2025 pm 05:37 PM

This article explores the challenges of NULL pointer dereferences in C. It argues that the problem isn't NULL itself, but its misuse. The article details best practices for preventing dereferences, including pre-dereference checks, pointer initiali

How to add next-level C compiler How to add next-level C compiler Mar 03, 2025 pm 05:44 PM

This article explains how to create newline characters in C using the \n escape sequence within printf and puts functions. It details the functionality and provides code examples demonstrating its use for line breaks in output.

Which C language compiler is better? Which C language compiler is better? Mar 03, 2025 pm 05:39 PM

This article guides beginners on choosing a C compiler. It argues that GCC, due to its ease of use, wide availability, and extensive resources, is best for beginners. However, it also compares GCC, Clang, MSVC, and TCC, highlighting their differenc

Is NULL still important in modern programming in C language? Is NULL still important in modern programming in C language? Mar 03, 2025 pm 05:35 PM

This article emphasizes the continued importance of NULL in modern C programming. Despite advancements, NULL remains crucial for explicit pointer management, preventing segmentation faults by marking the absence of a valid memory address. Best prac

What are the web versions of C language compilers? What are the web versions of C language compilers? Mar 03, 2025 pm 05:42 PM

This article reviews online C compilers for beginners, focusing on ease of use and debugging capabilities. OnlineGDB and Repl.it are highlighted for their user-friendly interfaces and helpful debugging tools. Other options like Programiz and Compil

C language online programming website C language compiler official website summary C language online programming website C language compiler official website summary Mar 03, 2025 pm 05:41 PM

This article compares online C programming platforms, highlighting differences in features like debugging tools, IDE functionality, standard compliance, and memory/execution limits. It argues that the "best" platform depends on user needs,

Method of copying code by C language compiler Method of copying code by C language compiler Mar 03, 2025 pm 05:43 PM

This article discusses efficient code copying in C IDEs. It emphasizes that copying is an IDE function, not a compiler feature, and details strategies for improved efficiency, including using IDE selection tools, code folding, search/replace, templa

How to solve the problem of not popping up the output window by the C language compiler How to solve the problem of not popping up the output window by the C language compiler Mar 03, 2025 pm 05:40 PM

This article troubleshoots missing output windows in C program compilation. It examines causes like failing to run the executable, program errors, incorrect compiler settings, background processes, and rapid program termination. Solutions involve ch

See all articles