Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set a Int property of a control on ASCX?

I have an ASCX that contains

<my:Foo ID="Bar" runat="server" Value='' />

I want to set Value with textbox1.Text, but Value is an Int32. I am looking for something like this:

<my:Foo ID="Bar" runat="server" Value='<%= Int32.Parse(textbox1.Text) %>' />

But I get

Parser Error Message: Cannot create an object of type 'System.Int32' from its string representation '<%= Int32.Parse(textbox1.Text) %>' for the 'Value' property.

Is there a way to do this on the ASCX file? Do I have to implement a TypeConverter for this property?

like image 843
BrunoLM Avatar asked Oct 15 '10 18:10

BrunoLM


2 Answers

I don't understand why you can't simply use the literal instead of a string representation:

<my:Foo ID="Bar" runat="server" Value="58" />

If you want to set this value dynamically, you will need to do so in the code behind or within a code block, for example in the page load event handle, as you cannot use code blocks (<%%>) within a server side control:

// code behind, in the page_load event handler
Bar.Value = 58;

Or, within the ascx, outside of server side controls:

<% Bar.Value = 58; %>
like image 198
Oded Avatar answered Oct 20 '22 05:10

Oded


Change it to

<my:Foo ID="Bar" runat="server" Value="58" />

The ASP.Net parser will automatically parse integer properties.

<%= ... %> expressions aren't supported for server-side controls, so your code causes ASP.Net to try (and fail) to parse the literal string <%= Int32.Parse("58") %> as an integer.

like image 45
SLaks Avatar answered Oct 20 '22 07:10

SLaks