类似baidu google分页器效果的代码(修改于 kwklover 同学基础上)
补充:基于JSON的ajax无刷新分页已经做好,请查阅 http://www.cnblogs.com/zhouzhaokun/archive/2011/08/24/2151866.html
今天本来要做基于json的Ajax分页效果。一大早起来就上园子找菜来吃。。刚好看到 kwklover 同学缩写的 分页器 实现。原文地址:http://www.cnblogs.com/kwklover/archive/2006/04/21/381832.html 我在他的基础上做了些修改,但是仍然不是非常完善!代码如下:
//http://www.cnblogs.com/kwklover/archive/2006/04/21/381832.html
protected void Page_Load(object sender, EventArgs e)
{
if (Request["page"] == null || Request["page"] == "")
BuildPager(10000, 1, 10);
else
BuildPager(10000, Convert.ToInt32(Request["page"]), 10);
}
/// <summary>
/// 创建并显示分页器
/// </summary>
/// <param name="totalRecords">总页数</param>
/// <param name="currentPage">当前页码</param>
/// <param name="pageSize">页面大小</param>
private void BuildPager(int totalRecords, int currentPage, int pageSize)
{
int alter = 4;
int startPage = 1;
int endPage = currentPage + alter;
int totalPages = this.CalculateTotalPages(totalRecords, pageSize);
if (currentPage > alter)
{
startPage = currentPage - alter;
}
if (endPage > totalPages)
{
endPage = totalPages;
}
string strTemp = @"<a title='{0}' href='Default.aspx?page={1}'>{2}</a> ";
StringBuilder sb = new StringBuilder("");
if (currentPage != startPage)
{
if (currentPage > startPage)
{
sb.Append(string.Format(strTemp, 1, 1, "首页"));
sb.Append(string.Format(strTemp, currentPage - 1, currentPage - 1, "上一页"));
}
}
for (int i = startPage; i <= endPage; i++)
{
if (currentPage == i)
{
sb.Append("<font color=red>" + i + "</font> ");
}
else
{
sb.Append(string.Format(strTemp, i, i, "[" + i + "]"));
}
}
if (currentPage != endPage)
{
sb.Append(string.Format(strTemp, currentPage + 1, currentPage + 1, "下一页"));
sb.Append(string.Format(strTemp, totalPages, totalPages, "尾页"));
}
this.ltlShowPager.Text = sb.ToString();
}
/**/
/// <summary>
/// 计算总页数
/// </summary>
/// <param name="totalRecords">总记录数</param>
/// <param name="pageSize">每页记录数</param>
private int CalculateTotalPages(int totalRecords, int pageSize)
{
int totalPagesAvailable;
totalPagesAvailable = totalRecords / pageSize;
//由于C#的整形除法 会把所有余数舍入为0,所以需要判断是否需要加1
if ((totalRecords % pageSize) > 0)
totalPagesAvailable++;
return totalPagesAvailable;
}
先贴上分页器代码。稍后我会上传 基于json 的ajax 分页。。。高手勿喷,:-)。。。欢迎交流
浙公网安备 33010602011771号