asp.net图文混排长新闻分页

海贝   2018年6月19日 浏览量:6850

https://www.cnblogs.com/fdggg/articles/2956400.html


前段时间遇到的问题,最后解决了,现在把代码共享,代码没有经过整理,有点乱,不过还是有思路的。

首先:分页是靠检索<p>标签实现的,所以后台发布新闻的时候最好用<p>标签分段。

其次:应该考虑到分页时不能截断html标签,否则,页面可能变的惨不忍睹,所以,程序要找到</p>标签,再在它后面分页。

最后:还要想到新闻的最后一页不能内容太少,比如用户辛苦点击了一个按钮,结果显示了一行新闻,是不是很搞笑,所以,要时刻判断新闻的长度。而且要注意新闻的分页数,不应太多,我这里设置的是最多9页。

以下代码是根据1000个字符分一页,基本思路是,程序检索到1000个字符处,然后从这开始寻找离得最近的<p>标签,找到之后在其 前面分页。同时还要判断剩余的没有分页的字符数,我设置的是如果剩余字符数少于1800就不需要在分页了,防止最后一页内容过少。

private void BindNewContent() //获取新闻的方法
    {
        int id = Convert.ToInt32(Request.QueryString["id"]); //获取新闻id
        int newPageId=Convert.ToInt32(Request.QueryString["newPageId"]); //获取现在显示的是该新闻哪页
        DBClass db = new DBClass();
        string SelectStr =
            "select * from new where newId=@id";
        SqlCommand com = db.GetCommandStr(SelectStr);
        com.Parameters.Add("@id", SqlDbType.Int).Value = id;
        SqlDataReader sdr = db.ExecDataReader(com);
        if (sdr.Read())
        {
            //绑定新闻正文,并分页。


            string newMainText = sdr["newMainText"].ToString();
            newMainText = SetNewMainPage(newMainText, newPageId, id); //这里通过SetNewMainPage给新闻分页

            TitleLabel.Text = sdr["newTitle"].ToString();
            Page.Header.Title = "【" + sdr["newTitle"].ToString() + "】";
            SourceLabel.Text = sdr["newSource"].ToString();
            TimeLabel.Text = sdr["newTime"].ToString();
            SummaryLabel.Text = sdr["newSummary"].ToString();
            ContentLabel.Text = newMainText;
            //ContentLabel.Text = sdr["newMainText"].ToString();
            EditorLabel.Text ="责任编辑:"+ sdr["newEditor"].ToString();
        }
        else
        {
            Response.Write("<script>alert('您访问的页面不存在');window.close();</script>");
            Response.End();
            return;
        }
        sdr.Close();
    }

    private string SetNewMainPage(string newMainText, int

newPageId,int id) //新闻分页的方法,newMainText是新闻正文字符串,newPageId表示当前为该新闻的哪一页,id表示新闻的id号。
    {
        string strContent = newMainText;
        ArrayList min =new ArrayList();
        string[] strNew = new string[10];
        int index = 0;
        while(!
加载中...

正在加载更多内容...

更新日期:2018年6月19日
关键字:asp.ne t图文混排 长新闻 分页
免责声明:文章或资料来源于网络,如有关于本文内容、版权或其它问题请于文章发表后的30日内与本网管理员联系删除或修改。

01/1|<<1>>|