Developing a Composite Control
You can author new controls by combining existing controls using class composition. A composite control renders a user interface that reuses the functionality of existing controls. A composite control can synthesize properties from the properties of its child controls and handle events raised by its child controls. It can also expose custom properties and events.
A composite control must do the following.
- Override the protected CreateChildControls method inherited from Control to create instances of the child controls and add them to its Controls collection.
- Implement the System.Web.UI.INamingContainer interface. INamingContainer is a marker interface that has no methods. When a control implements INamingContainer, the ASP.NET page framework creates a new naming scope under that control, thus ensuring that the child controls have unique names in the hierarchical tree of controls. This is especially important when a composite control exposes template properties, provides data binding, or needs to route events to its child controls.
You do not have to override the Render method because child controls provide the rendering logic. Note that you can bubble events from the child controls up to the container and expose them as top-level events on the container. For details, see Bubbling an Event and Event Bubbling Control Sample.
For a sample of a composite control, see Composite Server Control Sample.
Composite controls are equivalent to user controls that are authored declaratively. However, there are significant differences between the design-time behavior and the persistence format of composite controls and user controls. For an overview of the differences, see Composite Control vs. User Control.
If you want to optimize the performance of a control, you can consider direct rendering instead of composition, as described in Composition vs. Rendering.