Sharing methods between base classes for LinkButton, ImageButton, and Button

So I want to add some javascript to the onclick event of several controls in my web site. They are all of the Button, LinkButton, and ImageButton class.

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.

Answers


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">

Need Your Help

Why is PHP/WordPress echoing a blank line here?

php wordpress whitespace

I have a very simple loop for an archive page at a website:

ScrollView Positioning in Xcode 4

objective-c xcode osx cocoa

I'm new to programming in Xcode and I've got the following question: