如何正确使用HTML解码?

时间:2013-07-25 03:29:07

标签: c# html asp.net tinymce

我有一些文本存储在数据库中:

<p>Hi this is Roht <strong>Singh</strong></p>

当我检索它并将HTML解码为标签控件时,它会给我这个文本:

<p>Hi this is Roht <strong>Singh</strong></p>

我的代码:

 label1.Text = Server.HtmlDecode(ds.Tables[0].Rows[0][0].ToString());

如何将文本呈现为HTML?

  

嗨,这是Roht Singh

2 个答案:

答案 0 :(得分:4)

您的数据看起来像是两次HTML编码。尝试解码两次:

label1.Text = Server.HtmlDecode(
                    Server.HtmlDecode(ds.Tables[0].Rows[0][0].ToString()
              );

当您获取原始数据时:

&amp;lt;p&amp;gt;Hi this is Roht &amp;lt;strong&amp;gt;Singh&amp;lt;/strong&amp;gt;&amp;lt;/p&amp;gt;

HTML解码它,你得到以下结果:

&lt;p&gt;Hi this is Roht &lt;strong&gt;Singh&lt;/strong&gt;&lt;/p&gt;

当您对该结果进行HTML解码时,您会得到:

<p>Hi this is Roht <strong>Singh</strong></p>

然后应将其呈现为:

  

嗨,这是Roht Singh

答案 1 :(得分:2)

我已经解决了它我再次使用htmldecode文本并且它正在工作。 Ans:Server.HtmlDecode(Server.HtmlDecode(ds.Tables [0] .Rows [0] [0] .ToString()));