方法:
/// <summary>
/// 文章分页函数
/// </summary>
/// <param name="content">文章内容</param>
/// <param name="currentPage">当前页码</param>
/// <param name="pageUrl">当前页面地址</param>
protected String[] ArticlePage(string content, int currentPage, string pageUrl)
{
String[] result = new String[2];
int pageCount = 0;//页数
content = content.Replace("<div style=\"page-break-after: always\"><span style=\"display: none\"> </span></div>", "[--page--]");//FCK在IE中生成的默认分页符,替换为自定义分页符
string[] tempContent = System.Text.RegularExpressions.Regex.Split(content, "\\u005B--page--]"); //取得分页符 "\\u005B"为"["的转义
pageCount = tempContent.Length;
string outputContent = "";//要输出的内容
if (pageCount <= 1)
{
outputContent = content; //文章内容
result[0] = String.Empty;
}
else
{
string pageStr = "";//分页字符串
if (currentPage != 1)
{
//如果没有传参数的话,将"&cp="改成"?cp="
pageStr += "<a class='prev' href =" + pageUrl + "&cp=" + (currentPage - 1) + "><<上一页</a>";
}
for (int i = 1; i <= pageCount; i++)
{
if (i == currentPage)
pageStr += ("<span class='active'>[" + i + "]</span>");
else
{
//如果没有传参数的话,将"&cp="改成"?cp="
pageStr += ("<a class='num' href =" + pageUrl + "&cp=" + i + ">[" + i + "]</a>");
}
}
if (currentPage != pageCount)
{
//如果没有传参数的话,将"&cp="改成"?cp="
pageStr += "<a class='next' href =" + pageUrl + "&cp=" + (currentPage + 1) + ">下一页>></a>";
}
result[0] = pageStr;
outputContent = tempContent[currentPage - 1].ToString();
}
result[1] = outputContent;
return result;
}
//调用
public partial class jxjl : System.Web.UI.Page
{
SqlConnection conn = new SqlConnection(sqlHelper.conn);
SqlCommand cmd;
SqlDataReader dr;
String[] ree = new String[2];
int cpage = 1;
string purl = "";
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
if (Request.QueryString["id"] != null)
{
//分页地址
purl="jxjl.aspx?id="+Request.QueryString["id"]+"";
sel();
}
else
{
//传到错误页面
//bind();
}
}
}
private void sel()
{
try
{
if (conn.State == ConnectionState.Closed)
{
conn.Open();
}
string code = "select title,content from zj_Article where id='" + Request.QueryString["id"].ToString() + "'";
cmd = new SqlCommand(code, conn);
cmd.CommandTimeout = 200;
dr = cmd.ExecuteReader();
if (dr.HasRows)
{
while (dr.Read())
{
if (Request.QueryString["cp"] != null)
{
cpage = Convert.ToInt32(Request.QueryString["cp"].ToString());
}
ree = ArticlePage(dr["content"].ToString(), cpage, purl);
//页码显示 内容
this.labPage.Text = ree[1]; this.labContent.Text = ree[0];
}
}
}
finally
{
if (conn.State == ConnectionState.Open)
{
//dr.Close();
conn.Close();
}
}
}
}
}
//前台添加两个label
分别是labPage(页码) labContent(内容)