4

I created a blog (for coding practice). I use a Rich Text Editor (ckeditor) and save the post to a database. When I pull the post out to display in a Label.Text, it shows all the HTML tags:

<p><strong>there was</strong> once a fox that lived</p>
<p> in the<span style="color: #ff0000"> woods</span></p>

How do I get the post to display, with the proper formatting (paragraph, color, etc.), but without the HTML tags?

3
  • Please verify that your output is not filled with &gt; and &lt; Commented Oct 29, 2011 at 22:42
  • Justin808: Hi, the page shows/displays the HTML tags literally per my original post. But when I look at the source code, all the <> are replaced with &gt and &lt. How do I get the browser to read the HTML tags instead of using &gt and ignoring the tags? Commented Oct 29, 2011 at 23:02
  • nanonerd: See the answer from ChrisF below. You need to use a asp:Literal, not a asp:Label. Commented Oct 29, 2011 at 23:26

3 Answers 3

8

FYI: The literal produced the same result as the Label ... but I got my answer, this works:

string strHTML = "<p>Hello World!</p>";
Label.Text = Server.HtmlDecode(strHTML);
Sign up to request clarification or add additional context in comments.

Comments

8

Well the HTML is the formatting, so to get the text to display how you want you need to insert the text as HTML into your page rather than inserting the text into the Label.Text - which will treat is as plain text and display all the markup.

So rather than create a Label use a Literal:

<asp:Literal runat="server" ID="EditorOutput">

Then in your page load:

protected void Page_Load(object sender, EventArgs e)
{
    EditorOutput.Text = theText;
}

theText will be the string:

<p><strong>there was</strong> once a fox that lived</p> <p> in the<span style="color: #ff0000"> woods</span></p>

as read from your database.

If your string has been Encoded you will have to call Server.HtmlDecode on it to make sure that any &lt; and &gt; codes are converted back to < and >.

Source

Comments

-1

I didn't understand. Do you mean, when you check the post, you get something like:

<b>there was</b> once a fox that lived...

It's probably a good idea to save it in .html format, since RTFs were never meant for the internet. It also sounds like you don't need to use a database in the first place. XML is better for that kind of task.

1 Comment

He's not using the rich text format, he's using a "rich" text editor (i.e. a WYSIWYG editor) to produce HTML markup. As for not using a database in favor of XML files: WHAT?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.