TemplateContainerAttribute 類別

定義

屬性 (Property) 傳回 ITemplate 介面並使用 TemplateContainerAttribute 屬性 (Attribute) 標記,宣告其容器控制項的基底型別。 具有 ITemplate 屬性的控制項必須實作 INamingContainer 介面。 此類別無法獲得繼承。

public ref class TemplateContainerAttribute sealed : Attribute
[System.AttributeUsage(System.AttributeTargets.Property)]
public sealed class TemplateContainerAttribute : Attribute
[<System.AttributeUsage(System.AttributeTargets.Property)>]
type TemplateContainerAttribute = class
    inherit Attribute
Public NotInheritable Class TemplateContainerAttribute
Inherits Attribute
繼承
TemplateContainerAttribute
屬性

範例

下列程式碼範例示範如何建立名為 的 TemplatedFirstControl 樣板化控制項,並將它與名為 的 FirstTemplateContainer 容器產生關聯。 這可讓您建立自訂控制項,以在未指定範本時顯示伺服器時間,以及在指定範本時顯示範本的內容。

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

namespace Samples.AspNet.CS.Controls
{
    [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;
        }
      }
    }
  }

' File name:templatecontainerattribute.vb.

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)
            EnsureChildControls()
            MyBase.OnDataBinding(e)
        End Sub


        Public Property FirstTemplate() As ITemplate
            Get
                Return _firstTemplate
            End Get

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

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

            Set(ByVal value As [String])
                [_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

    End Class


    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

        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

End Namespace 'CustomControls

下列 Web 表單示範如何使用在上述程式碼範例中建立的自訂控制項。 的兩個 TemplatedFirstControl 實例會放在頁面上:

  • 第一個實例包含範本 <FirstTemplate>

  • 第二個實例不包含 <FirstTemplate> ,因此只會顯示時間。

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

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">

  protected void Page_Load(object sender, EventArgs e)
  {

    // <Snippet3>
    // Get the class type for which to access metadata.
    Type clsType = typeof(TemplatedFirstControl);
    // Get the PropertyInfo object for FirstTemplate.
    PropertyInfo pInfo = clsType.GetProperty("FirstTemplate");
    // See if the TemplateContainer attribute is defined for this property.
    bool isDef = Attribute.IsDefined(pInfo, typeof(TemplateContainerAttribute));
    // Display the result if the attribute exists.
    if (isDef)
    {
      TemplateContainerAttribute tca =
        (TemplateContainerAttribute)Attribute.GetCustomAttribute(pInfo, typeof(TemplateContainerAttribute));
      Response.Write("The binding direction is: " + tca.BindingDirection.ToString());
    }
    // </Snippet3>

  }
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>TemplateContainerAttribute Example</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
      <aspSample:TemplatedFirstControl id="TemplatedFirstControl1" runat="server">
         <FirstTemplate>This is the first template.</FirstTemplate>
      </aspSample:TemplatedFirstControl>
      <br />
      <aspSample:TemplatedFirstControl id="TemplatedFirstControl2" runat="server">
      </aspSample:TemplatedFirstControl>    
    </div>
    </form>
</body>
</html>
<%@ 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" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">

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

    ' <Snippet3>
    ' 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
    ' </Snippet3>
   
  End Sub
  
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>TemplateContainerAttribute Example</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
      <aspSample:VB_TemplatedFirstControl id="TemplatedFirstControl1" runat="server">
         <FirstTemplate>This is the first template.</FirstTemplate>
      </aspSample:VB_TemplatedFirstControl>
      <br />
      <aspSample:VB_TemplatedFirstControl id="TemplatedFirstControl2" runat="server">
      </aspSample:VB_TemplatedFirstControl>    
    </div>
    </form>
</body>
</html>

備註

Type 析器會使用以 物件參數 TemplateContainerAttribute 形式傳入的物件,做為資料系結運算式中使用的物件類型 Container 。 其 屬性會傳 ITemplate 回介面且標示為 的 TemplateContainerAttribute 控制項必須實作 INamingContainer 介面。

如需使用屬性的詳細資訊,請參閱 屬性

建構函式

TemplateContainerAttribute(Type)

使用指定的容器型別,初始化 TemplateContainerAttribute 類別的新執行個體。

TemplateContainerAttribute(Type, BindingDirection)

使用指定的容器型別和 TemplateContainerAttribute 屬性,初始化 BindingDirection 類別的新執行個體。

屬性

BindingDirection

取得容器控制項的繫結方向。

ContainerType

取得容器控制項類型。

TypeId

在衍生類別中實作時,取得這個 Attribute 的唯一識別碼。

(繼承來源 Attribute)

方法

Equals(Object)

傳回值,這個值指出此執行個體是否與指定的物件相等。

(繼承來源 Attribute)
GetHashCode()

傳回這個執行個體的雜湊碼。

(繼承來源 Attribute)
GetType()

取得目前執行個體的 Type

(繼承來源 Object)
IsDefaultAttribute()

在衍生類別中覆寫時,表示這個執行個體的值是衍生類別的預設值。

(繼承來源 Attribute)
Match(Object)

在衍生類別中覆寫時,會傳回值,表示這個執行個體是否等於指定物件。

(繼承來源 Attribute)
MemberwiseClone()

建立目前 Object 的淺層複製。

(繼承來源 Object)
ToString()

傳回代表目前物件的字串。

(繼承來源 Object)

明確介面實作

_Attribute.GetIDsOfNames(Guid, IntPtr, UInt32, UInt32, IntPtr)

將一組名稱對應至一組對應的分派識別項 (Dispatch Identifier)。

(繼承來源 Attribute)
_Attribute.GetTypeInfo(UInt32, UInt32, IntPtr)

擷取物件的類型資訊,可以用來取得介面的類型資訊。

(繼承來源 Attribute)
_Attribute.GetTypeInfoCount(UInt32)

擷取物件提供的類型資訊介面數目 (0 或 1)。

(繼承來源 Attribute)
_Attribute.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr)

提供物件所公開的屬性和方法的存取權。

(繼承來源 Attribute)

適用於

另請參閱