Syntax for general user control (i.e. <T>)
Assuming I created a web control as follows:
public class TestControl<T> : WebControl { ... } Is there a way to place this control on an .aspx page without having to do this with code? I really want to be able to do something like:
<controls:TestControl<int> runat="server" /> But as far as I can tell, there is no way to pass me a generic parameter. I tried searching on the Internet and found this http://forums.asp.net/t/1309629.aspx , which seems to be exactly what I need, but no one seems to understand what the guy wanted, and I cannot find nothing like this on stackoverflow.
Nope. Itβs best to use this as a base and get more direct controls from it, such as TestIntControl , a TestStringControl and something else. I know that this defeats the goal of pure labor, but you have few other options. You can then use these types where you need explicit markup, and still have the flexibility of the base type on more dynamic pages.
You can simply create an abstract abstract type and inherit a specific type, which can then be placed on the page. On the one hand, this is a bit more code, but it also allows you to customize the type by calling the base constructor.
public abstract class MyGenericControl<T> : WebControl { ... public T SomeStronglyTypedProperty { get; set; } protected MyGenericControl(...) { ... } ... } public sealed class MyConcreteControl : MyGenericControl<SomeType> { public MyConcreteControl() : base( ... ) { } } in your markup:
<%@ Page ... %> <%@ Register assembly="MyAssembly" namespace="MyNamespace" tagPrefix="abc" %> <asp:Content ...> <abc:MyConcreteControl id="myConcreteControl" runat="server" /> </asp:Content> and then in your code:
... SomeType value = GetAValue(); myConcreteControl.SomeStronglyTypedProperty = value; ...