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?
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);
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 <
and >
codes are converted back to <
and >
.
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.
来源:https://stackoverflow.com/questions/7942133/how-do-i-get-this-html-to-display-formatted-without-tags