This documentation is archived and is not being maintained.

TemplateContainerAttribute Class

Declares the base type of the container control of a property that returns an ITemplate interface and is marked with the TemplateContainerAttribute attribute. The control with the ITemplate property must implement the INamingContainer interface. This class cannot be inherited.

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

<AttributeUsageAttribute(AttributeTargets.Property)> _
<AspNetHostingPermissionAttribute(SecurityAction.LinkDemand, Level := AspNetHostingPermissionLevel.Minimal)> _
Public NotInheritable Class TemplateContainerAttribute _
	Inherits Attribute
Dim instance As TemplateContainerAttribute

The Type object that is passed in as a parameter of the TemplateContainerAttribute object is used by the parser as the type of Container object that is used in data-binding expressions. The control whose property returns an ITemplate interface and is marked with the TemplateContainerAttribute, must implement the INamingContainer interface.

For more information about using attributes, see Extending Metadata Using Attributes.

How to: Create Templated ASP.NET User ControlsBuilding ASP .NET Web Applications
How to: Create Templated ASP.NET User ControlsBuilding ASP .NET Web Applications

The following code example demonstrates how to create a templated control named TemplatedFirstControl and associate it with a container named FirstTemplateContainer. This allows the creation of custom controls that display the server time when the template is not specified and the template's content when the template is specified.

' File name:templatecontainerattribute.vb. 

Imports System
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Collections

Namespace Samples.AspNet.VB.Controls
    <ParseChildren(True)> _
    Public Class VB_TemplatedFirstControl
        Inherits Control
        Implements INamingContainer

        Private _firstTemplate As ITemplate
        Private [_text] As [String] = Nothing 
        Private _myTemplateContainer As Control

        <System.Security.Permissions.PermissionSetAttribute(System.Security.Permissions.SecurityAction.Demand, Name:="FullTrust")> _
        Protected Overrides Sub OnDataBinding(ByVal e As EventArgs)
        End Sub 

        Public Property FirstTemplate() As ITemplate
                Return _firstTemplate
            End Get 

            Set(ByVal value As ITemplate)
                _firstTemplate = Value
            End Set 
        End Property 

        Public Property [Text]() As [String]
                Return [_text]
            End Get 

            Set(ByVal value As [String])
                [_text] = Value
            End Set 
        End Property 

        Public ReadOnly Property DateTime() As [String]
                Return System.DateTime.Now.ToLongTimeString()
            End Get 
        End Property 

        Public ReadOnly Property MyTemplateContainer() As Control
                Return _myTemplateContainer
            End Get 
        End Property

        <System.Security.Permissions.PermissionSetAttribute(System.Security.Permissions.SecurityAction.Demand, Name:="FullTrust")> _
        Protected Overrides Sub CreateChildControls()

            If Not (FirstTemplate Is Nothing) Then
                _myTemplateContainer = New VB_FirstTemplateContainer(Me)
                Controls.Add(New LiteralControl([Text] + " " + DateTime))
            End If 
        End Sub 'CreateChildControls

    End Class 'VB_TemplatedFirstControl

    Public Class VB_FirstTemplateContainer
        Inherits Control
        Implements INamingContainer

        Private _parent As VB_TemplatedFirstControl

        Public Sub New(ByVal parent As VB_TemplatedFirstControl)
            Me._parent = parent
        End Sub 'New 

        Public ReadOnly Property [Text]() As [String]
                Return _parent.Text
            End Get 
        End Property 

        Public ReadOnly Property DateTime() As [String]
                Return _parent.DateTime
            End Get 
        End Property 

    End Class 'VB_FirstTemplateContainer

End Namespace 'CustomControls

The following Web form demonstrates how to use the custom control created in the preceding code example. Two instances of the TemplatedFirstControl are placed on the page:

  • The first instance includes the template <FirstTemplate>.

  • The second instance does not include <FirstTemplate>; therefore, it shows just the time.

<%@ Page Language="VB" %>
<%@ Import Namespace="System.Reflection" %>
<%@ Register TagPrefix="aspSample" Namespace="Samples.AspNet.VB.Controls" Assembly="Samples.AspNet.VB.Controls" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">

<script runat="server">

  Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)

    ' Get the class type for which to access metadata. 
    Dim clsType As Type = GetType(VB_TemplatedFirstControl)
    ' Get the PropertyInfo object for FirstTemplate. 
    Dim pInfo As PropertyInfo = clsType.GetProperty("FirstTemplate")
    ' See if the TemplateContainer attribute is defined for this property. 
    Dim isDef As Boolean = Attribute.IsDefined(pInfo, GetType(TemplateContainerAttribute))
    ' Display the result if the attribute exists. 
    If isDef Then 
      Dim tca As TemplateContainerAttribute = CType(Attribute.GetCustomAttribute(pInfo, GetType(TemplateContainerAttribute)), TemplateContainerAttribute)
      Response.Write("The binding direction is: " & tca.BindingDirection.ToString())
    End If 

  End Sub


<html xmlns="" >
<head runat="server">
    <title>TemplateContainerAttribute Example</title>
    <form id="form1" runat="server">
      <aspSample:VB_TemplatedFirstControl id="TemplatedFirstControl1" runat="server">
         <FirstTemplate>This is the first template.</FirstTemplate>
      <br />
      <aspSample:VB_TemplatedFirstControl id="TemplatedFirstControl2" runat="server">


Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98

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

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0