Sharing methods between base classes for LinkButton, ImageButton, and Button
I've created a class called CustomLinkButton that inherits from LinkButton and works perfectly. It makes use of the OnLoad method and has some properties that use ViewState. Great. Now I'd like to use that code for Button and ImageButton.
Problem is, all three of those classes inherit from System.Web.UI.WebControls.WebControl, which is missing some important methods and properties common to all three of these controls.
So what to do? Duplicating code in all the classes means poor maintainability. I've tried making another class and writing the methods there but I run into the same problem - I can't have a method that accepts all three types but I can't write a generic method with more than one constraint.
Ideas? Or do I just need to write three classes with duplicate code? I've done a good deal of searching and can't find a solution. This could apply to a lot of situations, hence my asking. Thanks guys.
EDIT: Examples of relevant properties they share are CausesValidation and ValidationGroup.
Although they share similar functionality and needs they are indeed different controls for different purposes. I recommend you keep them separate. With enough time, energy and testing you could have one control that renders anything, but is that really what you want?
You would think LinkButton and ImageButton should inherit from Button, but that is just semantics. You should think about them in terms of what they render, which is HTML:
- asp:button is a <input type="button">
- asp:linkbutton is a <a>
- asp:imagebutton is a <input type="image">