[英]embed string in the html
Asp.net c# I am using the below code.. 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>
But I am getting the output.. 但是我得到了输出。
output displayed on the label: "<%=h%>" 输出显示在标签上:“ <%= h%>”
I guess the syntax is not correct.. can I get help 我猜语法不正确。
You can't place a server command inside a server tag. 您不能在服务器标签内放置服务器命令。 Try this: 尝试这个:
<div id="content_sr_comment" style="height: auto"> <%= h %></div>
Or 要么
<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>
This should work: 这应该工作:
<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>
Another approach that I find useful, especially if you want to write out a value in multiple places in your html is to create a function in your code behind like this: 我发现有用的另一种方法,尤其是如果您想在html的多个位置写出一个值时,是在代码后面创建一个函数,如下所示:
protected string SayHello()
{
return "Hello";
}
You can then use it all over your html: 然后,您可以在整个html上使用它:
<div id="content_sr_comment" style="height: auto">
<%=SayHello() %>
</div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.