ParseChildrenAttribute 클래스

정의

ASP.NET 서버 컨트롤을 개발할 때 사용할 수 있는 메타데이터 특성을 정의합니다. 페이지 파서에서 페이지에 선언된 서버 컨트롤 태그 내부에 중첩된 내용을 처리하는 방법을 나타내려면 ParseChildrenAttribute 클래스를 사용합니다. 이 클래스는 상속될 수 없습니다.

public ref class ParseChildrenAttribute sealed : Attribute
[System.AttributeUsage(System.AttributeTargets.Class)]
public sealed class ParseChildrenAttribute : Attribute
[<System.AttributeUsage(System.AttributeTargets.Class)>]
type ParseChildrenAttribute = class
    inherit Attribute
Public NotInheritable Class ParseChildrenAttribute
Inherits Attribute
상속
ParseChildrenAttribute
특성

예제

이 섹션의 코드 예제에는 두 부분으로 구성됩니다. 첫 번째 코드 예제에서는 클래스에 대 한 속성을 설정 하는 방법을 보여 줍니다 ParseChildrenAttribute . 두 번째 코드 예제에서는 ASP.NET 페이지에서 클래스를 사용하는 방법을 보여 줍니다.

다음 코드 예제에서는 명명 CollectionPropertyControlParseChildrenAttribute 사용자 지정 서버 컨트롤의 개체를 설정 하는 방법을 보여 줍니다. 속성 ParseChildrenAttributeChildrenAsProperties 클래스 trueDefaultProperty 속성을 Employee 설정합니다.

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

namespace Samples.AspNet.CS.Controls
{
   // The child element class.
   [AspNetHostingPermission(SecurityAction.Demand, 
      Level=AspNetHostingPermissionLevel.Minimal)]
   public sealed class Employee
   {
      private String name;
      private String title;
      private String alias;

      public Employee():this ("","",""){}
      
      public Employee (String name, String title, String alias)
      {
         this.name = name;
         this.title = title;
         this.alias = alias;
      }
      public String Name
      {
         get
         {
            return name;
         }
         set
         {
            name = value;
         }
      }
      
      public String Title
      {
         get
         {
            return title;
         }
         set
         {
            title = value;
         }
      }
      
      public String Alias
      {
         get
         {
            return alias;
         }
         set
         {
            alias = value;
         }
      }
   }
   // Use the ParseChildren attribute to set the ChildrenAsProperties
   // and DefaultProperty properties. Using this constructor, the
   // control parses all child controls as properties and must define
   // a public property named Employees, which it declares as
   // an ArrayList. Nested (child) elements must correspond to
   // child elements of the Employees property or to other
   // properties of the control.  
   [ParseChildren(true, "Employees")]
   [AspNetHostingPermission(SecurityAction.Demand, 
      Level=AspNetHostingPermissionLevel.Minimal)]
   public sealed class CollectionPropertyControl : Control
   {  
      private String header;
      private ArrayList employees = new ArrayList();
      
      public String Header
      {
         get
         {
            return header;
         }
         set
         {
            header = value;
         }
      }

      public ArrayList Employees
      {
         get 
         {
            return employees;
         }
      }
      // Override the CreateChildControls method to 
      // add child controls to the Employees property when this
      // custom control is requested from a page.
      protected override void CreateChildControls()
      {
         Label label = new Label();
         label.Text = Header;
         label.BackColor = System.Drawing.Color.Beige;
         label.ForeColor = System.Drawing.Color.Red;
         Controls.Add(label);
         Controls.Add(new LiteralControl("<BR> <BR>"));

         Table table = new Table();
         TableRow htr = new TableRow();

         TableHeaderCell hcell1 = new TableHeaderCell();    
         hcell1.Text = "Name";
         htr.Cells.Add(hcell1);

         TableHeaderCell hcell2 = new TableHeaderCell();
         hcell2.Text = "Title";
         htr.Cells.Add(hcell2);
         
         TableHeaderCell hcell3 = new TableHeaderCell();
         hcell3.Text = "Alias";
         htr.Cells.Add(hcell3);
         table.Rows.Add(htr);

         table.BorderWidth = 2;
         table.BackColor = System.Drawing.Color.Beige;
         table.ForeColor = System.Drawing.Color.Red;
         foreach (Employee employee in Employees)
         {
            TableRow tr = new TableRow();

            TableCell cell1 = new TableCell();
            cell1.Text = employee.Name;
            tr.Cells.Add(cell1);
            
            TableCell cell2 = new TableCell();
            cell2.Text = employee.Title;
            tr.Cells.Add(cell2);
            
            TableCell cell3 = new TableCell();
            cell3.Text = employee.Alias;
            tr.Cells.Add(cell3);
            
            table.Rows.Add(tr);
         }
         Controls.Add(table);
      }
   }
}
Imports System.Collections
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Security.Permissions

Namespace Samples.AspNet.VB.Controls


    ' The child element class.

    <AspNetHostingPermission(SecurityAction.Demand, _
       Level:=AspNetHostingPermissionLevel.Minimal)> _
    Public NotInheritable Class Employee
        Private _name As String
        Private _title As String
        Private _alias As String


        Public Sub New()
            Me.New("", "", "")
        End Sub


        Public Sub New(ByVal name As String, ByVal title As String, ByVal employeeAlias As String)
            Me._name = name
            Me._title = title
            Me._alias = employeeAlias
        End Sub

        Public Property Name() As String
            Get
                Return _name
            End Get
            Set(ByVal value As String)
                _name = Value
            End Set
        End Property


        Public Property Title() As String
            Get
                Return _title
            End Get
            Set(ByVal value As String)
                _title = Value
            End Set
        End Property


        Public Property [Alias]() As String
            Get
                Return _alias
            End Get
            Set(ByVal value As String)
                _alias = Value
            End Set
        End Property
    End Class
    ' Use the ParseChildren attribute to set the ChildrenAsProperties
    ' and DefaultProperty properties. Using this constructor, the
    ' control parses all child controls as properties and must define
    ' a public property named Employees, which it declares as
    ' an ArrayList. Nested (child) elements must correspond to
    ' child elements of the Employees property or to other
    ' properties of the control.   
    <ParseChildren(True, "Employees")> _
    <AspNetHostingPermission(SecurityAction.Demand, _
       Level:=AspNetHostingPermissionLevel.Minimal)> _
    Public NotInheritable Class CollectionPropertyControl
        Inherits Control
        Private _header As String
        Private _employees As New ArrayList()


        Public Property Header() As String
            Get
                Return _header
            End Get
            Set(ByVal value As String)
                _header = Value
            End Set
        End Property




        Public ReadOnly Property Employees() As ArrayList
            Get
                Return _employees
            End Get
        End Property

        ' Override the CreateChildControls method to 
        ' add child controls to the Employees property when this
        ' custom control is requested from a page.
        Protected Overrides Sub CreateChildControls()
            Dim label As New Label()
            label.Text = Header
            label.BackColor = System.Drawing.Color.Beige
            label.ForeColor = System.Drawing.Color.Red
            Controls.Add(label)
            Controls.Add(New LiteralControl("<BR> <BR>"))

            Dim table As New Table()
            Dim htr As New TableRow()

            Dim hcell1 As New TableHeaderCell()
            hcell1.Text = "Name"
            htr.Cells.Add(hcell1)

            Dim hcell2 As New TableHeaderCell()
            hcell2.Text = "Title"
            htr.Cells.Add(hcell2)

            Dim hcell3 As New TableHeaderCell()
            hcell3.Text = "Alias"
            htr.Cells.Add(hcell3)
            table.Rows.Add(htr)

            table.BorderWidth = Unit.Pixel(2)
            table.BackColor = System.Drawing.Color.Beige
            table.ForeColor = System.Drawing.Color.Red
            Dim employee As Employee
            For Each employee In Employees
                Dim tr As New TableRow()

                Dim cell1 As New TableCell()
                cell1.Text = employee.Name
                tr.Cells.Add(cell1)

                Dim cell2 As New TableCell()
                cell2.Text = employee.Title
                tr.Cells.Add(cell2)

                Dim cell3 As New TableCell()
                cell3.Text = employee.Alias
                tr.Cells.Add(cell3)

                table.Rows.Add(tr)
            Next employee
            Controls.Add(table)
        End Sub
    End Class 
End Namespace

다음 코드 예제에서는 ASP.NET 페이지에서 클래스와 Employee 클래스를 사용하는 CollectionPropertyControl 방법을 보여 줍니다. 클래스의 인스턴스는 Employee 선언적으로 추가됩니다.

<%@ Page Language="C#" Debug="true" %>
<%@ Register TagPrefix="AspSample" Assembly="Samples.AspNet.CS.Controls" Namespace="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)
  {
    
    // Verify attribute values.
    ParseChildrenAttribute p = 
      (ParseChildrenAttribute)Attribute.GetCustomAttribute(typeof(CollectionPropertyControl),
      typeof(ParseChildrenAttribute));

    StringBuilder sb = new StringBuilder();
    sb.Append("The DefaultProperty property is " + p.DefaultProperty.ToString() + "<br />");
    sb.Append("The ChildrenAsProperties property is " + p.ChildrenAsProperties.ToString() + "<br />");
    sb.Append("The IsDefaultAttribute method returns " + p.IsDefaultAttribute().ToString());
    Message.Text = sb.ToString();

  }
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>ParseChildrenAttribute Example</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
      <asp:Label ID="Message"
                 runat="server"/>
      <AspSample:CollectionPropertyControl id="CollectionPropertyControl1" 
                                           runat="server">
        <AspSample:Employee Name="Employee 1" 
                            Title="Title 1" 
                            Alias="Alias 1" />
        <AspSample:Employee Name="Employee 2" 
                            Title="Title 2" 
                            Alias="Alias 2" />
      </AspSample:CollectionPropertyControl>    
    </div>
    </form>
</body>
</html>
<%@ Page Language="VB" %>
<%@ Register TagPrefix="AspSample" Assembly="Samples.AspNet.VB.Controls" Namespace="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)
    
    ' Verify attribute values.
    Dim p As ParseChildrenAttribute = _
    Attribute.GetCustomAttribute(GetType(CollectionPropertyControl), _
    GetType(ParseChildrenAttribute))

    Dim sb As New StringBuilder()
    sb.Append("The DefaultProperty property is " & p.DefaultProperty.ToString() & "<br />")
    sb.Append("The ChildrenAsProperties property is " & p.ChildrenAsProperties.ToString() & "<br />")
    sb.Append("The IsDefaultAttribute method returns " & p.IsDefaultAttribute().ToString())
    Message.Text = sb.ToString()

  End Sub
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
    <title>PersistChildrenAttribute</title>
</head>
<body>
    <form id="Form1" runat="server">
    <div>
      <asp:Label ID="Message"
                 runat="server"/>
      <AspSample:CollectionPropertyControl id="CollectionPropertyControl1" 
                                           runat="server">
        <AspSample:Employee Name="Employee 1" 
                            Title="Title 1" 
                            Alias="Alias 1" />
        <AspSample:Employee Name="Employee 2" 
                            Title="Title 2" 
                            Alias="Alias 2" />
      </AspSample:CollectionPropertyControl>
    </div>
    </form>
</body>
</html>

설명

ParseChildrenAttribute 클래스를 사용하면 서버 컨트롤을 메타데이터 특성으로 표시하여 사용자 지정 서버 컨트롤에 대한 구문 분석 논리를 ParseChildrenAttribute 지정할 수 있습니다.

서버 컨트롤을 메타데이터 특성 ParseChildren(true) 으로 표시하면 파서가 서버 컨트롤의 태그에 포함된 요소를 속성으로 해석하도록 지시합니다. 이 시나리오에서 속성은 ChildrenAsProperties .입니다 true.

서버 컨트롤을 메타데이터 특성 ParseChildren(true,"<Default Property>") 으로 표시하면 속성이 특성에 전달되는 속성의 이름으로 설정 DefaultProperty 됩니다.

서버 컨트롤을 메타데이터 특성 ParseChildren(false)(기본값)으로 표시하면 서버 컨트롤의 태그에 포함된 요소를 연결된 ControlBuilder 컨트롤과 구문 분석할 콘텐츠로 해석하도록 파서에 지시합니다. 이 시나리오에서 속성은 ChildrenAsProperties .입니다 false.

특성 사용에 대한 자세한 내용은 특성을 참조 하세요.

생성자

ParseChildrenAttribute()

ParseChildrenAttribute 클래스의 새 인스턴스를 초기화합니다.

ParseChildrenAttribute(Boolean)

서버 컨트롤에 포함된 요소를 서버 컨트롤의 속성으로 구문 분석할지 여부를 결정하기 위해 ParseChildrenAttribute 속성을 사용하여 ChildrenAsProperties 클래스의 새 인스턴스를 초기화합니다.

ParseChildrenAttribute(Boolean, String)

childrenAsPropertiesdefaultProperty 매개 변수를 사용하여 ParseChildrenAttribute 클래스의 새 인스턴스를 초기화합니다.

ParseChildrenAttribute(Type)

서버 컨트롤에 포함된 요소 중에서 컨트롤로 구문 분석할 요소를 확인하기 위해 ParseChildrenAttribute 속성을 사용하여 ChildControlType 클래스의 새 인스턴스를 초기화합니다.

필드

Default

ParseChildrenAttribute 클래스의 기본값을 정의합니다. 이 필드는 읽기 전용입니다.

ParseAsChildren

서버 컨트롤에 포함된 중첩 내용이 컨트롤로 구문 분석됨을 나타냅니다.

ParseAsProperties

서버 컨트롤에 포함된 중첩 내용이 컨트롤의 속성으로 구문 분석됨을 나타냅니다.

속성

ChildControlType

허용되는 컨트롤 형식을 나타내는 값을 가져옵니다.

ChildrenAsProperties

서버 컨트롤에 포함된 요소를 속성으로 구문 분석할지 여부를 나타내는 값을 가져오거나 설정합니다.

DefaultProperty

요소가 구문 분석되는 서버 컨트롤의 기본 속성을 가져오거나 설정합니다.

TypeId

파생 클래스에서 구현된 경우 이 Attribute에 대한 고유 식별자를 가져옵니다.

(다음에서 상속됨 Attribute)

메서드

Equals(Object)

지정된 개체가 현재 개체와 같은지 확인합니다.

GetHashCode()

ParseChildrenAttribute 개체의 해시 함수로 사용됩니다.

GetType()

현재 인스턴스의 Type을 가져옵니다.

(다음에서 상속됨 Object)
IsDefaultAttribute()

ParseChildrenAttribute 클래스의 현재 인스턴스 값이 파생 클래스의 기본값인지 여부를 나타내는 값을 반환합니다.

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)

적용 대상

추가 정보