Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

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 ()
Not applicable.

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 Web Server Controls Templates (Visual Studio) 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.

Security noteSecurity Note:

This example has a text box that accepts user input, which is a potential security threat. By default, ASP.NET Web pages validate that user input does not include script or HTML elements. For more information, see Script Exploits Overview (Visual Studio).

// 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 Server 2000 SP4, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0
Was this page helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

Show:
© 2014 Microsoft