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

时间秒杀一切 提交于 2019-12-01 06:56:07

问题


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?


回答1:


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



回答2:


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




回答3:


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!