HTML文本在网格视图中显示 [英] HTML Text show in Grid View

查看:98
本文介绍了HTML文本在网格视图中显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hai All,



我想在网格视图的一列中显示html文本(此网格是从数据库绑定的)







谢谢进展

Aatif

解决方案

< blockquote>我得到了解决方案



首先在.CS页面中创建以下方法,并在绑定数据时从ASPX页面调用thesese方法



为了实现这个方法,我们使用两个命名空间

>使用system.Text.RegularExpression

>使用system.Text





  public   string  HTMLToText( string  HTMLCode)
{
// 删除新行,因为它们在HTML中不可见
HTMLCode = HTMLCode.Replace( \ n );

// 删除标签空间
HTMLCode = HTMLCode.Replace ( \t );

// 从HTML中删除多个空格
HTMLCode = Regex.Replace(HTMLCode, \\\\ + );

// 删除HEAD代码
HTMLCode = Regex.Replace (HTMLCode, < head。*?>
,RegexOptions.IgnoreCase | RegexOptions.Singleline);

// 删除所有JavaScript
HTMLCode = Regex.Replace (HTMLCode, < script。*?>
,RegexOptions.IgnoreCase | RegexOptions.Singleline);

// 替换特殊字符,例如&,<,>,等。
StringBuilder sbHTML = new StringBuilder(HTMLCode);
// 注意:还有更多特殊字符,这些只是
// 最常见。如果需要,您可以在此数组中添加新字符
string [] OldWords = { & < > ® © };
string [] NewWords = {
& \ < > ® © â};
for int i = 0 ; i < OldWords.Length; i ++)
{
sbHTML。替换(OldWords [i],NewWords [i]);
}

// 检查是否有换行符(< br>)或段落(< p>)
sbHTML.Replace( < br> \ n< br>);
sbHTML.Replace( < br hold = /> sbHTML.Replace( >< p hold = />< br mode = >< [^>] +>,RegexOptions.IgnoreCase);

return System.Text.RegularExpressions.Regex.Replace(sbHTML.ToString(), @ > ,System.Text .RegularExpressions.RegexOptions.IgnoreCase);

}



从ASPX调用上述方法页面



 <   asp:label     id   =  lblTrackDescription    runat   =  server    text   =  <% #HTMLToText(Eval(  Description)。ToString() )%>   工具提示  =   <%#(Eval( 说明))%>    xmlns:asp   = #unknown    /  >   <   / p  >  <   / br  >  <   / br  >  <   / br  >  <   / p  >  <   / br  >  


Hai All,

I wanna show the html text in one column of Grid View( This grid is binding from Database)



Thanks Advnce
Aatif

解决方案

I got solution

first create the following method in .CS page and call thesse method from ASPX page while binding the data

For implementing this method we use two namespace
> using system.Text.RegularExpression
> using system.Text


public string HTMLToText(string HTMLCode)
        {
            // Remove new lines since they are not visible in HTML
            HTMLCode = HTMLCode.Replace("\n", " ");

            // Remove tab spaces
            HTMLCode = HTMLCode.Replace("\t", " ");

            // Remove multiple white spaces from HTML
            HTMLCode = Regex.Replace(HTMLCode, "\\s+", " ");

            // Remove HEAD tag
            HTMLCode = Regex.Replace(HTMLCode, "<head.*?>", ""
                , RegexOptions.IgnoreCase | RegexOptions.Singleline);

            // Remove any JavaScript
            HTMLCode = Regex.Replace(HTMLCode, "<script.*?>", ""
                , RegexOptions.IgnoreCase | RegexOptions.Singleline);

            // Replace special characters like &, <, >, " etc.
           StringBuilder sbHTML = new StringBuilder(HTMLCode);
            // Note: There are many more special characters, these are just
            // most common. You can add new characters in this arrays if needed
            string[] OldWords = { " ", "&", """, "<", ">", "®", "©", "", "" };
            string[] NewWords = { " ", "&", "\"", "<", ">", "®", "©", "•", "™" };
            for (int i = 0; i < OldWords.Length; i++)
            {
                sbHTML.Replace(OldWords[i], NewWords[i]);
            }

            // Check if there are line breaks (<br>) or paragraph (<p>)
            sbHTML.Replace("<br>", "\n<br>");
            sbHTML.Replace("<br hold=" />            sbHTML.Replace("><p hold=" /><br mode="><[^>]+>", RegexOptions.IgnoreCase); 

            return System.Text.RegularExpressions.Regex.Replace(sbHTML.ToString(), @">", "", System.Text.RegularExpressions.RegexOptions.IgnoreCase);

        }


Calling above method from ASPX Page

<asp:label id="lblTrackDescription" runat="server" text="<%# HTMLToText(Eval("Description").ToString())%>" tooltip="<%# (Eval("Description"))%>" xmlns:asp="#unknown" /></p></br></br></br></p></br>


这篇关于HTML文本在网格视图中显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆