简体   繁体   English

用客户端HTML(而不是值)呈现的ASP.NET服务器标签?

[英]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. 也许我已经忘记了如何使用它们,但是我试图将服务器端的值注入HTML输出中变得疯狂。 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: 在发送到浏览器的客户端HTML中的呈现方式如下:

<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: 如果将ASP源更改为使用“#”字符定义为数据绑定语法,则呈现给浏览器的输出将变为:

<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. 在现实生活中,我正在设置CssClass属性,该属性不允许我使用某些建议的“包装”解决方法。 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. 理想情况下,因为我已经在aspx页面上布置了所有控件。 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: 现在,我需要做的是在代码块中的代码后面重复我所有的(250+)控件,如下所示:

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". 我相信这可能无法在已编译的Web应用程序上运行,因为它在运行时不会像C#“网站”那样被解释。 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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