Export (0) Print
Expand All

Control.CreateChildControls Method

Notifies server controls that use composition-based implementation to create any child controls they contain in preparation for posting back or rendering.

[Visual Basic]
Protected Overridable Sub CreateChildControls()
[C#]
protected virtual void CreateChildControls();
[C++]
protected: virtual void CreateChildControls();
[JScript]
protected function CreateChildControls();

Remarks

When you develop a composite or templated server control, you must override this method. For more information, see Developing a Templated Control and Developing a Composite Control.

For an overview of Control class methods that are commonly overridden, see Methods in ASP.NET Server Controls.

Example

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

[Visual Basic] 
' Override CreateChildControls to create the control tree.
<System.Security.Permissions.PermissionSetAttribute(System.Security.Permissions.SecurityAction.Demand, Name:="FullTrust")> _
Protected Overrides Sub CreateChildControls()
   
   ' Add a LiteralControl to the current ControlCollection.
   Me.Controls.Add(New LiteralControl("<h3>Value: "))
   
   
   ' Create a text box control, set the default Text property, 
   ' and add it to the ControlCollection.
   Dim box As New TextBox()
   box.Text = "0"
   Me.Controls.Add(box)
   
   Me.Controls.Add(New LiteralControl("</h3>"))
End Sub 'CreateChildControls

[C#] 
// Override CreateChildControls to create the control tree.
 [System.Security.Permissions.PermissionSet(System.Security.Permissions.SecurityAction.Demand, Name="FullTrust")]
 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>"));
 }


[C++] 
// Override CreateChildControls to create the control tree.
protected:
[System::Security::Permissions::PermissionSet(System::Security::Permissions::SecurityAction::Demand, Name="FullTrust")]
void CreateChildControls() 
{
   // Add a LiteralControl to the current ControlCollection.
   this->Controls->Add(new LiteralControl(S"<h3>Value: "));

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

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

[JScript] 
// 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>"));
 }

Requirements

Platforms: Windows 2000, Windows XP Professional, Windows Server 2003 family

See Also

Control Class | Control Members | System.Web.UI Namespace | Developing a Composite Control | Developing a Templated Control | Controls | ControlCollection | ChildControlsCreated

Show:
© 2014 Microsoft