asp.net 使用FCK分頁符大文章分頁

來源:互聯網
上載者:User

方法:
/// <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\">&nbsp;</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(內容)

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.