ParseChildrenAttribute Class
Assembly: System.Web (in system.web.dll)
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.
| Topic | Location |
|---|---|
| Walkthrough: Developing and Using a Custom Server Control | Authoring ASP.NET Controls |
| Walkthrough: Developing and Using a Custom Server Control | Authoring ASP.NET 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.
package ParseChildrenSamples;
// 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.
import System.*;
import System.Collections.*;
import System.Web.*;
import System.Web.UI.*;
import System.Web.UI.WebControls.*;
// The child element class.
public class Employee
{
private String name;
private String title;
private String alias;
public Employee()
{
this("", "", "");
} //Employee
public Employee(String name, String title, String alias)
{
this.name = name;
this.title = title;
this.alias = alias;
} //Employee
/** @property
*/
public String get_Name()
{
return name;
} //get_Name
/** @property
*/
public void set_Name(String value)
{
name = value;
} //set_Name
/** @property
*/
public String get_Title()
{
return title;
} //get_Title
/** @property
*/
public void set_Title(String value)
{
title = value;
} //set_Title
/** @property
*/
public String get_Alias()
{
return alias;
} //get_Alias
/** @property
*/
public void set_Alias(String value)
{
alias = value;
} //set_Alias
} //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.
/** @attribute ParseChildren(true, "Employees")
*/
public class CollectionPropertyControl extends Control
{
private String header;
private ArrayList employees = new ArrayList();
/** @property
*/
public String get_Header()
{
return header;
} //get_Header
/** @property
*/
public void set_Header(String value)
{
header = value;
} //set_Header
/** @property
*/
public ArrayList get_Employees()
{
return employees;
} //get_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.set_Text(get_Header());
label.set_BackColor(System.Drawing.Color.get_Beige());
label.set_ForeColor(System.Drawing.Color.get_Red());
get_Controls().Add(label);
get_Controls().Add(new LiteralControl("<BR> <BR>"));
Table table = new Table();
TableRow htr = new TableRow();
TableHeaderCell hCell1 = new TableHeaderCell();
hCell1.set_Text("Name");
htr.get_Cells().Add(hCell1);
TableHeaderCell hCell2 = new TableHeaderCell();
hCell2.set_Text("Title");
htr.get_Cells().Add(hCell2);
TableHeaderCell hCell3 = new TableHeaderCell();
hCell3.set_Text("Alias");
htr.get_Cells().Add(hCell3);
table.get_Rows().Add(htr);
table.set_BorderWidth(new Unit(2));
table.set_BackColor(System.Drawing.Color.get_Beige());
table.set_ForeColor(System.Drawing.Color.get_Red());
Employee employee = null;
for (int iCtr = 0; iCtr < get_Employees().get_Count(); iCtr++) {
employee = (Employee)get_Employees().get_Item(iCtr);
TableRow tr = new TableRow();
TableCell cell1 = new TableCell();
cell1.set_Text(employee.get_Name());
tr.get_Cells().Add(cell1);
TableCell cell2 = new TableCell();
cell2.set_Text(employee.get_Title());
tr.get_Cells().Add(cell2);
TableCell cell3 = new TableCell();
cell3.set_Text(employee.get_Alias());
tr.get_Cells().Add(cell3);
table.get_Rows().Add(tr);
}
get_Controls().Add(table);
} //CreateChildControls
} //CollectionPropertyControl
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.
- AspNetHostingPermission for operating in a hosted environment. Demand value: LinkDemand; Permission value: Minimal.