现在的问题是关于内容页中的 html 代码。如果实现分页,容易被截断出现问题,请问这个问题如何解决。谢谢各位!

解决方案 »

  1.   

    添加分页标识,split
    或过滤HTML分页
      

  2.   

    手动分页是最方便的,如果你要自己分页的话,告诉你一个思路:就是按照标签符和字符来分割,而不是单纯根据字符数,例如"<"开始后一定要到">"结束。这样才能保证HTML的完整。
      

  3.   


    手动分页就是在你文章中添加一些标识例如“111111 <#page#>111112”判断的时候发现<#page#>就分页。
      

  4.   


    //content 内容 length 长度 StripHTML 是否除掉HTML来分割
        public static string GetContentSummaryEN(string content, int length, bool StripHTML)
        {        string NowCheckLength = "";
            if (length < content.Length)
            {
                NowCheckLength = "1";
            }
            if (string.IsNullOrEmpty(content) || length == 0)
                return "";
            if (StripHTML)
            {
                Regex re = new Regex("<[^>]*>");
                content = re.Replace(content, "");
                content = content.Replace(" ", "").Replace(" ", "");
                if (content.Length <= length
                    )
                    return content;
                else
                    CheckLeng = "1";
                return content.Substring(0, length) + "……";
            }
            else
            {
                if (content.Length <= length)
                    return content;            int pos = 0, npos = 0, size = 0;
                bool firststop = false, notr = false, noli = false;
                StringBuilder sb = new StringBuilder();            while (true)
                {
                    if (pos >= content.Length)
                        break;
                    string cur = content.Substring(pos, 1);
                    if (cur == "<")
                    {
                        string next = content.Substring(pos + 1, 3).ToLower();
                        if (next.IndexOf("p") == 0 && next.IndexOf("pre") != 0)
                        {
                            npos = content.IndexOf(">", pos) + 1;
                        }
                        else if (next.IndexOf("/p") == 0 && next.IndexOf("/pr") != 0)
                        {
                            npos = content.IndexOf(">", pos) + 1;
                            if (size < length)
                                sb.Append("<br/>");
                        }
                        else if (next.IndexOf("br") == 0)
                        {
                            npos = content.IndexOf(">", pos) + 1;
                            if (size < length)
                                sb.Append("<br/>");
                        }
                        else if (next.IndexOf("img") == 0)
                        {
                            npos = content.IndexOf(">", pos) + 1;
                            if (size < length)
                            {
                                sb.Append(content.Substring(pos, npos - pos));
                                size += npos - pos + 1;
                            }
                        }
                        else if (next.IndexOf("li") == 0 || next.IndexOf("/li") == 0)
                        {
                            npos = content.IndexOf(">", pos) + 1;
                            if (size < length)
                            {
                                sb.Append(content.Substring(pos, npos - pos));
                            }
                            else
                            {
                                if (!noli && next.IndexOf("/li") == 0)
                                {
                                    sb.Append(content.Substring(pos, npos - pos));
                                    noli = true;
                                }
                            }
                        }
                        else if (next.IndexOf("tr") == 0 || next.IndexOf("/tr") == 0)
                        {
                            npos = content.IndexOf(">", pos) + 1;
                            if (size < length)
                            {
                                sb.Append(content.Substring(pos, npos - pos));
                            }
                            else
                            {
                                if (!notr && next.IndexOf("/tr") == 0)
                                {
                                    sb.Append(content.Substring(pos, npos - pos));
                                    notr = true;
                                }
                            }
                        }
                        else if (next.IndexOf("td") == 0 || next.IndexOf("/td") == 0)
                        {
                            npos = content.IndexOf(">", pos) + 1;
                            if (size < length)
                            {
                                sb.Append(content.Substring(pos, npos - pos));
                            }
                            else
                            {
                                if (!notr)
                                {
                                    sb.Append(content.Substring(pos, npos - pos));
                                }
                            }
                        }
                        else
                        {
                            npos = content.IndexOf(">", pos) + 1;
                            sb.Append(content.Substring(pos, npos - pos));
                        }
                        if (npos <= pos)
                            npos = pos + 1;
                        pos = npos;
                    }
                    else
                    {                    if (size < length)
                        {
                            string GetWord = "";
                            if (ASCIIEncoding.ASCII.GetBytes(cur)[0] > 65 || ASCIIEncoding.ASCII.GetBytes(cur)[0] < 122)
                            {                            bool flag = false;
                                while (!flag)
                                {
                                    if (pos >= content.Length)
                                        break;                                string str = content.Substring(pos, 1);
                                    GetWord = GetWord + str;
                                    byte a = ASCIIEncoding.ASCII.GetBytes(str)[0];
                                    if (a < 65 || a > 122)
                                    {
                                        flag = true;
                                    }
                                    else
                                    {
                                        pos += 1;
                                    }
                                }
                                sb.Append(GetWord);                        }
                            else
                            {
                                sb.Append(cur);
                            }
                            //sb.Append(cur);
                            size++;
                            //sb.Append(cur);
                            //size++;                    }
                        else
                        {
                            if (!firststop)
                            {
                                CheckLeng = "1";
                                //sb.Append("……");
                                firststop = true;
                            }
                        }
                        pos++;
                    }            }            string HTML = sb.ToString();
                if (NoHTML(HTML).Length != NoHTML(content).Length || CheckLeng == "1")
                {
                    CheckLeng = "1";                HTML = HTML + "……";
                }
                return HTML;
            }
        }public static string NoHTML(string Htmlstring)
        {        //删除脚本         Htmlstring = Regex.Replace(Htmlstring, @"<script[^>]*?>.*?</script>", "", RegexOptions.IgnoreCase);        //删除HTML         Htmlstring = Regex.Replace(Htmlstring, @"<(.[^>]*)>", "", RegexOptions.IgnoreCase);        Htmlstring = Regex.Replace(Htmlstring, @"([\r\n])[\s]+", "", RegexOptions.IgnoreCase);        Htmlstring = Regex.Replace(Htmlstring, @"-->", "", RegexOptions.IgnoreCase);        Htmlstring = Regex.Replace(Htmlstring, @"<!--.*", "", RegexOptions.IgnoreCase);        Htmlstring = Regex.Replace(Htmlstring, @"&(quot|#34);", "\"", RegexOptions.IgnoreCase);        Htmlstring = Regex.Replace(Htmlstring, @"&(amp|#38);", "&", RegexOptions.IgnoreCase);        Htmlstring = Regex.Replace(Htmlstring, @"&(lt|#60);", "<", RegexOptions.IgnoreCase);        Htmlstring = Regex.Replace(Htmlstring, @"&(gt|#62);", ">", RegexOptions.IgnoreCase);        Htmlstring = Regex.Replace(Htmlstring, @"&(nbsp|#160);", " ", RegexOptions.IgnoreCase);        Htmlstring = Regex.Replace(Htmlstring, @"&(iexcl|#161);", "\xa1", RegexOptions.IgnoreCase);        Htmlstring = Regex.Replace(Htmlstring, @"&(cent|#162);", "\xa2", RegexOptions.IgnoreCase);        Htmlstring = Regex.Replace(Htmlstring, @"&(pound|#163);", "\xa3", RegexOptions.IgnoreCase);        Htmlstring = Regex.Replace(Htmlstring, @"&(copy|#169);", "\xa9", RegexOptions.IgnoreCase);        Htmlstring = Regex.Replace(Htmlstring, @"&#(\d+);", "", RegexOptions.IgnoreCase);        Htmlstring.Replace("<", "");        Htmlstring.Replace(">", "");        Htmlstring.Replace("\r\n", "");        Htmlstring = HttpContext.Current.Server.HtmlEncode(Htmlstring).Trim();        return Htmlstring;    }以前写的一个保持HTML分割的函数,你自己试试了。
      

  5.   

    [Quote=引用 9 楼 yan267 的回复:]
    引用 8 楼 jack15850798154 的回复:
    各位大哥能给我一个例子参考一下吗? 我不明白各位所说的手动分页是什么意思啊!还请指教一下。谢谢。
    手动分页就是在你文章中添加一些标识例如“111111 <#page#>111112”判断的时候发现<#page#>就分页。
    是不是我在添加文章的时候都要在添加文章的时候写上 “ 一些标识例如“111111 <#page#>111112” ”啊。
      

  6.   


    晕倒。你在文章中插入<#page#> 就可以了。
    这样111111 是第一页。111112就是第二页啊。你在编辑框里面添加一个按钮,点击按钮,就在编辑框插入<#page#> 就可以了。