Building Composite Controls

If you don’t want to start from scratch when building a custom control, you can build a composite control. When you create a composite control, you create a new control from existing controls.

Every ASP.NET control has a Controls property that represents all its child controls. If you add child controls to a control, the child controls are automatically rendered when the parent control is rendered.

When you create a composite control, you typically override a control’s CreateChildControls() method. This method is called when a control builds its collection of child controls.

For example, the control in Listing 36.8 combines a TextBox control and RequiredFieldValidator control.

Listing 36.8. RequiredTextBox.cs

Get ASP.NET 4 Unleashed now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.