ASP.NET使用FCKEditor的分页符进行文章分页

2012 年 9 月 16 日8150

前面有篇文章介绍了“asp.net长文章采用分隔符进行分页”,但是如果分页中含有html标记还是很容易出现问题的,比如在FCK中插入"[--page--]"(自定义的分页符)后,往往会把成对的<div></div>或<p></p>标记隔开导致网页布局混乱。不料FCK自带插入分页符功能,使用FCKeditor中的分页符后就可以完美解决长文章的分页问题了。

分页代码如下:
/// <summary>
///文章分页函数
/// </summary>
/// <param name="content">文章内容</param>
/// <param name="currentPage">当前页码</param>
/// <param name="pageUrl">当前页面地址</param>
protected void ArticlePage(string content, int currentPage, string pageUrl)
{
int pageCount = 0;//页数
content = content.Replace("<div style=\"page-break-after: always\"><span style=\"display: none\">&nbsp;</span></div>", "[--page--]");//FCK在IE中生成的默认分页符,替换为自定义分页符
content = content.Replace("<div style=\"page-break-after: always;\"><span style=\"display: none;\">&nbsp;</span></div>", "[--page--]");//FCK在FF中生成的默认分页符,替换为自定义分页符
string[] tempContent = System.Text.RegularExpressions.Regex.Split(content, "\\u005B--page--]"); //取得分页符 "\\u005B"为"["的转义
pageCount = tempContent.Length;
string outputContent = "";//要输出的内容

if (pageCount <= 1)
{
outputContent = content; //文章内容
this.pnlPage.Visible = false;
}
else
{
string pageStr = "";//分页字符串
pageStr += "共 <span>" + pageCount + "</span> 页&nbsp;&nbsp;";

if (currentPage != 1)
{
pageStr += "<a href =" + pageUrl + "?page=" + (currentPage - 1) + ">上一页</a>";
}

for (int i = 1; i <= pageCount; i++)
{
if (i == currentPage)
pageStr += ("<span>" + i + "</span>");
else
pageStr += ("<a href =" + pageUrl + "?page=" + i + ">" + i + "</a>");
}

if (currentPage != pageCount)
{
pageStr += "<a href =" + pageUrl + "?page=" + (currentPage + 1) + ">下一页</a>";
}
this.ltlPage.Text = pageStr;
outputContent = tempContent[currentPage - 1].ToString();
}
this.ltlContent.Text = outputContent;
}

调用代码举例:ArticlePage(strContent, page, "News_Detail_" + intid + ".html");//分页输出

0 0