System.Web.UI Namespace ControlCollection Class
Removes all Control objects from the collection.
[ VB ]
Overridable Public Sub Clear ( )
[ C# ]
public virtual void Clear ( );
[ C++ ]
public: virtual void Clear ( );
[ JScript ]
public function Clear ( );
Use the Clear method to remove all Control objects from the current server control's ControlCollection object.
This method is commonly used when developing composite, templated controls or templated data bound controls, so that outdated objects in the control's ControlCollection are not displayed inappropriately.
NOTE: The Count property is automatically set to 0 after this method is used.
The following example demonstrate how to use the Clear method to empty the ControlCollection before adding new items to the collection.
// 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.Add Controls.AddAt Controls.Remove Controls.RemoveAt