rohit singh
rohit singh

Reputation: 590

How do I properly use HTML decode?

I have some text stored in a database:

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

When I retrieve it and HTML decode it into a label control it gives me this text:

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

My code:

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

How can I render the text as HTML like so?

Hi this is Roht Singh

Upvotes: 1

Views: 733

Answers (2)

rohit singh
rohit singh

Reputation: 590

I have solved it i have htmldecode once more the text and it is working. Ans: Server.HtmlDecode(Server.HtmlDecode(ds.Tables[0].Rows[0][0].ToString()));

Upvotes: 2

Chad Levy
Chad Levy

Reputation: 10140

It looks like your data is HTML encoded twice. Try decoding it twice:

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

When you take your original data:

&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;

And HTML decode it, you get the following:

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

When you HTML decode that result, you get:

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

Which should then be rendered as:

Hi this is Roht Singh

Upvotes: 4

Related Questions