本文利用PagedDataSource+Repeater控件实现数字分页,
DataList控件也可以通过修改ID名称实现分页。
/// <summary>
/// Repeater分页
/// </summary>
/// <param name="ds">DataSet实例</param>
/// <param name="RepeaterName">Repeater ID名称</param>
/// <param name="pagesize">分页大小</param>
/// <returns></returns>
public static string GetRptPageNum(DataSet ds, Repeater RepeaterName, int pagesize)
{
PagedDataSource pds = new PagedDataSource();
pds.DataSource = ds.Tables[0].DefaultView;
pds.AllowPaging = true;
//得到数据总行数
int total = ds.Tables[0].Rows.Count;
pds.PageSize = pagesize;
//定义索引页
int page;
if (HttpContext.Current.Request.QueryString["page"] != null)
{
page = Convert.ToInt32(HttpContext.Current.Request.QueryString["page"]);
}
else
{
page = 1;
}
//索引从0开始算起,所以-1
pds.CurrentPageIndex = page - 1;
RepeaterName.DataSource = pds;
RepeaterName.DataBind();
int allpage = 0;
int next = 0;
int pre = 0;
int startcount = 0;
int endcount = 0;
string pagestr = "";
if (page < 1)
{
page = 1;
}
//计算总页数
if (pagesize != 0)
{
allpage = (total / pagesize); //总数量除于每页显示量
allpage = ((total % pagesize) != 0 ? allpage + 1 : allpage);
allpage = (allpage == 0 ? 1 : allpage);
}
next = page + 1;
pre = page - 1;
//中间页起始序号
startcount = (page + 5) > allpage ? allpage - 9 : page - 4;
//中间页终止序号
endcount = page < 5 ? 10 : page + 5;
//设置序号从1开始
if (startcount < 1)
{
startcount = 1;
}
//控制页码数在总页码数内
if (allpage < endcount)
{
endcount = allpage;
}
pageurl= "共" + allpage + "页"+ total +"条"+" ";
pageurl+= page > 1 ? "<a href = \"" + HttpContext.Current.Request.CurrentExecutionFilePath + "?page = 1\">首页</a> <a href = \"" + HttpContext.Current.Request.CurrentExecutionFilePath + "?page = " + pre + "\"> 上一页</a>" : "<a>首页</a>" + " " + "<a>上一页</a>";
for (int i = startcount; i <= endcount; i++)
{
pageurl+= page == i ? " " + "<a class=\"cpb\">" + i + "</a>" : " <a href=\"" + HttpContext.Current.Request.CurrentExecutionFilePath + "?page=" + i + "\">" + i + "</a>";
}
pageurl+= page != allpage ? " <a href=\"" + HttpContext.Current.Request.CurrentExecutionFilePath + "?page=" + next + "\">下一页</a> <a href=\"" + HttpContext.Current.Request.CurrentExecutionFilePath + "?page=" + allpage + "\">末页</a>" : " " + "<a >下一页</a>" + " " + "<a >末页</a>";
return pageurl;
}
其他分页需求及美化可以根据自己的需要去改,如下效果图!