[英]add html (for a copyable embed code) that displays as string not html
[英]embed string in the html
Asp.net C#我正在使用下面的代码。
<%String h = "hello";%>
<!-- "wall_com_insert.aspx?Tid=" + Application.Get("Tid");-->
<div id="content_sr_comment" style="height: auto"> <asp:Label ID="Label8"
runat="server" Text="<%=h%>" ></asp:Label>
</div>
但是我得到了输出。
输出显示在标签上:“ <%= h%>”
我猜语法不正确。
您不能在服务器标签内放置服务器命令。 尝试这个:
<div id="content_sr_comment" style="height: auto"> <%= h %></div>
要么
<script runat="server" language="C#">
void Page_Load(object sender, EventArgs e)
{
String h = "hello";
Label8.Text = h;
}
</script>
<div id="content_sr_comment" style="height: auto">
<asp:Label ID="Label8" runat="server"></asp:Label>
</div>
这应该工作:
<script runat="server" language="C#">
private string h = "hello";
</script>
<!-- "wall_com_insert.aspx?Tid=" + Application.Get("Tid");-->
<div id="content_sr_comment" style="height: auto"> <asp:Label ID="Label8"
runat="server"><%=h%></asp:Label>
</div>
我发现有用的另一种方法,尤其是如果您想在html的多个位置写出一个值时,是在代码后面创建一个函数,如下所示:
protected string SayHello()
{
return "Hello";
}
然后,您可以在整个html上使用它:
<div id="content_sr_comment" style="height: auto">
<%=SayHello() %>
</div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.