nanonerd
nanonerd

Reputation: 1984

How do I get this HTML to display formatted, without tags?

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?

Upvotes: 4

Views: 36238

Answers (3)

ChrisF
ChrisF

Reputation: 137178

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

Upvotes: 8

nanonerd
nanonerd

Reputation: 1984

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

Upvotes: 8

user962042
user962042

Reputation:

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.

Upvotes: -1

Related Questions