views:

272

answers:

3

can ASP.NET controls be used with generics? Never seen this done and want a way to differentiate some controls on a page by type, ie: DateTime vs int

example:

public class MyGenericTextBox<T>: TextBox
{
public MyGenericTextBox<T>() {... }
}
A: 

This would probably work if you only used the control from code. I doubt it could be represented in markup or in the designer.

AnthonyWJones
+1  A: 

Be careful - Visual Studio cannot create intellisense schemas when one of the classes in an assembly of controls is generic. Someone added a control like this to one of our projects and it took us two months to figure out why we had no intellisense in our markup.

Andrew Hare
+1  A: 

It can be done with a special hack, for example the one I used in this article:

A Typed Repeater in ASP.NET

However I would not recommend doing this just for convenience in simple cases, since the result would be more complex than the thing you'll try to improve.

Andrey Shchekin