Thursday, March 24, 2011

ASP.NET Controls and Generics

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>() {... }
}
From stackoverflow
  • This would probably work if you only used the control from code. I doubt it could be represented in markup or in the designer.

  • 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.

  • 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.

0 comments:

Post a Comment