如何让这个HTML显示格式化,没有标签?

nan*_*erd 3 html asp.net label rich-text-editor ckeditor

我创建了一个博客(用于编码练习).我使用富文本编辑器(ckeditor)并将帖子保存到数据库.当我将帖子拉出来以显示在Label.Text中时,它会显示所有HTML标记:

<p><strong>there was</strong> once a fox that lived</p>
<p> in the<span style="color: #ff0000"> woods</span></p>
Run Code Online (Sandbox Code Playgroud)

如何使用正确的格式(段落,颜色等)来显示帖子,但没有HTML标签?

nan*_*erd 7

仅供参考:文字产生了​​与标签相同的结果......但我得到了答案,这有效:

string strHTML = "<p>Hello World!</p>";
Label.Text = Server.HtmlDecode(strHTML);
Run Code Online (Sandbox Code Playgroud)


Chr*_*isF 6

那么HTML 就是格式化,所以要让文本显示你需要如何将文本作为HTML插入页面而不是将文本插入到Label.Text中 - 这将作为纯文本处理并显示所有标记.

因此,而不是创建一个标签使用文字:

<asp:Literal runat="server" ID="EditorOutput">
Run Code Online (Sandbox Code Playgroud)

然后在你的页面加载:

protected void Page_Load(object sender, EventArgs e)
{
    EditorOutput.Text = theText;
}
Run Code Online (Sandbox Code Playgroud)

theText 将是字符串:

<p><strong>there was</strong> once a fox that lived</p> <p> in the<span style="color: #ff0000"> woods</span></p>
Run Code Online (Sandbox Code Playgroud)

从您的数据库中读取.

如果您的字符串已经编码,则必须调用Server.HtmlDecode它以确保将任何&lt;&gt;代码转换回<>.

资源