下面利用分页控件实现分页功能。分页控件下载网址:http://www.webdiyer.com/
从该网址下载AspNetPager.dll后,在VS2008中在工具箱中,右键 —> 选择项 —> 浏览
找到AspNetPager.dll添加至工具箱中,在工具箱中可以找到下图所示
数据绑定用Reapter控件
●把两个控件拖拽至Web窗体中(如:test.aspx)。
●AspNetPager控件的属性中可以设置每页显示记录数(如图)。
●存储过程中的代码代码如下
- set ANSI_NULLS ON
- set QUOTED_IDENTIFIER ON
- GO
-
-
-
-
-
- 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
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
●执行该存储过程
- exec prceNewsPagerSelectAll 1,5
exec prceNewsPagerSelectAll 1,5
(从news
表中先出第1至第5条记录)查看结果。
●数据访问层中
-
- public int NewsRecordCount()
- {
- string cmdText="select * from news";
- int rc = (sqlhelper.ExecuteQuery(cmdText, CommandType.Text)).Rows .Count ;
- return rc;
- }
-
- public DataTable SelectNewsPager(int startIndex, int endIndex)
- {
- DataTable dt = new DataTable();
- string cmdText = "prceNewsPagerSelectAll";
- SqlParameter[] paras = new SqlParameter[] {
- new SqlParameter ("@startIndex",startIndex ),
- new SqlParameter ("@endIndex",endIndex )
- };
- dt = sqlhelper.ExecuteQuery(cmdText, paras, CommandType.StoredProcedure);
- return dt;
- }
//选出新闻表中总记录个数
public int NewsRecordCount()
{
string cmdText="select * from news";
int rc = (sqlhelper.ExecuteQuery(cmdText, CommandType.Text)).Rows .Count ;
return rc;
}
//新闻表分页功能
public DataTable SelectNewsPager(int startIndex, int endIndex)
{
DataTable dt = new DataTable();
string cmdText = "prceNewsPagerSelectAll";
SqlParameter[] paras = new SqlParameter[] {
new SqlParameter ("@startIndex",startIndex ),
new SqlParameter ("@endIndex",endIndex )
};
dt = sqlhelper.ExecuteQuery(cmdText, paras, CommandType.StoredProcedure);
return dt;
}
●后台代码中(test.aspx.cs)
- protected void Page_Load(object sender, EventArgs e)
- {
- if (!Page .IsPostBack )
- {
- int totalRecord = new NewsDAO().NewsRecordCount();
- AspNetPager1.RecordCount = totalRecord;
- databind();
- }
- }
-
- protected void AspNetPager1_PageChanged(object sender, EventArgs e)
- {
- databind();
- }
-
- private void databind()
- {
- int startIndex = AspNetPager1.StartRecordIndex;
- int endIndex = AspNetPager1.EndRecordIndex;
-
- Repeater1.DataSource = new NewsDAO().SelectNewsPager(startIndex, endIndex);
- Repeater1.DataBind();
- }
protected void Page_Load(object sender, EventArgs e)
{
if (!Page .IsPostBack )
{
int totalRecord = new NewsDAO().NewsRecordCount();//获取总记录数
AspNetPager1.RecordCount = totalRecord;//对AspNetPager属性进行设置
databind();
}
}
//AspNetPager1控件的PageChanged事件
protected void AspNetPager1_PageChanged(object sender, EventArgs e)
{
databind();
}
//数据绑定方法
private void databind()
{
int startIndex = AspNetPager1.StartRecordIndex;//StartRecordIndex是AspNetPager固有属性
int endIndex = AspNetPager1.EndRecordIndex;//EndRecordIndex是AspNetPager固有属性
//数据绑定
Repeater1.DataSource = new NewsDAO().SelectNewsPager(startIndex, endIndex);
Repeater1.DataBind();
}
●效果图如下:
●对该控件的属性进行设置还有更多效果:
.NET基于分页控件实现真分页功能,布布扣,bubuko.com
.NET基于分页控件实现真分页功能
原文:http://www.cnblogs.com/wqsbk/p/3570767.html