Few Things to take care while designing custom Composite Controls




We can visualize composite control, the composition of existing controls that reuses the implementation provided by child controls for rendering, post back handling etc. While creating composite controls we don’t face too much of complexity. For example, if our composite control has a Textbox control, the post back data processing for the textbox will be automatically taken care of. Similarly, we can use the existing button control to capture the post back.


Composite control should have: -

  1. Overridden CreateChildControl method that will have the logic implemented for initializing, instantiating, and finally adding the child controls in the control tree.

  2. System.Web.UI.INamingContainer interface implemented. It is a markup interface that doesn’t have any method but it will ensure that page framework will create unique identifiers for each and every control on the page by introducing UniqueID property. Most of the developers complain that state of the child control is not persisting on post backs within the composite control. It is because; composite control is not implemented by INamingContainer interface.

  3. We may want to use EnsureChildControls method before we try to access any child control in the composite control. This method checks whether the child controls have been created and if not first invokes CreateChildControl on that.

  4. We may also want to override Controls property of the base class which returns ControlCollection that can be utilized to call functionality such as Add, Remove or Clear in the controls collection. Before returning control collection, we will call EnsureChildControls API.

Hope this helps!!!