Partilhar via


TemplateBasedControl.TemplateContainer Property

Gets the container control that instantiates the content template.

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

Syntax

'Declaration
Public Overridable ReadOnly Property TemplateContainer As TemplateContainer
    Get
'Usage
Dim instance As TemplateBasedControl
Dim value As TemplateContainer

value = instance.TemplateContainer
public virtual TemplateContainer TemplateContainer { get; }

Property Value

Type: Microsoft.SharePoint.WebControls.TemplateContainer
A TemplateContainer object that instantiates the content template.

Examples

The following example shows how the TemplateContainer property can be used to obtain a reference to a control in a rendering template (defined in an .ascx file). 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 a non-null underlying ISBNField and control mode is not Display

 // Do nothing if the ISBNField is null or control mode is Display.
}
Protected Overrides Sub CreateChildControls()
    If Me.Field IsNot Nothing AndAlso 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 a non-null underlying ISBNField and control mode is not 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