.NET Framework Class Library
ParseChildrenAttribute Class

Defines a metadata attribute that you can use when developing ASP.NET server controls. Use the ParseChildrenAttribute class to indicate how the page parser should treat content nested inside a server control tag declared on a page. This class cannot be inherited.

Inheritance Hierarchy
SystemObject
   SystemAttribute
    System.Web.UIParseChildrenAttribute

Namespace:   System.Web.UI
Assembly:  System.Web (in System.Web.dll)
Syntax
<[%$TOPIC/ytc87195_en-us_VS_110_2_0_0_0_0%]([%$TOPIC/ytc87195_en-us_VS_110_2_0_0_0_1%].Class)> _
Public NotInheritable Class ParseChildrenAttribute _
	Inherits [%$TOPIC/ytc87195_en-us_VS_110_2_0_0_0_2%]
[[%$TOPIC/ytc87195_en-us_VS_110_2_0_1_0_0%]([%$TOPIC/ytc87195_en-us_VS_110_2_0_1_0_1%].Class)]
public sealed class ParseChildrenAttribute : [%$TOPIC/ytc87195_en-us_VS_110_2_0_1_0_2%]
[[%$TOPIC/ytc87195_en-us_VS_110_2_0_2_0_0%]([%$TOPIC/ytc87195_en-us_VS_110_2_0_2_0_1%]::Class)]
public ref class ParseChildrenAttribute sealed : public [%$TOPIC/ytc87195_en-us_VS_110_2_0_2_0_2%]
[<[%$TOPIC/ytc87195_en-us_VS_110_2_0_3_0_0%]>]
[<[%$TOPIC/ytc87195_en-us_VS_110_2_0_3_0_1%]([%$TOPIC/ytc87195_en-us_VS_110_2_0_3_0_2%].Class)>]
type ParseChildrenAttribute =  
    class 
        inherit [%$TOPIC/ytc87195_en-us_VS_110_2_0_3_0_3%] 
    end

The ParseChildrenAttribute type exposes the following members.

Constructors
  NameDescription
Public method ParseChildrenAttributeInitializes a new instance of the ParseChildrenAttribute class.
Public method ParseChildrenAttribute(Boolean)Initializes a new instance of the ParseChildrenAttribute class using the ChildrenAsProperties property to determine if the elements that are contained within a server control are parsed as properties of the server control.
Public method ParseChildrenAttribute(Type)Initializes a new instance of the ParseChildrenAttribute class using the ChildControlType property to determine which elements that are contained within a server control are parsed as controls.
Public method ParseChildrenAttribute(Boolean, String)Initializes a new instance of the ParseChildrenAttribute class using the childrenAsProperties and defaultProperty parameters.
Top
Properties
  NameDescription
Public property ChildControlTypeGets a value indicating the allowed type of a control.
Public property ChildrenAsPropertiesGets or sets a value indicating whether to parse the elements that are contained within a server control as properties.
Public property DefaultPropertyGets or sets the default property for the server control into which the elements are parsed.
Public property TypeIdWhen implemented in a derived class, gets a unique identifier for this Attribute. (Inherited from Attribute.)
Top
Methods
  NameDescription
Public method EqualsDetermines whether the specified object is equal to the current object. (Overrides AttributeEquals(Object).)
Public method GetHashCodeServes as a hash function for the ParseChildrenAttribute object. (Overrides AttributeGetHashCode.)
Public method GetTypeGets the Type of the current instance. (Inherited from Object.)
Public method IsDefaultAttributeReturns a value indicating whether the value of the current instance of the ParseChildrenAttribute class is the default value of the derived class. (Overrides AttributeIsDefaultAttribute.)
Public method MatchWhen overridden in a derived class, returns a value that indicates whether this instance equals a specified object. (Inherited from Attribute.)
Public method ToStringReturns a string that represents the current object. (Inherited from Object.)
Top
Fields
  NameDescription
Public field Static member DefaultDefines the default value for the ParseChildrenAttribute class. This field is read-only.
Public field Static member ParseAsChildrenIndicates that the nested content that is contained within the server control is parsed as controls.
Public field Static member ParseAsPropertiesIndicates that the nested content that is contained within a server control is parsed as properties of the control.
Top
Explicit Interface Implementations
  NameDescription
Explicit interface implemetation Private method _AttributeGetIDsOfNamesMaps a set of names to a corresponding set of dispatch identifiers. (Inherited from Attribute.)
Explicit interface implemetation Private method _AttributeGetTypeInfoRetrieves the type information for an object, which can be used to get the type information for an interface. (Inherited from Attribute.)
Explicit interface implemetation Private method _AttributeGetTypeInfoCountRetrieves the number of type information interfaces that an object provides (either 0 or 1). (Inherited from Attribute.)
Explicit interface implemetation Private method _AttributeInvokeProvides access to properties and methods exposed by an object. (Inherited from Attribute.)
Top
Remarks

The ParseChildrenAttribute class allows you to specify parsing logic for a custom server control by marking the server control with the ParseChildrenAttribute metadata attribute.

Marking your server control with the metadata attribute ParseChildren(true) instructs the parser to interpret the elements that are contained within the server control's tags as properties. In this scenario, the ChildrenAsProperties property is true.

Marking your server control with the metadata attribute ParseChildren(true,"<Default Property>") sets the DefaultProperty property to the name of the property that is passed into the attribute.

Marking your server control with the metadata attribute ParseChildren(false), the default value, instructs the parser to interpret the elements that are contained within the server control's tags as content that will be parsed with an associated ControlBuilder that is, as controls. In this scenario, the ChildrenAsProperties property is false.

For information about using attributes, see Extending Metadata Using Attributes.

Examples

The code example in this section contains two parts. The first code example demonstrates how to set properties for the ParseChildrenAttribute class. The second code example demonstrates how to use classes in an ASP.NET page.

The following code example demonstrates how to set the ParseChildrenAttribute object of a custom server control named CollectionPropertyControl. The ParseChildrenAttribute sets the ChildrenAsProperties property to true and the DefaultProperty property to the Employee class.

Imports System
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 'New 


        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 'New 

        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 'Employee
    ' 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 'CreateChildControls 
    End Class  
End Namespace
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);

      }
   }
}

The following code example demonstrates how to use the CollectionPropertyControl and Employee classes in an ASP.NET page. Instances of the Employee class are added declaratively.

<%@ 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>
<%@ 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>
Version Information

.NET Framework

Supported in: 4.5, 4, 3.5, 3.0, 2.0, 1.1, 1.0
Platforms

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Thread Safety
Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.