Dr. Dobb's is part of the Informa Tech Division of Informa PLC

This site is operated by a business or businesses owned by Informa PLC and all copyright resides with them. Informa PLC's registered office is 5 Howick Place, London SW1P 1WG. Registered in England and Wales. Number 8860726.


Channels ▼
RSS

The Dilemma of Control versus WebControl


asp_template

When it comes to creating a custom control, one has to make a critical decision—what 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-brainer—just pick up the control you want to extend and declare it as the new component’s 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, let’s review the characteristics of each of the potential base classes—Control 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].



Related Reading


More Insights






Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

 
Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.