文章分页之两种方法

分页函数的类之方法一:  根据每页多少字分页

public class ArticlePageBase
{
   public static HttpRequest Request;
    public static string page;
 public ArticlePageBase()
 {
  
 }

//此处的Request是前一个页面提交过来的

    public static string OutputBySize(string p_strContent,HttpRequest Request,int id)//分页函数
    {
        string m_strRet = "";
        int m_intPageSize = 100;//文章每页大小
        int m_intCurrentPage = 1;//设置第一页为初始页
        int m_intTotalPage = 0;
        int m_intArticlelength = p_strContent.Length;//文章长度
        if (m_intPageSize < m_intArticlelength)
        {//如果每页大小大于文章长度时就不用分页了
            if (m_intArticlelength % m_intPageSize == 0)
            {//set total pages count
                m_intTotalPage = m_intArticlelength / m_intPageSize;
            }
            else
            {//if the totalsize
                m_intTotalPage = m_intArticlelength / m_intPageSize + 1;
            }
            if (Request.QueryString["ps"] != null)
            {//set Current page number
                try
                {//处理不正常的地址栏的值
                    m_intCurrentPage = Convert.ToInt32(Request.QueryString["ps"]);
                    if (m_intCurrentPage > m_intTotalPage)

                        m_intCurrentPage = m_intTotalPage;

                }
                catch
                {
                    //m_intCurrentPage = m_intCurrentPage;
                }
            }
            //set the page content 设置获取当前页的大小
            if (m_intCurrentPage < m_intTotalPage)
            {
                m_intPageSize = m_intCurrentPage < m_intTotalPage ? m_intPageSize : (m_intArticlelength - m_intPageSize * (m_intCurrentPage - 1));
                m_strRet += p_strContent.Substring(m_intPageSize * (m_intCurrentPage - 1), m_intPageSize);
            }
            else if (m_intCurrentPage == m_intTotalPage)
            {
                int mm_intPageSize = m_intArticlelength - m_intPageSize * (m_intCurrentPage - 1);
                m_strRet += p_strContent.Substring(m_intArticlelength - mm_intPageSize);
            }

            string m_strPageInfo = "";
            for (int i = 1; i <= m_intTotalPage; i++)
            {
                if (i == m_intCurrentPage)
                    m_strPageInfo += "[" + i + "]";
                else
                    m_strPageInfo += " <a href=?ps=" + i + ">[" + i + "] </a> ";

            }
            if (m_intCurrentPage > 1)
                m_strPageInfo = " <a href=?ps=" + (m_intCurrentPage - 1) + ">上一页 </a>" + m_strPageInfo;
            if (m_intCurrentPage < m_intTotalPage)
                m_strPageInfo += " <a href=?ps=" + (m_intCurrentPage + 1) + ">下一页 </a>";
            //输出显示各个页码
            //this.ShowPageNumber.Text = " <p> </p>" + m_strPageInfo;
            page = " <p> </p>" + m_strPageInfo;

        }
        else
        {
            m_strRet += p_strContent;
        }
        return m_strRet;
    }
}

 

后台调用:

 protected void Page_Load(object sender, EventArgs e)
 {

       

        int id =int.Parse(Request.QueryString["myid"].ToString());

        DataSet ds = ExamBLL.SelectConetxtByExaminationId(id);

        string str = ds.Tables[0].Rows[0]["context"].ToString();

        Label1.Text = ArticlePageBase.OutputBySize(str,Request,id);
        ShowPageNumber.Text = ArticlePageBase.page;

}

 

前台代码:

<div>
    <asp:Label ID="Label1" runat="server" Text="Label"> </asp:Label>
    <asp:Label ID="ShowPageNumber" Font-Size="14px" runat="server"> </asp:Label> 
</div>

 

 

 

根据分隔符分页:

  #region  分页开始
            int page = 1;//初始页面代码,一打开显示第一页内容
            string mypage = "1";
            if (Request.QueryString["page"] != null)
            {
                mypage = Request.QueryString["page"];
            }
            if (mypage != null) //第一次的时候没有加载分页因此得到的结果为NULL,因此要判断一下
            {
                page = Convert.ToInt32(mypage);
            }


            string content = ds.Tables[0].Rows[0]["News_Context"].ToString();
            //以上内容可以从数据库中取得,这里为方便演示直接写上内容,用<P>来间隔要取得的内容

            string[] strContent = null;

            strContent = filesplit(content);//调用filesplit方法取得数组形式的内容

            if (strContent[page - 1] != null)
            {

                this.Label1.Text = strContent[page - 1].ToString();//显示内容
            }
            else
            {
                this.Label1.Text = "无内容";

            }
            string adpager = string.Empty;

            if (int.Parse(mypage) > 1)
            {
                int qianpage = int.Parse(mypage) - 1;
                adpager += " <a href=ZiXunDetails.aspx?Id=" + id +  "&page=" + qianpage + ">上一页 </a>";
            }


            for (int i = 0; i < strContent.Length; i++)
            {  //循环数组取得内容分页数
                int npage = 0;
                if (strContent[i] != null)
                {
                    npage = i + 1;
                    x = i + 1;
                    adpager += "<a href=ZiXunDetails.aspx?Id=" + id +  "&page=" + npage + ">" + npage + "</a>&nbsp;";


                }
            }
            if (int.Parse(mypage) < x)//strContent.Length)
            {
                int xiapage = int.Parse(mypage) + 1;

                adpager += " <a href=ZiXunDetails.aspx?Id=" + id + "&page=" + xiapage + ">下一页 </a>";

            }
            this.ShowPageNumber.Text = adpager.ToString();//显示分页


            #endregion  分页结尾

 

 

调用的方法:

 #region   分隔取值
    public string[] filesplit(string contents)
    {

        int fileindex = 0;
        string[] splitfile = new string[10];
        while (contents.Length > 3000 && fileindex < 9)
        {

            if (contents.IndexOf("(*^__^*)", 3000) < 0) break;

            splitfile[fileindex] = contents.Substring(0, contents.IndexOf("(*^__^*)", 3000));//这里注意这里的10是字数,我是为了测试而采用10,你可以根据你的新闻页面再设置,我想最少也得200字吧。呵呵。。。。。。
            contents = contents.Remove(0, splitfile[fileindex].Length);
            fileindex++;
        }
        splitfile[fileindex] = contents;
        return splitfile;
    }
    #endregion

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值