DotNET 长文章分页方法

长文章分页方法实现
本文介绍了一种长文章分页的方法,通过传入文章内容、显示页面的链接及每页预估字数来实现分页功能。此方法能够确保HTML标签的完整性,适用于网页上的文章分页展示。

 

自己写的一个长文章分页:

用的时候只要传入三个参数:文章的内容;文章显示页面的链接;每页估计显示的字数;

该方法不会截断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

转载于:https://www.cnblogs.com/shineqiujuan/archive/2008/11/13/1332984.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值