When it comes to creating a custom control, one has to make a critical decisionwhat will be the base class of the new control? There are various options to consider. If you just need a control that operates as a specialized version of an existing component, the choice is a no-brainerjust pick up the control you want to extend and declare it as the new components base class. Creating a brand new or a composite control makes your decision a bit more delicate and crucial because much less is obvious. To find out a few rules to help out with the decision, lets review the characteristics of each of the potential base classesControl and WebControl.
First and foremost, WebControl derives from Control. The core code of each WebControl is built into the class Control and is inherited by any active component ever placed on the surface of an ASP.NET web form. WebControl extends the base class Control with a few properties, most of which relate to the user interface. Typical properties of a WebControl are those that govern the appearance-things like colors, borders, and fonts. Hence, the first rule is, do not choose WebControl unless you need a significant user interface to work with and configure.
Having WebControl as the base class automatically endows the new control with properties such as BackColor, ForeColor, and Font. Are these properties okay with your project and coding style? If so, you're halfway there; if not, you'd better opt for Control and add whatever UI property you may need.
If you derive the new control from Control, you'd also better implement the otherwise-optional INamingContainer marker interface. (A marker interface is a memberless interface implemented only to mark the code with a sort of flag.) WebControl already implements INamingContainer, and this feature is inherited by higher level controls.
What if you have to build a composite control (that is, a control that embeds one or more other controls)?
In this case, the required structure of the user interface drives the choice of the base class. WebControl brings a few graphical properties like BackColor, but these properties hardly work with composite controls with no shared background, font, or foreground text. For example, suppose you want to build a composite control with a Label and a TextBox. If you inherit from WebControl, you have a BackColor property. But what would be the target of this property? The background of the Label? The background of the TextBox? The same holds true for font and other graphical styles. In this case, a better approach probably entails inheriting from Control and defining pairs of Font, BackColor, and ForeColor properties-one for the Label and one for the TextBox.
Choosing the right base class for a control is important, but not essential for the success of the new component. Making the right choice, though, can save you development time, and maybe more importantly, can result in a more elegant and effective overall design.
Dino Esposito is Wintellect's ADO.NET and XML expert, and a trainer and consultant
based in Rome, Italy. Dino is a contributing editor to Windows Developer
Network and MSDN Magazine, and the author of several books for Microsoft
Press including Building Web Solutions with ASP.NET and ADO.NET
and Applied XML Programming for .NET. Contact Dino at [email protected].