ParseChildrenAttribute Sample

The sample control described here shows how to apply the ParseChildrenAttribute to a control to specify a default property and how this attribute simplifies the declarative syntax for setting the default property. The default property is generally a collection type and can hold items of any system type or custom type.

The sample control (described later in this topic) is marked as follows:

[ParseChildren(true, "Employees")]
public class CollectionPropertyControl : Control {...} 

Employees is a property of type System.Collections.ArrayList and holds items of a custom type (Employee).

When the control is marked with the ParseChildrenAttribute, the declarative syntax for adding Employee objects to the Employees property is as follows:

<Custom:CollectionPropertyControl runat = "server">
<Custom:Employee Name = "Alice" Alias = "AliceA" Title = "Manager" />
<Custom:Employee Name = "Jerry" Alias = "JerryR" Title = "Programmer" />
<Custom:Employee Name = "Lynn" Alias = "LynnP" Title = "Architect" />
<Custom:Employee Name = "Mike" Alias = "MikeB" Title = "Tester" />
</Custom:CollectionPropertyControl> 

Note that the property name is not specified within the control's tags.

<Custom:CollectionPropertyControl runat = "server">
<%-- <Employees> not specified --%>
<Custom:Employee Name = "Alice" Alias = "AliceA" Title = "Manager" />
...
<Custom:Employee Name = "Mike" Alias = "MikeB" Title = "Tester" />
<%-- </Employees> not specified --%>
</Custom:CollectionPropertyControl>

ParseChildrenAttribute thus enables a simpler declarative syntax for adding items to a default property. When the default property is a collection type, the page parser creates child elements (specified within a control's tags) and adds them to the collection. A sample page that shows usage syntax is provided at the end of this topic.

Code for the custom control and for the custom type (of the collection items) follows. To build this sample, see the instructions in the Server Control Samples.

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

namespace CustomControls
{
   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;
         }
      }
   }
   
   [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;
         }
      }

      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);
         
      }
   }
}
[Visual Basic]
Option Explicit
Option Strict

Imports System
Imports System.Collections
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls

Namespace CustomControls
   Public Class Employee
      Private _name As String
      Private _title As String
      Private _alias As String
      
      Public Sub New()
         MyClass.New("", "", "")
      End Sub
       
      Public Sub New(name As String, title As String, 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
            _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
   
   <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
      
      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

Test Page for the Default Property Control

The following ASP.NET page uses the custom control CollectionPropertyControl and shows how to add items to the collection property declaratively.

<%@ Register TagPrefix="Custom" Namespace="CustomControls" Assembly = "CustomControls" %>
<html>      
<body>         
<form runat=server>              
<Custom:CollectionPropertyControl Header = "Employees" id = "prop" runat = "server">
<Custom:Employee Name = "Alice" Alias = "AliceA" Title = "Manager" />
<Custom:Employee Name = "Jerry" Alias = "JerryR" Title = "Programmer" />
<Custom:Employee Name = "Lynn" Alias = "LynnP" Title = "Architect" />
<Custom:Employee Name = "Mike" Alias = "MikeB" Title = "Tester" />
</Custom:CollectionPropertyControl>                                     
</form>                       
</body>                    
</html>                 

See Also

Using ParseChildrenAttribute | Control Parsing, ParseChildrenAttribute, and Control Builders