位置 :  首页 / 博客 / 学无止境

ASP.NET长文本的分页

发布人:南昌开优网络 发布时间:2017/02/16 访问量:2467
/// <summary>长文本分页
/// 
/// </summary>
/// <param name="p_strContent"></param>
/// <returns></returns>
public string OutputBySize(string p_strContent)    //分页函数 
{
    string m_strRet = "";
    int m_intPageSize = 500;    //文章每页大小 
    int m_intCurrentPage = 1;    //设置第一页为初始页 
    int m_intTotalPage = 0;
    //去掉html标签取出文章长度 
    int m_intArticlelength = Xiaobin.Utility.Tool.GetNoHTMLString(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["id"] != null)    //读取文章所传入的值
        {
            //set Current page number 
            try
            {
                //处理不正常的地址栏的值 
                m_intCurrentPage = Convert.ToInt32(Request.QueryString["id"]);
                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=?id=" + i + ">[" + i + "]</a> ";

        }
        if (m_intCurrentPage > 1)
            m_strPageInfo = "<a href=?id=" + (m_intCurrentPage - 1) + ">上一页</a>" + m_strPageInfo;
        if (m_intCurrentPage < m_intTotalPage)
            m_strPageInfo += "<a href=?id=" + (m_intCurrentPage + 1) + ">下一页</a>";
        //输出显示各个页码
        this.litPageNum.Text = m_strPageInfo;    //前台的Literal控件用于显示页码
    }
    else
    {
        m_strRet += p_strContent;
    }
    return m_strRet;
}
//页面加载
protected void Page_Load(object sender, EventArgs e)
 {
    litbody.Text = OutputBySize(tmp);    //调用分页方法
}
下一篇:ASP.NET 全选删除