简体   繁体   中英

ASP.NET server tags rendered in client HTML, not values?

Maybe I've forgotten how to use these, but I am going crazy trying to inject a server-side value into an HTML output. There are reasons why I am doing this inline, and not server-side, so please don't suggest that as a solution.

This code on the server side:

<asp:Label ID="Label1" runat="server" Text='<%= DateTime.Now.ToString() %>' />;

Renders as this in the client HTML sent to the browser:

<span id="Label1"> <%= DateTime.Now.ToString()></span>;

And it displays as big fat empty space, and nothing output to the interface.

If I change the ASP source to using the "#" character to define as data-binding syntax, then the rendered output to browser becomes:

<span id="Label1"></span>


EDIT:

Setting Label text was just a simplified object for the sake of asking the question. In real life, I am setting the CssClass attribute, which does not allow me to use the "wrapping" workaround some have suggested. I wanted to set a public property and have all the controls update from it dynamically on page load.

Ideally, since I already have all the controls laid out on the aspx page. Just looking to add an attribute. I wanted to have:

<asp:textbox ID='MyTxtBox1' CssClass='<% strVal1 %>' />  
<asp:textbox ID='MyTxtBox2' CssClass='<% strVal1 %>' />  
<asp:textbox ID='MyTxtBox3' CssClass='<% strOtherVal %>' />  
<asp:textbox ID='MyTxtBox4' CssClass='<% strVal1 %>' />  

Now what it looks like I need to do is repeat all my (250+) controls on the codebehind in a block of code that looks like:

MyTxtBox1.CssClass=strVal1  
MyTxtBox2.CssClass=strVal1  
MyTxtBox4.CssClass=strVal1  

MyTxtBox3.CssClass=strOtherVal  

I believe that may not work on a compiled Web Application as it's not interpreted at run-time like a C# "Web Site". However, I was able to get it to work wrapping the label around the value:

<asp:Label runat="server"><%= DateTime.Now.ToString() %></asp:Label>

设置Label1.Text =值,而不是尝试在服务器控件内部使用服务器端attrs

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM