不显示呈现的文字控件 HTML
Literal control not displaying rendered HTML
我将 HTML 存储在数据库中并尝试使用文字控件将其呈现在页面上。问题是文字没有呈现代码,而只是在网页上显示标记。我已经检查了 HTML 并且当直接添加到页面时它工作正常但是当设置为文字文本 属性.
时它不起作用
我以前做过很多次,事实上我已经重新使用了代码,但由于某种原因只显示了标记。
页面文字:
<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代码:
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
这是 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>
这是输出的 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>
您的 HTML 已编码,需要解码才能通过 Literal 呈现。使用 Server.HtmlDecode
.
很容易
Literal1.Text = Server.HtmlDecode(sb.ToString)
但是,我建议您不要像在代码中那样使用 Environment.NewLine
,而是使用:
sb.append("<br/>")
我将 HTML 存储在数据库中并尝试使用文字控件将其呈现在页面上。问题是文字没有呈现代码,而只是在网页上显示标记。我已经检查了 HTML 并且当直接添加到页面时它工作正常但是当设置为文字文本 属性.
时它不起作用我以前做过很多次,事实上我已经重新使用了代码,但由于某种原因只显示了标记。
页面文字:
<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代码:
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
这是 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>
这是输出的 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>
您的 HTML 已编码,需要解码才能通过 Literal 呈现。使用 Server.HtmlDecode
.
Literal1.Text = Server.HtmlDecode(sb.ToString)
但是,我建议您不要像在代码中那样使用 Environment.NewLine
,而是使用:
sb.append("<br/>")