方法:
/// <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(內容)