Export (0) Print
Expand All
1 out of 7 rated this helpful - Rate this topic

ITemplate Interface

Defines the behavior for populating a templated ASP.NET server control with child controls. The child controls represent the inline templates defined on the page.

Namespace:  System.Web.UI
Assembly:  System.Web (in System.Web.dll)
[AspNetHostingPermissionAttribute(SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
[AspNetHostingPermissionAttribute(SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
public interface ITemplate

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

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

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));
           }
        }
    }
}

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
Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.