Asp.Net中的三种分页方式

通常分页有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();

存储过程的核心在于:

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

Asp.Net中的三种分页方式的更多相关文章

  1. SQL Server 中的三种分页方式

    USE tempdb GO SET NOCOUNT ON --创建表结构 IF OBJECT_ID(N'ClassB', N'U') IS NOT NULL DROP TABLE ClassB GO ...

  2. python中的三种输入方式

    python中的三种输入方式 python2.X python2.x中以下三个函数都支持: raw_input() input() sys.stdin.readline() raw_input( )将 ...

  3. SQL Server中的三种Join方式

      1.测试数据准备 参考:Sql Server中的表访问方式Table Scan, Index Scan, Index Seek 这篇博客中的实验数据准备.这两篇博客使用了相同的实验数据. 2.SQ ...

  4. Java之线程安全中的三种同步方式

    一个程序在运行起来时,会转换为进程,通常含有多个线程. 通常情况下,一个进程中的比较耗时的操作(如长循环.文件上传下载.网络资源获取等),往往会采用多线程来解决. 比如,现实生活中,银行取钱问题.火车 ...

  5. C++中的三种继承方式

    1,被忽略的细节: 1,冒号( :)表示继承关系,Parent 表示被继承的类,public 的意义是什么? class Parent { }; class Child : public Parent ...

  6. ASP.Net 中的三种控件

    ---恢复内容开始--- 第一种:HTML控件 ASP.Net把HTML控件当成普通字符串渲染到浏览器端,不去检查正确性.无法在服务器端进行处理. 比如: <input111 type=&quo ...

  7. T-sql中的三种分页查询

    USE [APS_Future_FT] GO /****** Object: StoredProcedure [dbo].[A_PagingAndSorting] Script Date: 2013/ ...

  8. selenium中的三种等待方式(显示等待WebDriverWait()、隐式等待implicitly()、强制等待sleep())---基于python

    我们在实际使用selenium或者appium时,等待下个等待定位的元素出现,特别是web端加载的过程,都需要用到等待,而等待方式的设置是保证脚本稳定有效运行的一个非常重要的手段,在selenium中 ...

  9. sqlserver三种分页方式性能比较

    Liwu_Items表,CreateTime列建立聚集索引 第一种,sqlserver2005特有的分页语法 declare @page intdeclare @pagesize intset @pa ...

随机推荐

  1. Asp.net vnext的IIS部署

    微软前段时间推出asp.net vnext,自个也尝试了一把,也写了几篇关于vnext特性的博客,如下. 但一直有个困扰的问题,asp.net vnext如何在iis部署,因为整个目录结构跟之前有了很 ...

  2. 初学git,出现错误:fatal: Not a git repository (or any of the parent directories): .git

    提示说没有.git这样一个目录,解决办法: 输入  git init 就可以啦.

  3. uva10106(大数乘法)

    public class Product { public static void main(String[] args){ Scanner sc = new Scanner(new Buffered ...

  4. 学习IT资源分享,欢迎各位知道的学习IT资源前来分享

    1:排序不是按照名次,随机排序 慕课学习网https://www.imooc.com/ 腾讯课堂 https://ke.qq.com/ 柠檬学院 http://bjlemon.edusoho.cn/ ...

  5. 《Java数据结构与算法》笔记-CH4-3用栈实现分隔符匹配

    import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; /** * 利 ...

  6. python学习笔记七--数据操作符

    一.Python表达式操作符及程序:

  7. C# 内存管理优化实践

    内存优化畅想系列文章已经结束了,很多读者读完之后可能觉得“然并卵”,毕竟都是给微软提的建议而已,现在都没有实现.那么为了优化内存,有没有什么我们现在就能用的技巧呢?我的答案是:有.网上关于.net内存 ...

  8. 创业不要在IT业内创(市场太小,而且都喜欢自己折腾、没有花钱的习惯)

    精神是好的.但是,我看了你的产品和网站:你选的创业方向基本是死路一条,我劝你该放弃就放弃.我判断你的思路还局限在IT行业内,创业不要在IT业内创,要走出去,睁开眼看看别人的世界,自己体会.我不是打击你 ...

  9. 浅谈Spring(一)

    一.Spring引言 Spring是一款轻量级框架,代码入侵量很小,并且还是众多优秀的设计模式的组合(工厂.代理.模板.策略). 特点: 1.方便解耦,简化开发 通过Spring提供的IoC容器,我们 ...

  10. 方向梯度直方图(HOG)和颜色直方图的一些比較

    近期在学习视频检索领域的镜头切割方面的知识,发现经常使用的方法是直方图的方法,所以才专门有时间来学习下.查看到这两种直方图的时候,感觉有点接近,好像又不同,放在这做个比較.大部分还是百科的内容,只是对 ...