如何让该 HTML 显示格式化且不带标签?

发布于 2024-12-12 08:08:49 字数 325 浏览 0 评论 0原文

我创建了一个博客(用于编码练习)。我使用富文本编辑器(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>

如何以正确的格式(段落、颜色等)显示帖子,但没有 HTML 标记?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

相对绾红妆 2024-12-19 08:08:49

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

string strHTML = "<p>Hello World!</p>";
Label.Text = Server.HtmlDecode(strHTML);

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);
苏大泽ㄣ 2024-12-19 08:08:49

HTML 格式,因此要让文本显示您想要的方式,您需要将文本作为 HTML 插入到页面中,而不是将文本插入到标签中.Text - 将其视为纯文本并显示所有标记。

因此,不要使用 Literal: 创建标签,而是使用 Literal:

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

那么在页面加载中:

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

字符串:

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

theText 将是从数据库读取的

。如果您的字符串已被编码,您将必须调用Server.HtmlDecode 以确保任何 <> 代码都已转换返回到 <>

来源

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 >.

Source

情话墙 2024-12-19 08:08:49

我不明白。
您的意思是,当您检查该帖子时,您会得到类似以下内容:

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

将其保存为 .html 格式可能是个好主意,因为 RTF 从来就不是为互联网设计的。
听起来您一开始就不需要使用数据库。 XML 更适合此类任务。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文