System.Web.UI Namespace ControlCollection Class
Adds a specified control to a ControlCollection.
[ VB ]
Public Sub Add ( _
ByVal child As Control _
)
[ C# ]
public virtual void Add (
Control child
);
[ C++ ]
public: virtual void Add (
Control* child
);
[ JScript ]
public function Add (
child : Control
) : void;
- child
- The Control to add to the collection.
Use this method to add the specified Control to the end of a ControlCollection. The control can be an instance of any ASP.NET server control, a literal control, or a custom server control that you create.
To insert a Control at a specific location in the collection, use the AddAt method.
The following example uses the Add method to programmatically add a series of template items at run time, the number of which are taken from the server control's view state, to a custom templated control.
// Override to create repeated items.
protected override void CreateChildControls ( ) {
object o = ViewState [ "NumItems" ];
if ( o != null ) {
// clear any existing child controls.
Controls.Clear ( );
int numItems = ( int ) o;
for ( int i=0; i < numItems; i++ ) {
// create an item.
RepeaterItem item = new RepeaterItem ( i, null );
// initialize the item from the template.
ItemTemplate.InstantiateIn ( item );
// add the item to the ControlCollection.
Controls.Add ( item );
}
}
}
' Override to create repeated items.
Protected Overrides Sub CreateChildControls ( )
Dim O As Object = ViewState ( "NumItems" )
If Not ( O Is Nothing )
' clear any existing child controls.
Controls.Clear ( )
Dim I As Integer
Dim NumItems As Integer = CInt ( O )
For I = 0 To NumItems - 1
' create an item.
Dim Item As RepeaterItem = New RepeaterItem ( I, Nothing )
' initialize the item from the template.
ItemTemplate.InstantiateIn ( Item )
' add the item to the ControlCollection.
Controls.Add ( Item )
Next
End If
End Sub
// Override to create repeated items.
protected override function CreateChildControls ( ) {
var o = ViewState [ "NumItems" ];
if ( o != null ) {
// clear any existing child controls
Controls.Clear ( );
var numItems : int = int ( o );
for ( var i : int =0; i < numItems; i++ ) {
// create an item.
var item : RepeaterItem = new RepeaterItem ( i, null );
// initialize the item from the template.
ItemTemplate.InstantiateIn ( item );
// add the item to the ControlCollection.
Controls.Add ( item );
}
}
} |
|
C# |
VB |
JScript |
ControlCollection Members Controls.AddAt Controls.Remove Controls.RemoveAt Controls.Clear