Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Variables inside control properties

This code:

<asp:TextBox runat="server" MaxLength="<%=Settings.UsernameMaxLength %>" ID="Username"/>

Throws a parser error.

Is it possible to set properties in any way similar to this without using the code behind?

like image 872
Tom Gullen Avatar asked May 02 '26 19:05

Tom Gullen


2 Answers

No, it is not possible. Syntax <%= some code here %> cannot be used with server-side controls. You can either go with <%# some code here %>, but only in case of data binding, or just set this property in code behind, say on Page_Load:

protected void Page_Load(object source, EventArgs e)
{
    Username.MaxLength = Settings.UsernameMaxLength;
}
like image 195
Andrei Avatar answered May 05 '26 09:05

Andrei


You may try this, which should set the MaxLength value upon rendering :

<%
  Username.MaxLength = Settings.UsernameMaxLength;
%>
<asp:TextBox runat="server" ID="Username"/>

I think (not tried) you can also write :

<asp:TextBox runat="server" MaxLength="<%#Settings.UsernameMaxLength %>" ID="Username"/>

But you would then need to call Username.DataBind() somewhere in the codebehind.

like image 34
jbl Avatar answered May 05 '26 07:05

jbl



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!