using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Web;
namespace CP.Common
{
///
/// 通用分页方法
/// 支持UrlRewrite
///
public class ShowPageList
{
///
/// 通用分页方法
///
/// 每页条数
/// 页码
/// 记录总数
/// 重写规则
///
public static string PageList(int pagesize, int page, int recordcount, string rewriter = null, bool first = true)
{
System.Text.StringBuilder sb = new System.Text.StringBuilder();
if (recordcount == 0)
return "";
int pagecount = 0;
if (recordcount % pagesize == 0)
pagecount = recordcount / pagesize;
else
pagecount = recordcount / pagesize + 1;
if (first)
sb.Append("第一页");
if (page > 1)
sb.Append("上一页");
else
sb.Append("上一页");
///显示1,2,3,4,5的链接
if (pagecount < 5)
{
for (int i = 1; i <= pagecount; i++)
{
if (i != page)
sb.Append("" + i + "");
else
sb.Append("" + i + "");
}
}
else
{
if (page < 5)
{
for (int i = 1; i <= 5; i++)
{
if (i != page)
sb.Append("" + i + "");
else
sb.Append("" + i + "");
}
}
else
{
if (page + 3 <= pagecount)
{
for (int i = page - 2; i < page + 2; i++)
{
if (i != page)
sb.Append("" + i + "");
else
sb.Append("" + i + "");
}
}
else
{
for (int i = pagecount - 2; i <= pagecount; i++)
{
if (i != page)
sb.Append("" + i + "");
else
sb.Append("" + i + "");
}
}
}
}
if (page < pagecount)
sb.Append("下一页");
else
sb.Append("下一页");
if (first)
{
if (pagecount == 0)
sb.Append("末尾页");
else
sb.Append("末尾页");
}
sb.Append(""+page+"/" + pagecount + "");
return sb.ToString();
}
private static string GetLink(int page, string rewriter)
{
//目前只支持如:rewriter="topic-xxxx-{0}.html"的重写
string link = string.Empty;
string url = string.Empty;
if (!string.IsNullOrEmpty(rewriter)) //url重写时
{
if (page == 1)
url = rewriter.Replace("-{0}", "");
else
url = string.Format(rewriter, page);
link = url;
}
else
{
url = HttpContext.Current.Request.RawUrl;
if (url.IndexOf('?') != -1)
{
if (page == 1)
{
link = Regex.Replace(url, @"[\&,\?]p=\d+", "");
}
else
{
if (url.IndexOf("p=") != -1)
link = Regex.Replace(url, @"p=\d+", "p=" + page);
else
link = url + "&p=" + page;
}
}
else
{
link = url + "?p=" + page;
}
}
return link;
}
}
}