因项目需要,需要对长文章进行分页,下面的方法是使用插入分页符进行分页,稍微改造了一下一个编辑器,给编辑器加了一个插入分页按钮工具,点击后在光标当前位置添加"[xingye_page]",此方法非常简单,只是根据插入的特定分页符进行分页,将数据库中读取的某条文章记录赋给string,再对该变量进行运算即可。另自动分页功能暂未添加。这里使用了一个label控件作为分页标识,
public string OutArticlePage(string strContent)
{
string strRet = "";
int intCurrentPage = 1;//初始页第第一页
int intTotalPage = 0; //总页数
if (strContent.IndexOf("[xingye_page]", 0) >= 0)//判断是否存在分页符
{
string[] strm_Content = Regex.Split(strContent, @"\[xingye_page\]",RegexOptions.IgnoreCase);
intTotalPage = strm_Content.Length;
if (Request["pages"] != null)
{
try
{
intCurrentPage = Convert.ToInt32(Request["pages"].ToString());
if (intCurrentPage > intTotalPage)
intCurrentPage = intTotalPage;
}
catch
{
intCurrentPage = intCurrentPage;
}
}
strRet = GetHighLightKey(strm_Content[intCurrentPage - 1].ToString());//取出当前页
string strInfoid = Request["id"].ToString();
//获取参数
string strPageHtml, strParse, strNext;
string m_strPageInfo = "";
strPageHtml = "<div style=\"text-align:center\" class=\"manu\" ><p></p>";
if (intCurrentPage == 1)//当前为第一页
{
strParse = "<a disabled=\"true\">前一页</a>";
strNext = "<a href=\"Browse.aspx?id=" + strInfoid + "&pages=2\">后一页</a>";
}
else if (intCurrentPage == intTotalPage)//当前为最后一页
{
strParse = "<a href=\"Browse.aspx?id=" + strInfoid + "&pages=" + (intCurrentPage - 1) + "\">前一页</a>";
strNext = "<a disabled=\"true\">后一页</a>";
}
else
{
strParse = "<a href=\"Browse.aspx?id=" + strInfoid + "&pages=" + (intCurrentPage - 1) + "\">前一页</a>";
strNext = "<a href=\"Browse.aspx?id=" + strInfoid + "&pages=" + (intCurrentPage + 1) + "\">后一页</a>";
}
for (int i = 1; i <= intTotalPage; i++)
{
if (i == intCurrentPage)
{
m_strPageInfo += " <b>" + i + "</b> ";
}
else
{
m_strPageInfo += "
<a href=Browse.aspx?id=" + Request["id"].ToString() + "&pages=" + i + ">" + i + "</a> ";
}
}
//输出显示各个页码
this.labPageNumber.Text = strPageHtml + strParse + m_strPageInfo + strNext + "</div>";
}
else
{
strRet = strContent;
}
return strRet;
}
本文来源于:C# .net学习之旅 http://www. 原文地址:http://www.