TemplateContainerAttribute 클래스

정의

ITemplate 인터페이스를 반환하고 TemplateContainerAttribute 특성으로 표시되는 속성의 컨테이너 컨트롤에 대한 기본 형식을 선언합니다. 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

다음 웹 양식에서는 앞의 코드 예제에서 만든 사용자 지정 컨트롤을 사용하는 방법을 보여 줍니다. 두 인스턴스가 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 반환하고 인터페이스로 표시된 컨트롤은 인터페이스를 TemplateContainerAttributeINamingContainer 구현해야 합니다.

특성을 사용 하는 방법에 대 한 자세한 내용은 참조 하세요. 특성합니다.

생성자

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)

이름 집합을 해당하는 디스패치 식별자 집합에 매핑합니다.

(다음에서 상속됨 Attribute)
_Attribute.GetTypeInfo(UInt32, UInt32, IntPtr)

인터페이스의 형식 정보를 가져오는 데 사용할 수 있는 개체의 형식 정보를 검색합니다.

(다음에서 상속됨 Attribute)
_Attribute.GetTypeInfoCount(UInt32)

개체에서 제공하는 형식 정보 인터페이스의 수를 검색합니다(0 또는 1).

(다음에서 상속됨 Attribute)
_Attribute.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr)

개체에서 노출하는 메서드와 속성에 대한 액세스를 제공합니다.

(다음에서 상속됨 Attribute)

적용 대상

추가 정보