asp.net实现简单分页实例
程序员文章站
2024-02-15 20:48:04
本文实例讲述了asp.net实现简单分页的方法。分享给大家供大家参考。
具体实现方法如下:
复制代码 代码如下:///
/// 分页内...
本文实例讲述了asp.net实现简单分页的方法。分享给大家供大家参考。
具体实现方法如下:
复制代码 代码如下:
/// <summary>
/// 分页内容
/// </summary>
/// <param name="size">页面大小</param>
/// <param name="count">页面数量</param>
/// <param name="currendindex">当前页</param>
/// <param name="pattern">url模式:demo.aspx?page={0}</param>
/// <param name="target">窗口模式</param>
/// <returns></returns>
public static string get_pagenation(int size,
int count,
int currendindex,
string pattern,
string target)
{
//1>打开窗口目标
target = string.isnullorempty(target) ? "_top" : target;
//2>总页数
int pagecount = count / size;
pagecount = pagecount * size == count ? pagecount : pagecount + 1;
//3>分页内容
stringbuilder strhtml = new stringbuilder();
strhtml.append("<span class='pagenation'>");
#region 首部处理
if (currendindex > 1)
{
strhtml.appendformat("<a href='1' target='{0}'>[首页]</a>", target);
strhtml.appendformat("<a href='{0}' target='{1}'>[上一页]</a>", string.format(pattern, currendindex - 1), target);
}
else
{
strhtml.append("<span class='disabled'>[首页]</span> <span class='disabled'>[上一页]</span>");
}
#endregion
#region 中间部分
int i = 1;
int right = (currendindex + 4) > pagecount ? pagecount : currendindex + 4;
if (currendindex > 6)
{
i = currendindex - 5;
}
else
{
right = pagecount >= 10 ? 10 : pagecount;
}
for (; i <= right; i++)
{
if (i == currendindex)
{
strhtml.appendformat("<font class='current'>{0}</font>", i);
strhtml.appendline();
continue;
}
strhtml.appendformat("<a href='{0}' target='{1}'>[{2}]</a>", string.format(pattern, i), target, i);
strhtml.appendline();
}
#endregion
#region 尾部处理
if (currendindex == pagecount)
{
strhtml.append("<span class='disabled'>[下一页]</span><span class='disabled'>[末页]</span>");
strhtml.appendformat("总共({0})页", pagecount);
}
else
{
strhtml.appendformat("<a href='{0}' target='{1}'>[下一页]</a>", string.format(pattern, currendindex + 1), target);
strhtml.appendformat("<a href='{0}' target='{1}'>[末页]</a>", string.format(pattern, pagecount), target);
strhtml.appendformat(" <label>总共({0})页</label>", pagecount);
}
#endregion
strhtml.append("</span>");
return strhtml.tostring();
}
/// 分页内容
/// </summary>
/// <param name="size">页面大小</param>
/// <param name="count">页面数量</param>
/// <param name="currendindex">当前页</param>
/// <param name="pattern">url模式:demo.aspx?page={0}</param>
/// <param name="target">窗口模式</param>
/// <returns></returns>
public static string get_pagenation(int size,
int count,
int currendindex,
string pattern,
string target)
{
//1>打开窗口目标
target = string.isnullorempty(target) ? "_top" : target;
//2>总页数
int pagecount = count / size;
pagecount = pagecount * size == count ? pagecount : pagecount + 1;
//3>分页内容
stringbuilder strhtml = new stringbuilder();
strhtml.append("<span class='pagenation'>");
#region 首部处理
if (currendindex > 1)
{
strhtml.appendformat("<a href='1' target='{0}'>[首页]</a>", target);
strhtml.appendformat("<a href='{0}' target='{1}'>[上一页]</a>", string.format(pattern, currendindex - 1), target);
}
else
{
strhtml.append("<span class='disabled'>[首页]</span> <span class='disabled'>[上一页]</span>");
}
#endregion
#region 中间部分
int i = 1;
int right = (currendindex + 4) > pagecount ? pagecount : currendindex + 4;
if (currendindex > 6)
{
i = currendindex - 5;
}
else
{
right = pagecount >= 10 ? 10 : pagecount;
}
for (; i <= right; i++)
{
if (i == currendindex)
{
strhtml.appendformat("<font class='current'>{0}</font>", i);
strhtml.appendline();
continue;
}
strhtml.appendformat("<a href='{0}' target='{1}'>[{2}]</a>", string.format(pattern, i), target, i);
strhtml.appendline();
}
#endregion
#region 尾部处理
if (currendindex == pagecount)
{
strhtml.append("<span class='disabled'>[下一页]</span><span class='disabled'>[末页]</span>");
strhtml.appendformat("总共({0})页", pagecount);
}
else
{
strhtml.appendformat("<a href='{0}' target='{1}'>[下一页]</a>", string.format(pattern, currendindex + 1), target);
strhtml.appendformat("<a href='{0}' target='{1}'>[末页]</a>", string.format(pattern, pagecount), target);
strhtml.appendformat(" <label>总共({0})页</label>", pagecount);
}
#endregion
strhtml.append("</span>");
return strhtml.tostring();
}
希望本文所述对大家的asp.net程序设计有所帮助。