This documentation is archived and is not being maintained.

TemplateContainerAttribute Class

Declares the type of the INamingContainer that will contain the template once it is created.

For a list of all members of this type, see TemplateContainerAttribute Members.

System.Object
   System.Attribute
      System.Web.UI.TemplateContainerAttribute

[Visual Basic]
<AttributeUsage(AttributeTargets.Property)>
NotInheritable Public Class TemplateContainerAttribute
   Inherits Attribute
[C#]
[AttributeUsage(AttributeTargets.Property)]
public sealed class TemplateContainerAttribute : Attribute
[C++]
[AttributeUsage(AttributeTargets::Property)]
public __gc __sealed class TemplateContainerAttribute : public
   Attribute
[JScript]
public
   AttributeUsage(AttributeTargets.Property)
class TemplateContainerAttribute extends Attribute

Thread Safety

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

Remarks

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

Example

[Visual Basic] 

' File name:templatecontainerattribute.cs.

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


Namespace CustomControls
  <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(e As EventArgs)
      EnsureChildControls()
      MyBase.OnDataBinding(e)
    End Sub 
      
      
    Public Property FirstTemplate() As ITemplate
      Get
        Return _firstTemplate
      End Get
      
      Set
        _firstTemplate = value
      End Set
    End Property
        
    Public Property [Text]() As [String]
      Get
        Return [_text]
      End Get
         
      Set      
        [_text] = value
      End Set
    End Property
         
    Public ReadOnly Property DateTime() As [String]
     Get
       Return System.DateTime.Now.ToLongTimeString()
     End Get
    End Property 
        
    Public ReadOnly Property MyTemplateContainer() As Control
      Get
        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)
        FirstTemplate.InstantiateIn(_myTemplateContainer)
        Controls.Add(_myTemplateContainer)
      Else
        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(parent As VB_TemplatedFirstControl)
      Me._parent = parent
    End Sub 'New
           
    Public ReadOnly Property [Text]() As [String]
      Get
        Return _parent.Text
      End Get
    End Property
      
    Public ReadOnly Property DateTime() As [String]
      Get
        Return _parent.DateTime
      End Get
    End Property 

  End Class 'VB_FirstTemplateContainer

End Namespace 'CustomControls


[C#] 

/* File name:templatecontainerattribute.cs. */

using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;

namespace CustomControls
{
  [ParseChildren(true)]
  public class TemplatedFirstControl : Control, INamingContainer
  {
    private ITemplate firstTemplate;
    private String text = null;
    private Control myTemplateContainer;
    
    [System.Security.Permissions.PermissionSet(System.Security.Permissions.SecurityAction.Demand, Name="FullTrust")]         
    protected override void OnDataBinding(EventArgs e) 
    {
      EnsureChildControls();
      base.OnDataBinding(e);
    }


    [TemplateContainer(typeof(FirstTemplateContainer))]
    public ITemplate FirstTemplate
    {
      get 
      {
        return firstTemplate;
      }
      set
      {
        firstTemplate = value;
      }
    }
            
    public String Text
    {
      get 
      {
        return text;
      }
      set
      {
        text = value;
      }
    }
            
    public String DateTime
    {
      get 
      {
        return System.DateTime.Now.ToLongTimeString();
      }
                  
    }
            
    public Control MyTemplateContainer
    {
      get
      {
        return myTemplateContainer;
      }
    }
    
    [System.Security.Permissions.PermissionSet(System.Security.Permissions.SecurityAction.Demand, Name="FullTrust")]         
    protected override void CreateChildControls ()
    {
      if (FirstTemplate != null)
      {
        myTemplateContainer = new FirstTemplateContainer(this);
        FirstTemplate.InstantiateIn(myTemplateContainer);
        Controls.Add(myTemplateContainer);
      }
      else
      {
        Controls.Add(new LiteralControl(Text + " " + DateTime));
      }
    }
            
    }


    public class FirstTemplateContainer : Control, INamingContainer
    {
      private TemplatedFirstControl parent;
      public FirstTemplateContainer(TemplatedFirstControl parent)
      {
        this.parent = parent;
      }
            
      public String Text
      {
        get
        {
          return parent.Text;
        }
      }
     
      public String DateTime
      {
        get 
        {
          return parent.DateTime;
        }
                  
      }
    }
    
  }


[C++] 
/* File name:templatecontainerattribute.cpp. */
#using <mscorlib.dll>
#using <System.dll>
#using <System.Web.dll>
using namespace System;
using namespace System::Web;
using namespace System::Web::UI;
using namespace System::Web::UI::WebControls;
using namespace System::Collections;

namespace CustomControls 
{
   public __gc class FirstTemplateContainer;
   public __gc class TemplatedFirstControl;

public __gc class FirstTemplateContainer : 
   public Control, public INamingContainer 
{
private:
   TemplatedFirstControl*  parent;

public:
   // forwward declarations
   FirstTemplateContainer(TemplatedFirstControl* parent);
   __property String* get_Text();
   __property String* get_DateTime();
};

[ParseChildren(true)]
public __gc class TemplatedFirstControl : 
   public Control, public INamingContainer
{
private:
   ITemplate*  firstTemplate;
   String* text;
   Control*  myTemplateContainer;

protected:
   [System::Security::Permissions::PermissionSet(System::Security::Permissions::SecurityAction::Demand, Name="FullTrust")]
   void OnDataBinding(EventArgs *e) 
   {
      EnsureChildControls();
      __super::OnDataBinding(e);
   }

   [System::Security::Permissions::PermissionSet(System::Security::Permissions::SecurityAction::Demand, Name="FullTrust")]
   void CreateChildControls() 
   {
      if (FirstTemplate != 0) 
      {
         myTemplateContainer = new FirstTemplateContainer(this);
         FirstTemplate->InstantiateIn(myTemplateContainer);
         Controls->Add(myTemplateContainer);
      } 
      else
         Controls->Add(new LiteralControl(String::Concat(Text, S" {0}", DateTime)));
   }

public:
   [TemplateContainer(__typeof(FirstTemplateContainer))]
   __property ITemplate* get_FirstTemplate()
   {
      return firstTemplate;      
   }

   [TemplateContainer(__typeof(FirstTemplateContainer))]
   __property void set_FirstTemplate(ITemplate* value)
   {
      firstTemplate = value;      
   }

   __property String * get_Text()
   {
      return text;
   }

   __property void set_Text(String * value)
   {
      text = value;      
   }

   __property String * get_DateTime() 
   {
      return System::DateTime::Now.ToLongTimeString();
   }

   __property Control * get_MyTemplateContainer() 
   {
      return myTemplateContainer;
   }
};

FirstTemplateContainer::FirstTemplateContainer(TemplatedFirstControl* parent)
{
   this->parent = parent;
}

String* FirstTemplateContainer::get_Text()
{
   return parent->Text;
}

String* FirstTemplateContainer::get_DateTime()
{
   return parent->DateTime;
}
}

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Namespace: System.Web.UI

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

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

See Also

TemplateContainerAttribute Members | System.Web.UI Namespace | Attribute

Show: