How to set the int property of a control to ASCX?
I have an ASCX containing
<my:Foo ID="Bar" runat="server" Value='' /> I want to set Value using textbox1.Text , but Value is 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 in an ASCX file? Should I use TypeConverter for this property?
I do not understand why you cannot just use a 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 this in the code located behind or inside the code block, for example, in the page load event descriptor, since you cannot use code blocks ( <%%> ) within server control:
// code behind, in the page_load event handler Bar.Value = 58; Or, within ascx , outside of the server-side controls:
<% Bar.Value = 58; %> Change it to
<my:Foo ID="Bar" runat="server" Value="58" /> The ASP.Net parser automatically parses the properties of an integer.
<%= ... %> expressions are not 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 .