如何在标记中"绑定"标签的Text属性

Ada*_*igh 3 c# asp.net

基本上我想找到一种ddo的方法:

<asp:Label ID="lID" runat="server" AssociatedControlID="txtId" Text="<%# MyProperty %>"></asp:Label>
Run Code Online (Sandbox Code Playgroud)

我知道我可以从后面的代码设置它(写lId.Text = MyProperty),但我更喜欢在标记中进行,我似乎无法找到解决方案.(MyProperty是一个字符串属性)欢呼

小智 10

代码表达式也是一种选择.与标准<%=%>标记不同,它们可以在ASP标记中的引号内使用.

一般语法是:

<%$ resources: ResourceKey %>
Run Code Online (Sandbox Code Playgroud)

appSettings有一个内置表达式:

<%$ appSettings: AppSettingsKey %>
Run Code Online (Sandbox Code Playgroud)

有关这方面的更多信息:http://weblogs.asp.net/infinitiesloop/archive/2006/08/09/The-CodeExpressionBuilder.aspx


Mar*_*sen 9

你可以做

<asp:Label runat="server" Text='<%# MyProperty %>' />
Run Code Online (Sandbox Code Playgroud)

然后是代码隐藏中的Page.DataBind().