0)需要先搜索AspNetPager.dll(分页控件), SqlHelper.cs(微软编写的数据库访问类,可以简化访问sql数据库代码),不用SqlHelper.cs自己写访问sql数据库代码比较长.
1)网站项目引用AspNetPager.dll,
2)控件工具箱添加一个选项卡,右键选择项,选择AspNetPager.dll
3)拖动一个repeat控件(也可以是其它的数据绑定控件)到aspx页面,在repeat控件下方拖动一个AspNetPager分页控件,利用该控件生成一个分页存储过程如:pagedSP,
在上图中点生成存储过程并复制到剪贴板,在sql管理器中选中相关的数据库表,新建一个查询,粘贴一下,出现下面的代码,按F5运行.
create procedure [pagedSP]
(@startIndex int,
@endIndex int)
as
begin
with temptbl as (
SELECT ROW_NUMBER() OVER (ORDER BY 流水号desc)AS Row, * from 新闻信息)
SELECT * FROM temptbl where Row between @startIndex and @endIndex
end
写一个得到总页数的存储过程P_GetNewsNumber 代码
create procedure P_GetNewsNumber
as
select count(1) from 新闻信息
,在源视图手工写repeat控件的模板.
4.cs页面代码:
using Wuqi.Webdiyer;//引入分页控件命名空间
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
int total = (int)SqlHelper.ExecuteScalar(CommandType.StoredProcedure, "P_GetNewsNumber");
AspNetPager1.RecordCount = total;
}
}
void bindData()
{
Repeater1.DataSource = SqlHelper.ExecuteReader(CommandType.StoredProcedure, "pagedSP",
new SqlParameter("@startIndex", AspNetPager1.StartRecordIndex),
new SqlParameter("@endIndex", AspNetPager1.EndRecordIndex));
Repeater1.DataBind();
}
protected void AspNetPager1_PageChanged(object src, EventArgs e)
{
bindData();
}