.NET基于分页控件实现真分页功能

转自:http://myroom.javaeye.com/blog/542825

关键字: .net

下面利用分页控件实现分页功能。分页控件下载网址:http://www.webdiyer.com/ 从该网址下载AspNetPager.dll后,在VS2008中在工具箱中,右键 —> 选择项 —> 浏览 找到AspNetPager.dll添加至工具箱中,在工具箱中可以找到下图所示 


数据绑定用Reapter控件
●把两个控件拖拽至Web窗体中(如:test.aspx)。
●AspNetPager控件的属性中可以设置每页显示记录数(如图)。

 
●存储过程中的代码代码如下
Sql代码 复制代码
  1. set ANSI_NULLS ON  
  2. set QUOTED_IDENTIFIER ON  
  3. GO   
  4. -- =============================================   
  5. -- Author:      myroom   
  6. -- Create date: 2009-12-6   
  7. -- Description: 新闻表的分页   
  8. -- =============================================   
  9. ALTER PROCEDURE [dbo].[prceNewsPagerSelectAll]   
  10. @startIndex int,   
  11. @endIndex int  
  12. AS  
  13. BEGIN  
  14.     with temptbl as(   
  15.     select ROW_NUMBER() OVER (ORDER BY id desc)AS Row,* from news   
  16. )   
  17.     SELECT Row,title FROM temptbl where Row between @startIndex and @endIndex   
  18. END  
set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		myroom
-- Create date: 2009-12-6
-- Description:	新闻表的分页
-- =============================================
ALTER PROCEDURE [dbo].[prceNewsPagerSelectAll]
@startIndex int,
@endIndex int
AS
BEGIN
	with temptbl as(
	select ROW_NUMBER() OVER (ORDER BY id desc)AS Row,* from news
)
	SELECT Row,title FROM temptbl where Row between @startIndex and @endIndex
END

●执行该存储过程
Sql代码 复制代码
  1. exec prceNewsPagerSelectAll 1,5  
exec prceNewsPagerSelectAll 1,5
(从news 表中先出第1至第5条记录)查看结果。
●数据访问层中
C#代码 复制代码
  1. //选出新闻表中总记录个数   
  2.        public int NewsRecordCount()    
  3.        {   
  4.            string cmdText="select * from news";   
  5.            int rc = (sqlhelper.ExecuteQuery(cmdText, CommandType.Text)).Rows .Count ;   
  6.            return rc;   
  7.        }   
  8.        //新闻表分页功能   
  9.        public DataTable SelectNewsPager(int startIndex, int endIndex)   
  10.        {   
  11.            DataTable dt = new DataTable();   
  12.            string cmdText = "prceNewsPagerSelectAll";   
  13.            SqlParameter[] paras = new SqlParameter[] {    
  14.                new SqlParameter ("@startIndex",startIndex ),   
  15.                new SqlParameter ("@endIndex",endIndex )   
  16.            };   
  17.            dt = sqlhelper.ExecuteQuery(cmdText, paras, CommandType.StoredProcedure);   
  18.            return dt;   
  19.        }  

●后台代码中(test.aspx.cs)
C#代码 复制代码
  1. protected void Page_Load(object sender, EventArgs e)   
  2. {   
  3.     if (!Page .IsPostBack )   
  4.     {                
  5.          int totalRecord = new NewsDAO().NewsRecordCount();//获取总记录数   
  6.         AspNetPager1.RecordCount = totalRecord;//对AspNetPager属性进行设置   
  7.         databind();   
  8.     }   
  9. }   
  10. //AspNetPager1控件的PageChanged事件   
  11. protected void AspNetPager1_PageChanged(object sender, EventArgs e)   
  12. {   
  13.     databind();   
  14. }   
  15. //数据绑定方法   
  16. private void databind()   
  17. {   
  18.     int startIndex = AspNetPager1.StartRecordIndex;//StartRecordIndex是AspNetPager固有属性   
  19.     int endIndex = AspNetPager1.EndRecordIndex;//EndRecordIndex是AspNetPager固有属性   
  20.     //数据绑定   
  21.     Repeater1.DataSource = new NewsDAO().SelectNewsPager(startIndex, endIndex);   
  22.     Repeater1.DataBind();   
  23. }  

●效果图如下: 

 

●对该控件的属性进行设置还有更多效果: 




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值