This documentation is archived and is not being maintained.

ITemplate Interface

Defines the method to implement for populating an ASP.NET server control with child controls when using a control with inline templates when declared in an .aspx file.

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

[Visual Basic]
Public Interface ITemplate
[C#]
public interface ITemplate
[C++]
public __gc __interface ITemplate
[JScript]
public interface ITemplate

Classes that Implement ITemplate

Class Description
TemplateBuilder Supports the page parser in building a template and the child controls it contains.

Remarks

This interface is used by custom server controls, but never implemented by them. ASP.NET always implements it.

Example

The following example demonstrates a simple templated server control that uses the ITemplate interface to create a templated property.

[Visual Basic] 
Imports System
Imports System.Web
Imports System.Web.UI

Namespace TemplateControlSamplesVB

    Public Class TemplateItem
        Inherits Control
        Implements INamingContainer

        Private _message As String = Nothing

        Public Sub New(Message As String)
            _message = message
        End Sub

        Public Property Message As String
           Get
              Return _message
           End Get
           Set
              _message = Value
           End Set
        End Property
    End Class
    <System.Security.Permissions.PermissionSetAttribute(System.Security.Permissions.SecurityAction.Demand, Name:="FullTrust"), _
    ParseChildren(true)> _
    Public Class Template1VB
        Inherits Control
        Implements INamingContainer

        Private _messageTemplate As ITemplate = Nothing
        Private _message As String = Nothing

        Public Property Message As String

           Get
              Return _message
           End Get
           Set
              _message = Value
           End Set
        End Property

        <TemplateContainer(GetType(TemplateItem))> _
        Public Property MessageTemplate As ITemplate

           Get
              Return _messageTemplate
           End Get
           Set
              _messageTemplate = Value
           End Set
        End Property

        Protected Overrides Sub CreateChildControls()

           ' If a template has been specified, use it to create children.
           ' Otherwise, create a single LiteralControl with the message value.

           If Not (MessageTemplate Is Nothing)
              Controls.Clear()
              Dim I As New TemplateItem(Me.Message)
              MessageTemplate.InstantiateIn(I)
              Controls.Add(I)
           Else
              Me.Controls.Add(New LiteralControl(Me.Message))
           End If
        End Sub

    End Class

End Namespace
   

[C#] 
using System;
using System.Web;
using System.Web.UI;

namespace TemplateControlSamples {

    public class TemplateItem : Control, INamingContainer {
        private String     _message         = null;

        public TemplateItem(String message) {
            _message = message;
        }

        public String Message {

           get {
              return _message;
           }
           set {
              _message = value;
           }
        }
    }

    [System.Security.Permissions.PermissionSet(System.Security.Permissions.SecurityAction.Demand, Name="FullTrust")]
    [ParseChildren(true)]
    public class Template1 : Control, INamingContainer {

        private ITemplate  _messageTemplate = null;
        private String     _message         = null;

        public String Message {

           get {
              return _message;
           }
           set {
              _message = value;
           }
        }

        [
            PersistenceMode(PersistenceMode.InnerProperty),
            TemplateContainer(typeof(TemplateItem))
        ]
        public ITemplate MessageTemplate {
           get {
              return _messageTemplate;
           }
           set {
              _messageTemplate = value;
           }
        }

        protected override void CreateChildControls() {

           // If a template has been specified, use it to create children.
           // Otherwise, create a single LiteralControl with the message value.

           if (MessageTemplate != null) {
              Controls.Clear();
              TemplateItem i = new TemplateItem(this.Message);
              MessageTemplate.InstantiateIn(i);
              Controls.Add(i);
           }
           else {
              this.Controls.Add(new LiteralControl(this.Message));
           }
        }
    }
}
   

[C++] 
#using <mscorlib.dll>
#using <System.dll>
#using <System.Web.dll>
using namespace System;
using namespace System::Web;
using namespace System::Web::UI;

public __gc class TemplateItem : public Control, public INamingContainer {
private:
   String*     _message;

public:
   TemplateItem() : _message(0) {}

   TemplateItem(String* message) {
      _message = message;
   }

   __property String* get_Message() {
      return _message;
   }

   __property void set_Message( String* value ) {
      _message = value;
   }

};

[System::Security::Permissions::PermissionSet(System::Security::Permissions::SecurityAction::Demand, Name=S"FullTrust")]
[ParseChildren(true)]
public __gc class Template1 : public Control, public INamingContainer {

private:
   ITemplate*  _messageTemplate;
   String*     _message;

public:
   Template1() : _messageTemplate(0), _message(0) {}

   __property String* get_Message() {
      return _message;
   }

   __property void set_Message( String* value ) {
      _message = value;
   }

   [
      PersistenceMode(PersistenceMode::InnerProperty),
      TemplateContainer(__typeof(TemplateItem))
   ]
   __property ITemplate* get_MessageTemplate() {
      return _messageTemplate;
   }

   __property void set_MessageTemplate( ITemplate* value ) {
      _messageTemplate = value;
   }

protected:
   void CreateChildControls() {

      // If a template has been specified, use it to create children.
      // Otherwise, create a single LiteralControl with the message value.

      if (MessageTemplate != 0) {
         Controls->Clear();
         TemplateItem* i = new TemplateItem(this->Message);
         MessageTemplate->InstantiateIn(i);
         Controls->Add(i);
      }
      else {
         this->Controls->Add(new LiteralControl(this->Message));
      }
   }
};

[JScript] 

Requirements

Namespace: System.Web.UI

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

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

See Also

ITemplate Members | System.Web.UI Namespace | Control

Show: