我有一个案例,我需要在aspx页面中为asp标签设置Text属性,而不是从后面的代码.更确切地说,我需要在aspx页面中为asp控件设置一个值,并且该值由后面相同页面代码中的属性设置.
所以我需要使用表达式来做到这一点:
- <asp:Label Text="<%= MyProperty %>" ..../>
我用:
- <%= MyProperty %> doesn't work.
- <%# MyProperty %> doesn't also.
解决方法
Default.aspx.cs
- public partial class _Default : System.Web.UI.Page
- {
- public string CustomTitle = "This Is Title";
- protected void Page_Load(object sender,EventArgs e)
- {
- Page.DataBind();
- }
- }
Default.aspx的
- <asp:Label Text='<%#CustomTitle %>' runat="server" />