asp.net类似百度知道、贴吧的页码显示方法
仔细观察了百度知道、贴吧的分页页码显示方法,整理了下面这个页码显示方法,贴出来共同学习!
显示效果:
可以结合我之前写的“我的net分页方法(三层架构、适用于所有数据控件) ”,在BLL层加上这个静态方法,即可
Code
/// <summary>
/// 分页页码显示方法(类似百度知道、贴吧)
/// </summary>
/// <param name="iPage">当前页码</param>
/// <param name="iPageSize">每页条数</param>
/// <param name="count">数据总数</param>
/// <param name="showNum">显示页标个数</param>
/// <param name="url">拓展URL 如:?xx=1&yy=2&vv=3&</param>
/// <returns></returns>
public static string PageShow(int iPage, int iPageSize, int count, int showNum, string url)
{
url = url == "" ? "?" : url; //默认值 '?'
showNum = showNum < 3 ? 3 : showNum; //默认值 '?'
string reStr = ""; //返回值附初值
int pageCount = count % iPageSize != 0 ? count / iPageSize + 1 : count / iPageSize; //总页数
int n = 1;
int m = showNum;
if (iPage > showNum / 2)
{
n = iPage - (showNum % 2 == 0 ? showNum / 2 - 1 : showNum / 2);
if ((iPage + showNum / 2) <= pageCount)
m = iPage + showNum / 2;
else
m = pageCount;
}
if (pageCount > 1)
{
if (pageCount <= showNum && pageCount > 1)
{
if (iPage != 1)
reStr += " <a href=" + url + "iPage=" + (iPage - 1) + ">上一页</a>";
for (int i = 1; i <= pageCount; i++)
{
if (iPage == i)
reStr += " " + i;
else
reStr += " <a href=" + url + "iPage=" + i + ">[" + i + "]</a> ";
}
if (iPage != pageCount)
reStr += " <a href=" + url + "iPage=" + (iPage + 1) + ">下一页</a>";
}
else
{
if (iPage > showNum / 2)
reStr += " <a href=" + url + "iPage=1>首页</a>";
if (iPage != 1)
reStr += " <a href=" + url + "iPage=" + (iPage - 1) + ">上一页</a>";
for (int i = n; i <= m; i++)
{
if (iPage == i)
reStr += " " + i;
else
reStr += " <a href=" + url + "iPage=" + i + ">[" + i + "]</a> ";
}
if (iPage != pageCount)
reStr += " <a href=" + url + "iPage=" + (iPage + 1) + ">下一页</a>";
if (iPage + showNum / 2 < pageCount)
reStr += " <a href=" + url + "iPage=" + pageCount + ">尾页</a>";
}
}
return reStr;
}
/// <summary>
/// 分页页码显示方法(类似百度知道、贴吧)
/// </summary>
/// <param name="iPage">当前页码</param>
/// <param name="iPageSize">每页条数</param>
/// <param name="count">数据总数</param>
/// <param name="showNum">显示页标个数</param>
/// <param name="url">拓展URL 如:?xx=1&yy=2&vv=3&</param>
/// <returns></returns>
public static string PageShow(int iPage, int iPageSize, int count, int showNum, string url)
{
url = url == "" ? "?" : url; //默认值 '?'
showNum = showNum < 3 ? 3 : showNum; //默认值 '?'
string reStr = ""; //返回值附初值
int pageCount = count % iPageSize != 0 ? count / iPageSize + 1 : count / iPageSize; //总页数
int n = 1;
int m = showNum;
if (iPage > showNum / 2)
{
n = iPage - (showNum % 2 == 0 ? showNum / 2 - 1 : showNum / 2);
if ((iPage + showNum / 2) <= pageCount)
m = iPage + showNum / 2;
else
m = pageCount;
}
if (pageCount > 1)
{
if (pageCount <= showNum && pageCount > 1)
{
if (iPage != 1)
reStr += " <a href=" + url + "iPage=" + (iPage - 1) + ">上一页</a>";
for (int i = 1; i <= pageCount; i++)
{
if (iPage == i)
reStr += " " + i;
else
reStr += " <a href=" + url + "iPage=" + i + ">[" + i + "]</a> ";
}
if (iPage != pageCount)
reStr += " <a href=" + url + "iPage=" + (iPage + 1) + ">下一页</a>";
}
else
{
if (iPage > showNum / 2)
reStr += " <a href=" + url + "iPage=1>首页</a>";
if (iPage != 1)
reStr += " <a href=" + url + "iPage=" + (iPage - 1) + ">上一页</a>";
for (int i = n; i <= m; i++)
{
if (iPage == i)
reStr += " " + i;
else
reStr += " <a href=" + url + "iPage=" + i + ">[" + i + "]</a> ";
}
if (iPage != pageCount)
reStr += " <a href=" + url + "iPage=" + (iPage + 1) + ">下一页</a>";
if (iPage + showNum / 2 < pageCount)
reStr += " <a href=" + url + "iPage=" + pageCount + ">尾页</a>";
}
}
return reStr;
}