I have a UserControl (lets say Foo.ascx) that has a Type public property with the name Bar.
I am looking for a way that when i declare this usercontrol in the source view of the markup part to pass a type. for example
<%@ Register Src="~/Controls/Foo.ascx" TagPrefix="prfx" TagName="fooCtrl" %>
and then use it as
<prfx:fooCtrl ID="theId" runat="server" />
if for example i wanted to pass to the control the type of string (such as typeof(string))
something that would have this effect
<prfx:fooCtrl ID="theId" runat="server" Bar="typeof(string)" />
how can it be done?
Before anyone asks, the reason is that i have many other properties in this usercontrol that i pass in this manner and i want to avoid using the CodeBehind just to pass the Type
Short answer, you can’t do this. The editor and compiler treats .ascx files as (in the end) XML. That means on “deserialization” properties can be converted to various primitive types but not complex types such as System.Type. To work, what you would like to do would require, at some point during compilation, that an attribute in an xml document not be treated as text, not converted to a simple type, but interpreted and executed as code. Without altering how Visual Studio and the ASP.NET compiler works, this won’t happen.
Workaround:
stringin your UserControlUse the GetType(string) overload to get an instance of this type within your code.
<prfx:fooCtrl
ID=”theId”
runat=”server”
Bar=”System.String, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089″ />
and, in code: