How to Allow Generic Controls in ASP.NET Pages
Did you ever want to have a Repeater<Customer> control on your page? And its events were all strongly typed to recognize that each row was bound to a Customer object? Well, I came up with a way of doing it using some lesser known features of ASP.NET.
Today it's possible to use generic controls in ASP.NET, but you can't directly have a generic control in the tag markup. You'd typically have to do some trick such as deriving a concrete type from the generic type or instantiating the control from code-behind and adding it to a placeholder on the page.
My solution involves using a ControlBuilder to alter how the ASP.NET tag gets parsed. Typically ASP.NET parses a tag such as <cc:GenericControl runat="server" /> and instantiated a type called GenericControl in the mapped "cc" namespace. My ControlBuilder overrides that behavior by returning an instance of GenericControl<TDataItem> whenever that tag is encountered.
Here's an outline of my sample control:
namespace GenericControls { public class GenericControl<TDataItem> : Control where TDataItem : new() { public event EventHandler<GenericEventArgs<TDataItem>> Stuffing { add; remove; } } }
Note that the control itself is generic over TDataItem, and that is exposes an event called "Stuffing" that uses a generic EventHandler with generic EventArgs also bound to TDataItem. So, if you have an instance of the GenericControl<Customer>, the event handler should take event arguments of type EventArgs<Customer>. Now you don't have to cast your event arguments to get to your data object!
To use the control you specify the generic argument in the form of a pseudo-property on the control tag itself called ObjectType:
<gc:GenericControlGeneric runat="server" ID="CustomerGeneric" ObjectType="MyStuff.Customer, App_Code" OnStuffing="CustomerGeneric_Stuffing"> </gc:GenericControlGeneric>
Yes, my naming convention is horrible. Unless you thing GenericControlGeneric sounds good. But it doesn't sound good; it's horrible! :)
And before anyone asks, the topic of generic syntax in tags has been discussed at length, so I won't repeat that here.
In summary, I encourage you to check it out and see if it helps solve any problems you've had, hopefully without creating any new problems. This was just a pet project I did to see whether it was possible to create generic controls in markup.
And finally, a caveat:
While this seems to work great at runtime, I've encountered some weird problems in Visual Studio to support this. For example, when you try to add an event handler for the Stuffing event through the property grid you might get some weird behavior.
Download the project source code here: GenericControl.zip