vb.net 文字控件不显示呈现的 HTML
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27931124/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Literal control not displaying rendered HTML
提问by connersz
I am storing HTML in a database and trying to render it on a page using a literal control. The problem is that the literal is not rendering the code and is instead just displaying the markup on the web page. I have checked the HTML and it works fine when added directly to the page but does not work when set as the literals text property.
我将 HTML 存储在数据库中并尝试使用文字控件在页面上呈现它。问题是文字没有呈现代码,而只是在网页上显示标记。我已经检查了 HTML,当直接添加到页面时它工作正常,但在设置为文字文本属性时不起作用。
I have done this many times before, in face I have re-used the code but for some reason only the markup is showing.
我之前已经做过很多次了,面对我已经重新使用了代码,但由于某种原因,只显示了标记。
Literal on page:
页面文字:
<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentArea" Runat="Server">
<asp:Literal ID="Literal1" runat="server" Mode="PassThrough"></asp:Literal>
</asp:Content>
VB Code:
VB代码:
Private Sub LoadContent()
Dim strConnectionString As String = ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString
Dim conn As SqlConnection = New SqlConnection
conn.ConnectionString = ConfigurationManager _
.ConnectionStrings("ConnectionString").ConnectionString
Dim cmd As SqlCommand = New SqlCommand
cmd.CommandText = ("select HTML from Pages where " & _
"PageName like @SearchText + '%'")
cmd.Parameters.AddWithValue("@SearchText", "TheShop")
cmd.Connection = conn
Dim sb As StringBuilder = New StringBuilder
conn.Open()
Dim sdr As SqlDataReader = cmd.ExecuteReader
While sdr.Read
sb.Append(sdr("HTML")) _
.Append(Environment.NewLine)
End While
conn.Close()
Literal1.Text = sb.ToString
End Sub
This is the contents of sb.ToString before it is added to the literal:
这是将 sb.ToString 添加到文字之前的内容:
<p><span style="color:#FF0000">revergveraer</span></p>
<p><span style="color:#FF0000"><img alt="" src="http://pngimg.com/upload/fish_PNG1156.png" style="height:133px; width:200px" />rgergerg</span></p>
This is the HTML that is output and shown as markup by the literal:
这是输出并显示为文字标记的 HTML:
<p><span style="color:#FF0000">revergveraer</span></p> <p><span style="color:#FF0000"><img alt="" src="http://pngimg.com/upload/fish_PNG1156.png" style="height:133px; width:200px" />rgergerg</span></p>
回答by Steve
Your HTML is encoded and needs to be decoded before it can be rendered through the Literal. This is easy with Server.HtmlDecode.
您的 HTML 已编码,需要先解码,然后才能通过 Literal 呈现。这很容易与Server.HtmlDecode.
Literal1.Text = Server.HtmlDecode(sb.ToString)
However, I would suggest you do NOT use Environment.NewLineas you are in your code but instead use:
但是,我建议您不要Environment.NewLine像在代码中那样使用,而是使用:
sb.append("<br/>")

