{infiniteZest}
// Articles. Tutorials. Utilities.
Home  |   Search  |   Login  
Categories Skip Navigation Links
New / All
AJAX
Apple
ASP.NET
.NET
Git
Google / Android
Python / IronPython
Miscellaneous
SQL Server
Base class for a custom control
Summary
When you are constructing a custom control, one of the first decisions to make is the base class to be used for the custom control class. You have a few different options for the base class. This article looks at choosing between Control and WebControl as the base class.
 
Table of Contents

Control vs. WebControl

 

An ASP.NET Custom Control (or Web Control) you write can be derived from a whole bunch of classes depending on the functionality you want. Following are some examples:

  • Control
  • WebControl
  • DataBoundControl / BaseDataBoundControl
  • Out of the box controls from the System.Web.UI.WebControls namespace
  • etc.

Control vs. WebControl

If the functionality you are trying to create in your custom control does not extend the functionality in some other existing control and if it’s not tied to a database (or some datasource), you are left with two options: Control or WebControl.

Let’s see which one should be chosen as the base class for your custom control. The entire decision to choosing between Control and WebControl boils down to this: Whether your new custom control has a UI or not. If your custom control has a user interface, then choose WebControl. Otherwise, choose Control as the base class.

You can add user interface with Control as base class as well, but you need to add those properties yourselves. This becomes clear, if you look at the public properties exposed by Control and WebControl classes. Control class resides in System.Web.UI namespace. WebControl class resides in System.Web.UI.WebControls namespace. WebControl class is derived from Control class; so, the WebControl class has all the public properties that the Control class has and then some additional ones.

Let’s see which additional properties does the WebControl class expose:

  • BackColor
  • BorderStyle,
  • BorderWidth
  • CssClass
  • EnableTheming
  • Font
  • ForeColor
  • Width
  • TabIndex, ToolTip (IE-specific)

There are some additional properties, but the above list forms the bulk of the additional properties in the WebControl class. As you can see the common thread among all of them is the ability to set the look and feel of your custom control by the developer using your custom control. All the above or partial list of the above properties can be added by you to your custom control even if it is derived from the Control class. In this case, you would need to add the implementation for these properties yourselves.

Bookmark and Share This

More Articles With Similar Tags
Your custom control events might not fire properly without RegisterRequiresPostBack method. And it would keep on losing its state.
About  Contact  Privacy Policy  Site Map