似水无痕

我的未来不用是梦!

当在 一个页面显示一个文章的内容时,如果文章太长,会造成页面无限高,样式很不好看,对此我有两种解决方法:
一,将内容放在<div>标签中,然后设置标签的style,具体<div style="overflow:auto;height:50px;">,这里的height是设置div的高度,然后设置当高度超过后会显示一个滚动条,不会破坏页面的整体效果
二,在后来通过分割字符来进行分页,代码如下:
前台代码
<div class="rBox artBox" style="margin-top:0;background-color:#f0f9ff;">
  <div class="tit02" style="background-image:url('images/tit_bg2.gif');">
      <b><a href="Default.aspx">首页</a>&gt;&gt;</b>
      <asp:Literal ID="litSubject" runat="server"></asp:Literal>&gt;&gt;
            <asp:Literal ID="litName" runat="server"></asp:Literal>
  </div>
  <h2><asp:Literal ID="litNewsName" runat="server" Text=""></asp:Literal></h2>
  <div style="font-size:14px;line-height:1.7em;padding:7px 10px 0 10px;"><asp:Literal ID="litContents" runat="server" Text=""></asp:Literal></div>
<hr style="width:98%; text-align:center;" />
  <div style="text-align:right; padding-right:20px; font-size:14px;"><asp:Literal ID="litPageNum" runat="server"></asp:Literal></div>
     <div class="clear"></div>
</div>
后台代码
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using BLL;
using Common;
namespace WebTrain
{
    public partial class ArticleDetails : System.Web.UI.Page
    {
        string id = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(Request.QueryString["id"]))
            {
                id =Request.QueryString["id"];
            }
            if (!string.IsNullOrEmpty(id))
            {
                if (!IsPostBack)
                {
                    loadData();
                }
            }
        }
        void loadData()
        {
            DataSet dsArt = BLL.Release_BLL.getReleaseByID(id);
            if (dsArt.Tables[0].Rows.Count > 0)
            {
                litNewsName.Text = dsArt.Tables[0].Rows[0]["newsName"].ToString();
                this.litContents.Text = OutputBySize(dsArt.Tables[0].Rows[0]["contents"].ToString().Replace("<div","").Replace("</div>","").Replace("<span","").Replace("</span>",""));
                DataSet dsSubject = BLL.Subject_BLL.getSubject(dsArt.Tables[0].Rows[0]["subjectID"].ToString());
                if (dsSubject.Tables[0].Rows.Count > 0)
                {
                    litSubject.Text = dsSubject.Tables[0].Rows[0]["subjectName"].ToString();
                    litName.Text = dsArt.Tables[0].Rows[0]["newsName"].ToString();
                }
            }
        }
        //分页
        public string OutputBySize(string p_strContent)
        {
            string m_strRet = p_strContent;
            int m_intPageSize = 1000;//文章每页大小
            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["pages"] != null)
                {
                    //set Current page number
                    try
                    {
                        //处理不正常的地址栏的值
                        m_intCurrentPage = Convert.ToInt32(Request.QueryString["pages"]);
                        if (m_intCurrentPage > m_intTotalPage)
                            m_intCurrentPage = m_intTotalPage;
                    }
                    catch (Exception ex)
                    {
                        m_intCurrentPage = m_intCurrentPage;
                    }
                }
                //set the page content 设置获取当前页的大小
                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);
                string m_strPageInfo = "<p></p>";
                for (int i = 1; i <= m_intTotalPage; i++)
                {
                    if (i == m_intCurrentPage)
                        m_strPageInfo += "<b>" + i + "</b>|";
                    else
                        m_strPageInfo += "<a href=ArticleDetails.aspx?id=" + Request.QueryString["id"] + "&pages=" + i + ">" + i + "</a>|";
                }
                //输出显示各个页码
                this.litPageNum.Text = m_strPageInfo;
            }
            return m_strRet;
        }
    }
}

posted on 2010-10-11 16:38  似水无痕2010  阅读(350)  评论(0编辑  收藏  举报