asp.net.ph

ControlCollection.Clear Method

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 ( );

Remarks

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.

Example

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 );
      }
   }
}
  C# VB JScript

See Also

ControlCollection Members   Controls.Add   Controls.AddAt   Controls.Remove   Controls.RemoveAt Skip Navigation Links




Home
Suggested Reading


Previous page Back to top Next page

© 2000-2010 Rey Nuñez All rights reserved.

If you have any question, comment or suggestion
about this site, please send us a note

You can help support asp.net.ph