Dynamically set a control property

  • Thread starter Thread starter Andrew Robinson
  • Start date Start date
A

Andrew Robinson

is there a way of correctly coding this?

<asp:TextBox ID="TextBox1" runat="server" MaxLength='<%GetMaxLength()%>' />

and from my cs file:

protected int GetMaxLength() { return 5; }

Pretty sure I have seen this before but I can't seem to get it to work
outside of databinding.

-Andy
 
Andy,

You got to declare your method from protected to "public"...

Karthick
 
Nope. Still doesn't work.

Cannot create an object of type 'System.Int32' from its string
representation '<%GetMaxLength()%>' for the 'MaxLength' property

-Andy
 
Andy,

Also set the Maxlength as: <%# GetMaxLength()%> (note the '#')

Hth,
Karthick
 
Still not working or I am missing something there. I am pretty sure the #
notation is for a databound property (which in my case is not the case.)


Thanks again,


-Andy
 
Ok, did you include the '#' (hash) in the GetMaxLength method ?

Hth,
Karthick
 
Howdy,
Howdy,

karthick, protected methods are visible in derived classes. Anyway the
problem is he has to to use data binding expression <%#%> and call DataBind()
somewhere in the code behind/beside if textbox is not embded on data bound
control.

-- begin code behind --
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
// please note there is no need to call databind method if textbox
control is
// placed inside data bound control. otherwise, there are two ways: you
can call
// DataBind() in page scope (it causes all child controls to call the
same method
// therefore it's quite expensive, or call textbox.DataBind()
TextBox1.DataBind();
}
}
protected int GetMaxLength()
{
return 5;
}

-- end code behind --


-- aspx code --
<asp:TextBox ID="TextBox1" runat="server" MaxLength='<%# GetMaxLength()%>' />
-- end aspx code

Hope this helps
 
if you use runat=server, you can not use <%= %> expressions, only
databinding <%# %>, which would require you call databind on the
control. you could use code behind or inline server script

-- bruce (sqlwork.com)
 
Hi Andrew,

As for ASP.NET aspx page's inline expression. <% %> can only be used at
aspx page or user control's top document level, but can not be embeded in
server control's tag attribute (such as <asp:Button... Text =<% %> ..>).
As you've found you can create custom expression builder in ASP.NET 2.0 to
add your inline expression.

BTW, another means for supplying values to server control properties in
aspx inline tempalte is using <%# %> databinding expression. This is
built-in supported. The only different from other inline expression is that
<%# %> databinding expression require you to explicitly call "DataBind()"
method on the target control or its Container control.

If you have any further question on this, please feel free to post here.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead


This posting is provided "AS IS" with no warranties, and confers no rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top