自己写的一个长文章分页:
用的时候只要传入三个参数:文章的内容;文章显示页面的链接;每页估计显示的字数;
该方法不会截断HTML的标签。
Code
#region 长文章分页方法
/// <summary>
/// 长文章分页方法
/// </summary>
/// <param name="content">整篇文章的所有内容</param>
/// <param name="url">文章显示页的路径</param>
/// <param name="contenLength">每页预计的字数</param>
/// <returns>每页的内容</returns>
public string ArticlePager(string content, string url, int contenLength)
{
string[] contentArray;
ArrayList newContentArray = new ArrayList();
string perPageContent;
string color = "black";
string pagerBottom = "<br>";
string urlUpPage = "", urlNextPage = "";
System.Text.StringBuilder strBuider = new System.Text.StringBuilder();
content = @"<p>" + content + "<p>";
contentArray = Regex.Split(content, "<p>", RegexOptions.IgnoreCase);
string _tempString = "";
if(content.Length<contenLength)
{
newContentArray.Add(content);
}
else
{
for (int i = 0; i < contentArray.Length; i++)
{
_tempString += contentArray[i];
if (_tempString.Length > contenLength)
{
newContentArray.Add(_tempString);
_tempString = "";
}
}
newContentArray.Add(_tempString);
}
if (newContentArray.Count <= 0)
{
pagerBottom = "";
}
else
{
for (int i = 0; i < newContentArray.Count; i++)
{
int j = i + 1;
string _tempUrl = "";
_tempUrl = url + "page=" + j.ToString();
strBuider.Append("<a href=\"");
strBuider.Append(_tempUrl);
strBuider.Append("\">");
strBuider.Append("[");
strBuider.Append("<font color=\""+color+"\">");
strBuider.Append(j.ToString());
strBuider.Append("</font>");
strBuider.Append("]");
strBuider.Append("</a>");
}
}
if (System.Web.HttpContext.Current.Request.QueryString["page"] == null)
{
urlUpPage = "上一页";
urlNextPage = "下一页";
pagerBottom = urlUpPage + strBuider.ToString() + urlNextPage;
perPageContent = (newContentArray.Count > 0) ? newContentArray[0] + "<center>" + pagerBottom + "</center>" : "<center>文章无内容</center>";
}
else
{
int _up, _next;
int page = int.Parse(System.Web.HttpContext.Current.Request.QueryString["page"]);
_up = page - 1;
_next = page + 1;
urlUpPage = (_up <= 0) ? "无上一页" : "<a href=\"" + url + "page=" + Convert.ToString(_up) + "\">上一页</a>";
urlNextPage = (_next > newContentArray.Count) ? "无下一页" : "<a href=\"" + url + "page=" + Convert.ToString(_next) + "\">下一页</a>";
StringBuilder strBuildI = new StringBuilder();
for (int i = 0; i < newContentArray.Count; i++)
{
int j = i + 1;
color = (page == j) ? "red" : "black";
string _tempUrl = "";
_tempUrl = url + "page=" + j.ToString();
strBuildI.Append("<a href=\"");
strBuildI.Append(_tempUrl);
strBuildI.Append("\">");
strBuildI.Append("[");
strBuildI.Append("<font color=\"" + color + "\">");
strBuildI.Append(j.ToString());
strBuildI.Append("</font>");
strBuildI.Append("]");
strBuildI.Append("</a>");
}
pagerBottom = urlUpPage + strBuildI + urlNextPage;
perPageContent = (newContentArray.Count > 0) ? newContentArray[page - 1] + "<center>" + pagerBottom + "</center>" : "<center>文章无内容</center>";
}
return perPageContent;
}
#endregion