假如数据库中,我保存的内容含有html标签,比如说<br> <p><p/> ,我读取后在TextBox中显示,用什么方法。我用Response.Write()方法可以,但是这不能按我的要求显示在我指定的地方,请教,谢谢

解决方案 »

  1.   

       public string checkStr(string html)
        {
            System.Text.RegularExpressions.Regex regex1 = new System.Text.RegularExpressions.Regex(@"<script[\s\S]+</script *>", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
            System.Text.RegularExpressions.Regex regex2 = new System.Text.RegularExpressions.Regex(@" href *= *[\s\S]*script *:", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
            System.Text.RegularExpressions.Regex regex3 = new System.Text.RegularExpressions.Regex(@" no[\s\S]*=", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
            System.Text.RegularExpressions.Regex regex4 = new System.Text.RegularExpressions.Regex(@"<iframe[\s\S]+</iframe *>", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
            System.Text.RegularExpressions.Regex regex5 = new System.Text.RegularExpressions.Regex(@"<frameset[\s\S]+</frameset *>", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
            System.Text.RegularExpressions.Regex regex6 = new System.Text.RegularExpressions.Regex(@"\<img[^\>]+\>", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
            System.Text.RegularExpressions.Regex regex7 = new System.Text.RegularExpressions.Regex(@"</p>", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
            System.Text.RegularExpressions.Regex regex8 = new System.Text.RegularExpressions.Regex(@"<p>", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
            System.Text.RegularExpressions.Regex regex9 = new System.Text.RegularExpressions.Regex(@"<[^>]*>", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
            html = regex1.Replace(html, ""); //过滤<script></script>标记 
            html = regex2.Replace(html, ""); //过滤href=javascript: (<A>) 属性 
            html = regex3.Replace(html, " _disibledevent="); //过滤其它控件的on...事件 
            html = regex4.Replace(html, ""); //过滤iframe 
            html = regex5.Replace(html, ""); //过滤frameset 
            html = regex6.Replace(html, ""); //过滤frameset 
            html = regex7.Replace(html, ""); //过滤frameset 
            html = regex8.Replace(html, ""); //过滤frameset 
            html = regex9.Replace(html, "");
            html = html.Replace(" ", "");
            html = html.Replace("</strong>", "");
            html = html.Replace("<strong>", "");
            return html;
        }
      

  2.   


    public string NoHTML(string Htmlstring) //去除HTML标记 
        { 
            //删除脚本 
            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; 
        }
      

  3.   

    谢谢你,new_Stone,你把html都过滤了,都换成string.empty了,我不是想要这样的结果,我是想要:比如有<strong></strong>,则实现字体加粗,如果过滤都空了,则不能达到想要的效果
      

  4.   

    用 jquery 这个功能很easy
      

  5.   

    wangjun你也采用了//去除HTML标记,我不要去除标记,要实现其效果,呵呵
      

  6.   

    LZ想保留样式标签:如保留<strong></strong>加粗,那么可以将:
     html = html.Replace(" </strong>", ""); 
     html = html.Replace(" <strong>", ""); 
    去掉,即不替换<strong></strong>标签.
      

  7.   

    Literal 放个Literal控件,cs里 Literal.Text=字符串
      

  8.   

    如果按9楼你这么做,显示出来的就是<strong> 文本内容</strong>,而不是  文本内容,我想要的是不显示标签,但是要其效果
      

  9.   


    <script type="text/javascript" src="js/jquery.js"></script>
    <script type="text/javascript">
      function show()
        {
            window.alert($('#divContent').html());//获取id为divContent的所有html标记
        
        }
    </script>
      

  10.   

    Literal 放个Literal控件,cs里 Literal.Text=字符串,这个方法确实行,但是这个控件不支持修改,我晕,我要是读出来后修改内容咋办啊?
      

  11.   

    你用编辑器呀!编辑器你都不知道呀
    fckeditor,cuteeditor,freetextbox!
      

  12.   

    我这里直接用的Response.Write();测试的,LZ请把html标签不替换试试.
      

  13.   

    我用Response.Write()方法可以,我在提问的时候说过了,但是我要让他显示在textBox中,我试试看楼上说的编辑器,呵呵
      

  14.   


     objDiv.innerHtml = "html代码";
    <div id="objDiv" runat="server"></div>
      

  15.   

    呵呵,我知道了,用EwebEditor编辑器可以,谢谢大家了。特别感谢new_Stone和Rohan