ControlCollection.Clear Method
Removes all controls from the current server control's ControlCollection object.
[Visual Basic] Public Overridable Sub Clear() [C#] public virtual void Clear(); [C++] public: virtual void Clear(); [JScript] public function Clear();
Remarks
Use this method to empty a custom control's ControlCollection when you override the Control.CreateChildControls and DataBind methods. Do this when you develop composite, templated controls or templated data bound controls. For more information, see Methods in ASP.NET Server Controls.
Example
The following example demonstrates overriding the CreateChildControls method, and using the Clear method to delete all child controls previously in the ControlCollection object. In this case, you must do this so that outdated objects in your control's ControlCollection are not displayed inappropriately.
[Visual Basic] ' 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 RepeaterItemVB = New RepeaterItemVB(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 [C#] // 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); } } } [C++] // Override to create repeated items. void CreateChildControls() { Object* o = ViewState->Item[S"NumItems"]; if (o != 0) { // Clear any existing child controls. Controls->Clear(); int numItems = *dynamic_cast<int __gc *>(o); for (int i = 0; i < numItems; i++) { // Create an item. RepeaterItem* item = new RepeaterItem(i, 0); // Initialize the item from the template. ItemTemplate->InstantiateIn(item); // Add the item to the ControlCollection. Controls->Add(item); } } } [JScript] // 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); } } }
Requirements
Platforms: Windows 2000, Windows XP Professional, Windows Server 2003 family
See Also
ControlCollection Class | ControlCollection Members | System.Web.UI Namespace | Controls | Developing ASP.NET Server Controls