我可以使用<%= ...%>在ASP.NET中设置控件属性?

问题描述:

<asp:TextBox ID="tbName" CssClass="formField" MaxLength="<%=Constants.MaxCharacterLengthOfGameName %>" runat="server"></asp:TextBox>

上面的代码不起作用.我可以在后面的代码中设置文本框的MaxLength属性,但我不愿意.是否可以像上面那样在前端代码中设置MaxLength属性?

The code above does not work. I can set the MaxLength property of the textbox in the code behind but i rather not. Is there away I can set the MaxLength property in the front-end code as above?

您可以使用DataBinding:

You could use DataBinding:

<asp:TextBox 
    ID="tbName" 
    CssClass="formField" 
    MaxLength="<%# Constants.MaxCharacterLengthOfGameName %>" 
    runat="server">
</asp:TextBox>

以及在Page_Load调用后面的代码中:

and in your code behind Page_Load call:

tbName.DataBind(); 

或直接对页面进行数据绑定:

or directly databind the page:

this.DataBind();