Mike
Mike

Reputation: 5509

Can I create an abstract base class UserControl so derived .ascx must implement certain controls?

My situation is that I have a control that is implemented to handle two different types. So in some of the methods I have if(controlType == "Type1") ... else... I would like to split this into two different controls with a base class to handle the common operations of both controls. Both controls would have the same markup too so I would like to handle the control events in the base class but the designer seems to screw this up for me. I almost feel like a generic .ascx would be perfect but I don't know where to start with that.

Upvotes: 0

Views: 1115

Answers (1)

Carl Sharman
Carl Sharman

Reputation: 4845

In the past I've tried all sorts of things with inheritance and user controls, including one ascx inheriting from another ascx, and decided in retrospect it wasn't worth the trouble as the maintenance of it tends to be a bit of a headache.

Having two ascx controls inheriting from a common base class works out ok, but I too found trying to handle the events in the base class gets difficult. In the end I just called methods in the base class from the events. You have to handle the events in both controls, but at least people will can follow the code 2 years from now.

Generic ascx would be awesome and is possible:

BUT although this is a clever workaround, as the EDI doesn't directly support it I suspect it will end up adding to the maintenance problems.

I know it's probably not the answer you're looking for, but having ugly if(controlType == "Type1") code, although it feels inelegant, may end up being the easiest to maintain, just because Visual Studio doesn't inherently support inheritance for user controls.

Good luck, and if you find an elegant solution, please let me know!

Upvotes: 1

Related Questions