System.Web.UI Namespace Control Class
Returns the collection of child controls for a specified server control in the UI hierarchy. Also used to add or remove individual control items in the collection.
Script |
[ ControlCollection var = ] Control.Controls |
This property can only be used programmatically; it cannot be set when declaring the control.
A ControlCollection object that represents the child controls for the specified server control.
On an ASP.NET page, when controls are added declaratively between the opening and closing tags of a server control, ASP.NET automatically adds the controls to the containing server control's ControlCollection. Any HTML tags or text strings that are not processed on the server are treated as LiteralControl objects, and are added to the collection like other server controls.
The Controls property allows programmatic access to the instance of the ControlCollection class for any server control. Like any collection in the .NET Framework, you can add to, remove from, or iterate through the list of items in the collection.
The following example demonstrates how to dynamically add child controls ( TableRow and TableCell controls ) to a Table control using its Controls property.
int numrows=int.Parse ( DropDown1.SelectedItem.Value );
int numcells=int.Parse ( DropDown2.SelectedItem.Value );
for ( int j=0; j<numrows; j++ ) {
TableRow r=new TableRow ( );
for ( int i=0; i<numcells; i++ ) {
TableCell c=new TableCell ( );
c.Controls.Add ( new LiteralControl ( "row " + j.ToString ( ) + ", cell " + i.ToString ( ) ) );
r.Cells.Add ( c );
}
myTable.Rows.Add ( r );
}
Show me
Control Members ControlCollection