ParseChildrenAttribute Constructor (Boolean, String)


The .NET API Reference documentation has a new home. Visit the .NET API Browser on to see the new experience.

Initializes a new instance of the ParseChildrenAttribute class using the childrenAsProperties and defaultProperty parameters.

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

public ParseChildrenAttribute(
	bool childrenAsProperties,
	string defaultProperty


Type: System.Boolean

true to parse the elements as properties of the server control; otherwise, false.

Type: System.String

A string that defines a collection property of the server control into which nested content is parsed by default.

If childrenAsProperties is false, the elements are parsed as controls.

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.

// 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 sealed class CollectionPropertyControl : Control
   private String header;
   private ArrayList employees = new ArrayList();

   public String Header
         return header;
         header = value;

   public ArrayList 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 override void CreateChildControls()
      Label label = new Label();
      label.Text = Header;
      label.BackColor = System.Drawing.Color.Beige;
      label.ForeColor = System.Drawing.Color.Red;
      Controls.Add(new LiteralControl("<BR> <BR>"));

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

      TableHeaderCell hcell1 = new TableHeaderCell();    
      hcell1.Text = "Name";

      TableHeaderCell hcell2 = new TableHeaderCell();
      hcell2.Text = "Title";

      TableHeaderCell hcell3 = new TableHeaderCell();
      hcell3.Text = "Alias";

      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;

         TableCell cell2 = new TableCell();
         cell2.Text = employee.Title;

         TableCell cell3 = new TableCell();
         cell3.Text = employee.Alias;



The following code example demonstrates how to use the CollectionPropertyControl and Employee classes in an ASP.NET page.

<%@ 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" "">

<script runat="server">

  protected void Page_Load(object sender, EventArgs e)

    // Verify attribute values.
    ParseChildrenAttribute p = 

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


<html xmlns="" >
<head runat="server">
    <title>ParseChildrenAttribute Example</title>
    <form id="form1" runat="server">
      <asp:Label ID="Message"
      <AspSample:CollectionPropertyControl id="CollectionPropertyControl1" 
        <AspSample:Employee Name="Employee 1" 
                            Title="Title 1" 
                            Alias="Alias 1" />
        <AspSample:Employee Name="Employee 2" 
                            Title="Title 2" 
                            Alias="Alias 2" />

.NET Framework
Available since 1.1
Return to top