长文章分页代码类

提供一个重载,默认有一个分页符,分割采用正则表达式,样式名是page,或者自己改。

需引入using System.Text.RegularExpressions;

    /// <summary>
/// 长文章分页
/// </summary>
public class SplictPage
{
/// <summary>
/// 长文章分页
/// </summary>
/// <param name="content">欲分割的内容</param>
/// <param name="pageingFlag">分割标识符</param>
/// <param name="sprarm">传递的参数,如:newsid=1</param>
/// <returns></returns>
public static string bind(string content,string pageingFlag, string sprarm)
{
string thisnr = "";
string thispage = "";
int currpagenum;

string[] temp = Regex.Split(content, pageingFlag, RegexOptions.IgnoreCase);

try
{
currpagenum = System.Convert.ToInt32(System.Web.HttpContext.Current.Request.QueryString["page"]);
if (currpagenum <= 0)
{
currpagenum = 1;
}

if (currpagenum > temp.Length)
{
currpagenum = temp.Length;
}
}
catch
{
return "error";
}

if (System.Web.HttpContext.Current.Request.QueryString["page"] == null)
{
thisnr = temp[0];
}
else
{
thisnr = temp[currpagenum - 1];
}

if (temp.Length > 1)
{
thispage = "<div class=\"page\"><a href=\"?" + sprarm + "&page=1\" style=\"auto;border:0;\">首页</a>";
thispage += "<a href=\"?" + sprarm + "&page=" + (currpagenum - 1) + "\" style=\"auto;border:0;\">上一页</a>";
for (int i = 0; i <= temp.Length - 1; i++)
{
if (i == currpagenum - 1)
{
thispage += "<font>" + (i + 1) + "</font>";
}
else
{
thispage += "<a href=?" + sprarm + "&page=" + (i + 1) + ">" + (i + 1) + "</a>";
}
}
thispage += "<a href=\"?" + sprarm + "&page=" + (currpagenum + 1) + "\" style=\"auto;border:0;\">下一页</a>";
thispage += "<a href=\"?" + sprarm + "&page=" + temp.Length + "\" style=\"auto;border:0;\">末页</a></div>";
}
return thisnr + thispage;
}

/// <summary>
/// 长文章分页,默认分页标识:<div>\\$HengCmsPage\\$</div>
/// </summary>
/// <param name="content">欲分割的内容</param>
/// <param name="sprarm">传递的参数,如:newsid=1</param>
/// <returns></returns>
public static string bind(string content, string sprarm)
{
return bind(content, "<div>\\$HengCmsPage\\$</div>", sprarm);
}
}
原文地址:https://www.cnblogs.com/superfeeling/p/2346165.html