This documentation is archived and is not being maintained.

ParseChildrenAttribute Class

Defines a metadata attribute that you can use when developing ASP.NET server controls. Use it to indicate whether XML elements nested within the server control's tags, when the control is used declaratively on a page, should be treated as properties or as child controls. This class cannot be inherited.

For a list of all members of this type, see ParseChildrenAttribute Members.

System.Object
   System.Attribute
      System.Web.UI.ParseChildrenAttribute

[Visual Basic]
<AttributeUsage(AttributeTargets.Class)>
NotInheritable Public Class ParseChildrenAttribute
   Inherits Attribute
[C#]
[AttributeUsage(AttributeTargets.Class)]
public sealed class ParseChildrenAttribute : Attribute
[C++]
[AttributeUsage(AttributeTargets::Class)]
public __gc __sealed class ParseChildrenAttribute : public
   Attribute
[JScript]
public
   AttributeUsage(AttributeTargets.Class)
class ParseChildrenAttribute extends Attribute

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.

Remarks

When a String is passed in this class's constructor, it defines the default property for the parent server control.

For information on how to use this class, see Using ParseChildrenAttribute.

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

Example

[Visual Basic] 
' When compiling this class, name it ParseChildrenVB.dll.
' Create a namespace that defines two classes: one is a custom control 
' named Employee, which is created for every instance of a child
' element with its name declared in a page associated with this namespace,
' and the other, named Employees, that contains these child elements.
Imports System
Imports System.Collections
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls

Namespace ParseChildrenVBSamples


' The child element class.

Public 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(name As String, title As String, 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
         _name = value
      End Set
   End Property
   
   
   Public Property Title() As String
      Get
         Return _title
      End Get
      Set
         _title = value
      End Set
   End Property
   
   
   Public Property [Alias]() As String
      Get
         Return _alias
      End Get
      Set
         _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")>  _
Public Class CollectionPropertyControl
   Inherits Control
   Private _header As String
   Private _employees As New ArrayList()
   
   
   Public Property Header() As String
      Get
         Return _header
      End Get
      Set
         _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 'CollectionPropertyControl
End Namespace 'ParseChildrenVBSamples

[C#] 
// When compiling this class, name it ParseChildren.dll.
// Create a namespace that defines two classes: one is a custom control 
// named Employee, which is created for every instance of a child
// element with its name declared in a page associated with this namespace,
// and the other, named Employees, that contains these child elements.
using System;
using System.Collections;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace ParseChildrenSamples
{
   // The child element class.
   public 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")]
   public 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);
         
      }
   }
}

[C++] 
// When compiling this class, name it ParseChildren.dll.
// Create a namespace that defines two classes: one is a custom control
// named Employee, which is created for every instance of a child
// element with its name declared in a page associated with this namespace,
// and the other, named Employees, that contains these child elements.
#using <mscorlib.dll>
#using <System.dll>
#using <System.Web.dll>
#using <System.Drawing.dll>

using namespace System;
using namespace System::Collections;
using namespace System::Web;
using namespace System::Web::UI;
using namespace System::Web::UI::WebControls;

// The child element class.
public __gc class Employee {
private:
   String*  name;
   String*  title;
   String*  alias;

public:
   Employee() {
      this->name = S"";
      this->title = S"";
      this->alias = S"";
   }

   Employee (String* name, String* title, String* alias) {
      this->name = name;
      this->title = title;
      this->alias = alias;
   }

   __property String* get_Name() {
      return name;
   }
   __property void set_Name(String* value) {
      name = value;
   }

   __property String* get_Title() {
      return title;
   }
   __property void set_Title(String* value) {
      title = value;
   }

   __property String* get_Alias() {
      return alias;
   }
   __property void set_Alias(String* value) {
      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, S"Employees")]
public __gc class CollectionPropertyControl : public Control {
private:
   String*  header;
   ArrayList* employees;
public:
   CollectionPropertyControl()
   {
      employees = new ArrayList();
   }
   __property String* get_Header() {
      return header;
   }
   __property void set_Header(String* value) {
      header = value;
   }

   __property ArrayList* get_Employees() {
      return employees;
   }

   // Override the CreateChildControls method to
   // add child controls to the Employees property when this
   // custom control is requested from a page.
protected:
   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(S"<BR> <BR>"));

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

      TableHeaderCell* hcell1 = new TableHeaderCell();
      hcell1->Text = S"Name";
      htr->Cells->Add(hcell1);

      TableHeaderCell* hcell2 = new TableHeaderCell();
      hcell2->Text = S"Title";
      htr->Cells->Add(hcell2);

      TableHeaderCell* hcell3 = new TableHeaderCell();
      hcell3->Text = S"Alias";
      htr->Cells->Add(hcell3);
      table->Rows->Add(htr);

      table->BorderWidth = 2;
      table->BackColor = System::Drawing::Color::Beige;
      table->ForeColor = System::Drawing::Color::Red;
      IEnumerator* myEnum = Employees->GetEnumerator();
      while (myEnum->MoveNext()) {
         Employee* employee = __try_cast<Employee*>(myEnum->Current);

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

   }
};

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Namespace: System.Web.UI

Platforms: Windows 2000, Windows XP Professional, Windows Server 2003 family

Assembly: System.Web (in System.Web.dll)

See Also

ParseChildrenAttribute Members | System.Web.UI Namespace | Attribute | Using ParseChildrenAttribute

Show: