TemplateBasedControl.CreateChildControls Method

Creates the child controls of the TemplateBasedControl class.

Namespace:  Microsoft.SharePoint.WebControls
Assembly:  Microsoft.SharePoint (in Microsoft.SharePoint.dll)
Available in Sandboxed Solutions: No

Syntax

'Declaration
Protected Overrides Sub CreateChildControls
'Usage

Me.CreateChildControls()
protected override void CreateChildControls()

Remarks

Depending on the value specified in the TemplateOverride property, the CreateChildControls method instantiates either the Template property or the AlternateTemplate property; or, it lets the logic of the ControlTemplate property determine which rendering template to instantiate.

If the TemplateOverride property is None, the logic of the ControlTemplate property determines which template is used. If the TemplateOverride property override is Both, the Template property is instantiated, unless it's value is a null reference (Nothing in Visual Basic), in which case the AlternateTemplate property is used.

Examples

The following example shows an override of this method in a custom field rendering control. See Walkthrough: Creating a Custom Field Type for the full example.

protected override void CreateChildControls()
{
    if (this.Field != null || this.ControlMode == SPControlMode.Display)
    {
        // Make sure inherited child controls are completely rendered.
        base.CreateChildControls();

        // Associate child controls in the .ascx file with the 
        // fields allocated by this control.
        this.ISBNPrefix = (Label)TemplateContainer.FindControl("ISBNPrefix");
        this.textBox = (TextBox)TemplateContainer.FindControl("TextField");

        if (!this.Page.IsPostBack)
        {
            if (this.ControlMode == SPControlMode.New)
            {
                textBox.Text = "0-000-00000-0";

            } // end assign default value in New mode

        }// end if this is not a postback 

     //Do not reinitialize on a postback.

    }// end if there is an non-null underlying ISBNField or control mode is Display

 // Do nothing if the ISBNField is null or control mode is Display.
}
Protected Overrides Sub CreateChildControls()
    If Me.Field IsNot Nothing OrElse Me.ControlMode = SPControlMode.Display Then
        ' Make sure inherited child controls are completely rendered.
        MyBase.CreateChildControls()

        ' Associate child controls in the .ascx file with the 
        ' fields allocated by this control.
        Me.ISBNPrefix = CType(TemplateContainer.FindControl("ISBNPrefix"), Label)
        Me.textBox = CType(TemplateContainer.FindControl("TextField"), TextBox)

        If Not Me.Page.IsPostBack Then
            If Me.ControlMode = SPControlMode.New Then
                textBox.Text = "0-000-00000-0"

            End If ' end assign default value in New mode

        End If ' end if this is not a postback

     'Do not reinitialize on a postback.

    End If ' end if there is an non-null underlying ISBNField or control mode is Display

 ' Do nothing if the ISBNField is null or control mode is Display.
End Sub

See Also

Reference

TemplateBasedControl Class

TemplateBasedControl Members

Microsoft.SharePoint.WebControls Namespace

Other Resources

How to: Create a Custom Field Type

Custom Field Types

Walkthrough: Creating a Custom Field Type