Yes, but you may have to manage the process if you're overriding the render methods or if using a custom collection for the children.
You have to add the controls to the parents
Controls
collection otherwise the
Init
and
Load
events wont fire properly in the child controls.
public List<control> MyChildren { get; set; }
public override void Load(object sender, EventArgs e)
{
foreach(Control c in MyChildren)
this.Controls.Add(c);
}
</control>
Then you can render out your children.
public override void Render(HtmlWriter writer)
{
writer.Write("<div>");
foreach(Control c in MyChildren)
c.Render(writer);
writer.Write("</div>");
}
This approach allows you to maintain more complex control hierarchies than simple parent, child controls.
---------- Update ----------
The MyChildren property declaration works exactly the same way as doing this:
private List<Control> _myChildren;
public List<Control> MyChildren
{
get { return _myChildren; }
set { _myChildren = value; }
}
Although for a control collection you'd probably want to protect the value and ensure it's instantiated.
private List<Control> _myChildren;
public List<Control> MyChildren
{
get { return _myChildren ?? (_myChildren = new List<Control>()); }
}