Can I use <% = ...%> to set the control property in ASP.NET?

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

The code above does not work. I can set the MaxLength property of the text field in the code, but I probably don't want it. Can I set the MaxLength property in the front-end code as above?

+5
source share
9 answers

You can use DataBinding:

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

and in your code for calling Page_Load:

tbName.DataBind(); 

or directly link the page to the page:

this.DataBind();
+13
source

<% = % > Response.Write(), . <% = % > ( ) Response.Write, -. , :

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

: http://aspnet.4guysfromrolla.com/articles/022509-1.aspx

+6

:

// from http://weblogs.asp.net/infinitiesloop/archive/2006/08/09/The-CodeExpressionBuilder.aspx
[ExpressionPrefix("Code")]
public class CodeExpressionBuilder : System.Web.Compilation.ExpressionBuilder
{
    public override CodeExpression GetCodeExpression(BoundPropertyEntry entry,
       object parsedData, ExpressionBuilderContext context)
    {
        return new CodeSnippetExpression(entry.Expression);
    }
}

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

, <% = expression% > .

, , , , <% # expression% > , MyTextBox.Databind CodeBehind.

, , CodeBehind.

: , , span. , CSS .., , <% = % > .

+4

Page_Init ?

+3

, , , .

, . , , , , . , .

+1

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

<

protected void Page_Load(object sender, EventArgs e) {
    Page.DataBind();
}
+1

"" .aspx , , :

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

"" ASP.

+1
source

All Articles