1 out of 7 rated this helpful - Rate this topic

Control.CreateChildControls Method

Called by the ASP.NET page framework to notify server controls that use composition-based implementation to create any child controls they contain in preparation for posting back or rendering.

Namespace: System.Web.UI
Assembly: System.Web (in system.web.dll)

protected internal virtual void CreateChildControls ()
protected void CreateChildControls ()
protected internal function CreateChildControls ()

When you develop a composite or templated server control, you must override this method. Controls that override the CreateChildControls method should implement the INamingContainer interface to avoid naming conflicts.

For more information, see ASP.NET Web Server Controls Templates and Developing Custom ASP.NET Server Controls.

The following example demonstrates an overridden version of the CreateChildControls method. In this implementation, the composite control displays a TextBox control enclosed in two literal controls that render HTML.

// Override CreateChildControls to create the control tree.
 [System.Security.Permissions.PermissionSet(System.Security.Permissions.SecurityAction.Demand, Name="Execution")]
 protected override void CreateChildControls() {

     // Add a LiteralControl to the current ControlCollection.
     this.Controls.Add(new LiteralControl("<h3>Value: "));


     // Create a text box control, set the default Text property, 
     // and add it to the ControlCollection.
     TextBox box = new TextBox();
     box.Text = "0";
     this.Controls.Add(box);

     this.Controls.Add(new LiteralControl("</h3>"));
 }


// Override CreateChildControls to create the control tree.
/** @attribute System.Security.Permissions.PermissionSet(
    System.Security.Permissions.SecurityAction.Demand, Name = "Execution")
 */
protected void CreateChildControls()
{
    // Add a LiteralControl to the current ControlCollection.
    this.get_Controls().Add(new LiteralControl("<h3>Value: "));
    // Create a text box control, set the default Text property, 
    // and add it to the ControlCollection.
    TextBox box = new TextBox();
    box.set_Text("0");
    this.get_Controls().Add(box);

    this.get_Controls().Add(new LiteralControl("</h3>"));
} //CreateChildControls

// Override CreateChildControls to create the control tree.
 protected override function CreateChildControls() 
 {

     // Add a LiteralControl to the current ControlCollection.
     this.Controls.Add(new LiteralControl("<h3>Value: "));

     // Create a text box control, set the default Text property, 
     // and add it to the ControlCollection.
     var box : TextBox = new TextBox();
     box.Text = "0";
     this.Controls.Add(box);

     this.Controls.Add(new LiteralControl("</h3>"));
 }

Windows 98, Windows 2000 SP4, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0, 1.1, 1.0
Did you find this helpful?
(1500 characters remaining)
Community Content Add
Annotations FAQ
!!! DO NOT OVERRIDE CreateChildControls() !!!
You will run into a world of hurt, especially when it comes to subscribing to child control events! Instead, create a private method called, "CreateCustomChildControls()". Build your control tree there. Simply override OnInit() and call CreateCustomChildControls() from there.

You can buy me a beer later.

Now, if you are using some specialized means of binding data (e.g., Custom Combo Box), load your control tree with "CreateCustomChildControls()", but load your data using "CreateChildControls()".