今天刚做完的一个文章内容分页,主要原理是插入特殊字符,然后根据特殊字符进行字符串分解。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
namespace ZS.BLL
{
public class Pager
{
public static string ContentPage(string content,int page)
{
string[] array_content = content.Split(new string[] { "{$:page}" }, StringSplitOptions.None);
int length = array_content.Length;
if (length <= 1)
{
return content;
}
return array_content[page - 1] + MakePage(length,page);
}
public static string MakePage(int length,int page)
{
string url;
int ic = HttpContext.Current.Request.RawUrl.IndexOf("&page");
if (ic > 0)
{
url = HttpContext.Current.Request.RawUrl.Remove(ic);
}
else
{
url = HttpContext.Current.Request.RawUrl;
}
StringBuilder builder = new StringBuilder();
builder.Append("<table id=\"pagelist\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\" align=\"center\" width=\"100%\">");
builder.Append("<tr><td align=\"center\"><ul><li><a href=\"" + url + "\">首页</a></li><li><a href=\"" + url + "&page=" + (page - 1).ToString() + "\">上一页</a></li>");
for (int i = 0; i < length; i++)
{
if (page - 1 == i)
{
builder.Append("<li class=\"current\">" + (i + 1).ToString() + "</li>");
}
else
{
builder.Append("<li><a href=\"" + url + "&page=" + (i + 1).ToString() + "\">" + (i + 1).ToString() + "</a></li>");
}
}
if (page < length)
{
builder.Append("<li><a href=\"" + url + "&page=" + (page + 1).ToString() + "\">下一页</a></li>");
}
else
{
builder.Append("<li><a href=\"" + url + "&page=" + page.ToString() + "\">下一页</a></li>");
}
builder.Append("<li><a href=\"" + url + "&page=" + length.ToString() + "\">尾页</a></li></ul></td></tr></table>");
return builder.ToString();
}
}
}