Share via


Authoring Custom ASP.NET Server Controls (Which base class?)

ASP.NET 1.1 and earlier provided essentially two potential base-classes for authoring custom ASP.NET server controls:

 

 System.Web.UI.Control - The root class for all server controls, even for the Page class. The Control class does not provide any user interface specific functionality and is generally the base class used for server controls that contain child controls in ASP.NET 1.1. Inheriting from Control makes sense when full customization is required and the styling / UI support provided by WebControl is not needed.

 

System.Web.UI.WebControls.WebControl - Inherits from Control and adds rich user interface functionality such as styling, making it a great base class for custom server controls.

 

The descendent server controls such as TextBox and DataGrid can be inherited from as well if that is what you need but otherwise Control and WebControl were it in terms of custom server control base class options for ASP.NET 1.1 and earlier.

 

ASP.NET 2.0 introduced a few new base classes for server controls that can make things easier:

 

System.Web.UI.WebControls.CompositeControl - Control developers can inherit from this class when building composite server controls that contain child controls as the primary user interface. The class CompositeControl has the designer attribute set to CompositeControlDesigner so inheriting from this base class automatically brings in a custom designer class that helps composite controls render at design-time. You can of course customize design-time further by creating a custom designer but it is nice to have the basic design-time rendering taken care of “for free”. In ASP.NET 1.1, composite controls quite often didn’t render properly without a basic designer applied to the composite control that forced child control creation and rendering at design-time.

 

System.Web.UI.WebControls.DataBoundControl - Control developers can inherit from this class to create data-bound server controls that display data in list or tabular form. This base class makes it super easy to create databound controls when compared to ASP.NET 1.1. To assist with design-time support, ASP.NET 2.0 provides the DataBoundControlDesigner class.

 

System.Web.UI.WebControls.CompositeDataBoundControl – This class inherits from DataBoundControl. A useful base class for databound composite controls.

 

System.Web.UI.WebControls.HierarchicalDataBoundControl - Control developers can inherit from this class to create data-bound controls that work with classes that implement the IHierarchicalDataSource interface and classes that derive from the HierarchicalDataSourceControl and HierarchicalDataSourceView classes.

 

System.Web.UI.TemplateControl - TemplateControl is an abstract class that servers as the base class for both ASP.NET pages and user controls. You might be tempted to think that if you are writing a control that has templates you should inherit from TemplateControl. That’s not the case. CompositeControl with a custom designer that inherits from ControlDesigner is going to be the best choice.

 

Speaking of design-time support, don't be scared off by the improved design-time experience of ASP.NET 2.0 and later. Not only has design-time support gotten better, but it is also easier. More on that in another postJ

Comments