程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> .NET實例教程 >> asp.net 大文章分頁代碼(c#)

asp.net 大文章分頁代碼(c#)

編輯:.NET實例教程

這幾天閒 來無事,所以寫了個大文章分頁代碼,下面提供給大家。
實現思路是:後台管理員文章發布的時候自己手動添加分頁符,後台我用的也是FCKeditor,所以修改了他的source增加了自己的button。在button的事件裡寫上自己的代碼,當然就是直接insert innerHtml自己的分頁標志了,我目前用的是|,|,|,|,|,|當然可以自定義其他的。首先提供個類庫。
1 ContentPager .cs
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.ComponentModel;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Web.SessionState;

/// <summary>
/// ContentPager 的摘要說明
/// </summary>
public class ContentPager : System.Web.UI.Page
{

    private System.Web.UI.WebControls.HyperLink lnknext;
    private System.Web.UI.WebControls.HyperLink lnkprev;
    private System.Web.UI.WebControls.HyperLink lnkfist;
    private System.Web.UI.WebControls.HyperLink lnklast;
    private System.Web.UI.WebControls.Label pagenum;
    private System.Web.UI.WebControls.Label pagecount;
    private System.Web.UI.WebControls.Label displaycontent;

    public ContentPager()
    {
        //
        // TODO: 在此處添加構造函數邏輯
        //
    }

    public void datapager(string content,string query)
    {
        string PagerSplit = System.Configuration.ConfigurationManager.APPSettings["PagerSplit"];//從web.config中獲取分頁符,當然這裡可以自己定義,為了方便還是放到配置文件或者數據庫中。
        string[] contentArray = StringSplit(content, PagerSplit);//這裡是一個方法,摘自網上

        int PageCount = contentArray.Length;
        this.pagecount.Text = "共<b><font color=''#FF0000''>" + PageCount.ToString() + "</font></b>頁";
        int CurPage;

        //確定當前頁數
        if (System.Web.HttpContext.Current.Request.Params["Page"] != null)
        {
            CurPage = Convert.ToInt32(System.Web.HttpContext.Current.Request.Params["Page"]);
        }
        else
        {
            CurPage = 1;
        }

        if (CurPage < 1) CurPage = 1;
        if (Convert.ToInt32(System.Web.HttpContext.Current.Request.Params["Page"]) > PageCount)
        {
            CurPage = PageCount;
        }

        //顯示當前頁數
        this.pagenum.Text = "第<b><font color=''#FF0000''>" + CurPage.ToString() + "</font></b>頁";
        //下一頁
        if (CurPage != PageCount)
        {
            lnknext.NavigateUrl = System.Web.HttpContext.Current.Request.CurrentExecutionFilePath + "?Page=" + Convert.ToString(CurPage + 1) + query;
        }
        //上一頁
        if (CurPage > 1)
        {
            lnkprev.NavigateUrl = System.Web.HttpContext.Current.Request.CurrentExecutionFilePath + "?Page=" + Convert.ToString(CurPage - 1) + query;
        }

        //第一頁
 &nbsp;      if (CurPage != 1)
        {
            lnkfist.NavigateUrl = System.Web.HttpContext.Current.Request.CurrentExecutionFilePath + "?Page=" + Convert.ToString(1) + query;
        }

        //最後一頁
        if (CurPage != PageCount)
        {
            lnklast.NavigateUrl = System.Web.HttpContext.Current.Request.CurrentExecutionFilePath + "?Page=" + Convert.ToString(PageCount) + query;
        }

        displaycontent.Text = contentArray[CurPage - 1].ToString();
    }

 

    #region  System.Web.UI.WebControls.HyperLink DisplayContent
    public System.Web.UI.WebControls.Label DisplayContent
    {
        get
        {
            return this.DisplayContent;
        }
        set
        {
            this.displaycontent = value;
        }
    }
    #endregion

    #region  System.Web.UI.WebControls.HyperLink Lnkprev
    public System.Web.UI.WebControls.HyperLink Lnkprev
    {
        get
        {
            return this.lnkprev;
        }
        set
        {
       &nbsp;    this.lnkprev = value;
        }
    }
    #endregion

    #region System.Web.UI.WebControls.HyperLink Lnknext
    public System.Web.UI.WebControls.HyperLink Lnknext
    {
        get
        {
            return this.lnknext;
        }
        set
        {
            this.lnknext = value;
        }
    }
    #endregion

    #region  System.Web.UI.WebControls.HyperLink Lnkfist
    public System.Web.UI.WebControls.HyperLink Lnkfist
    {
        get
        {
            return this.lnkfist;
        }
        set
        {
            this.lnkfist = value;
        }
    }
    #endregion

    #region  System.Web.UI.WebControls.HyperLink Lnklast
    public System.Web.UI.WebControls.HyperLink Lnklast
    {
        get
        {
            return this.lnklast;
        }
        set
        {
            this.lnklast = value;
        }
    }


    #endregion

    #region  System.Web.UI.WebControls.Label Pagenum
    public System.Web.UI.WebControls.Label Pagenum
    {
        get
        {
            return this.pagenum;
        }
        set
        {
            this.pagenum = value;
        }
    }
    #endregion

    #region System.Web.UI.WebControls.Label Pagecount
    public System.Web.UI.WebControls.Label Pagecount
    {
        get
        {
            return this.pagecount;
        }
        set
        {
            this.pagecount = value;
        }
    }
    #endregion

   #region 將字符串分割成數組
        /// <summary>
        /// 將字符串分割成數組
        /// </summary>
        /// <param name="strSource"></param>
        /// <param name="strSplit"></param>
        /// <returns></returns>
     &nbsp;  public string[] StringSplit(string strSource, string strSplit)
        {
            string[] strtmp = new string[1];

&nbsp;           int index = strSource.IndexOf(strSplit, 0);
            if (index < 0)
            {
                strtmp[0] = strSource;
                return strtmp;
            }
            else
            {
                strtmp[0] = strSource.Substring(0, index);
                return StringSplit(strSource.Substring(index + strSplit.Length), strSplit, strtmp);
            }
        }
        #endregion

        #region 采用遞歸將字符串分割成數組
        /// <summary>
        /// 采用遞歸將字符串分割成數組
        /// </summary>
        /// <param name="strSource"></param>
        /// <param name="strSplit"></param>
        /// <param name="attachArray"></param>
        /// <returns></returns>
        private string[] StringSplit(string strSource, string strSplit, string[] attachArray)
        {
            string[] strtmp = new string[attachArray.Length + 1];
            attachArray.CopyTo(strtmp, 0);            int index = strSource.IndexOf(strSplit, 0);
            if (index < 0)
            {
                strtmp[attachArray.Length] = strSource;
                return strtmp;
            }
            else
            {
                strtmp[attachArray.Length] = strSource.Substring(0, index);
                return StringSplit(strSource.Substring(index + strSplit.Length), strSplit, strtmp);
            }
        }
        #endregion
}
2 調用部分:
在ASPx中增加lable等元素。
 <asp:label id="currentpage" runat="server" CSSClass="gray">當前頁</ASP:label>&nbsp;&nbsp;
                                        <asp:label id="totalpage" runat="server" CSSClass="gray">Label</ASP:label>&nbsp;&nbsp;
             <asp:hyperlink id="lnkfist" runat="server" CSSClass="gray">首頁</ASP:hyperlink>|
             <asp:hyperlink id="lnkprev" runat="server" CSSClass="gray">前頁</ASP:hyperlink>|
          &nbsp;  <asp:hyperlink id="lnknext" runat="server" CSSClass="gray">下頁</ASP:hyperlink>|
             <asp:hyperlink id="lnklast" runat="server" CSSClass="gray">尾頁</ASP:hyperlink>
在ASPx.cs中增加調用代碼。
 private ContentPager pager;

 protected void Page_Load(object sender, EventArgs e)
    {
        int id = Convert.ToInt32(Request.QueryString["id"]);//接收參數,例如訪問這個頁是xxx.ASPx?id=1        DataSet ds = new DataSet();

        //從數據庫讀內容部分就略掉了

        DataRow drow;
        drow = ds.Tables[0].Rows[0];
        string cont = drow["content"].ToString();//此部分可以把內容中的Html代碼轉換一下,我也省略了
      
       //文章分頁,關鍵部分
        pager = new ContentPager();

        pager.Lnknext = this.lnknext;
        pager.Lnkprev = this.lnkprev;
        pager.Lnklast = this.lnklast;
        pager.Lnkfist = this.lnkfist;
        pager.Pagenum = this.currentpage;
        pager.Pagecount = this.totalpage;
        pager.DisplayContent = this.content;

        string query = "&id=" + Request.QueryString["id"].ToString();
        pager.datapager(cont, query);
}

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved