ListItemCollection.Clear Method ()


The .NET API Reference documentation has a new home. Visit the .NET API Browser on to see the new experience.

Removes all ListItem objects from the collection.

Namespace:   System.Web.UI.WebControls
Assembly:  System.Web (in System.Web.dll)

public void Clear()



Use the Clear method to remove all ListItem objects. The Count property is also set to 0.


When you use this method, all ListItem objects are removed from the collection. You cannot undo this.

// Copy the items in the ListItemCollection to an array before 
// deleting them.     
ListItem[] myListItemArray = new ListItem[ListBox1.Items.Count];
ListBox1.Items.CopyTo(myListItemArray, 0);

// Delete all the items from the ListBox.
DeleteLabel.Text = "<b>All items in the ListBox were deleted successfully." 
    + "</b><br /><b>The deleted items are:";
String listResults="";
    foreach(ListItem myItem in myListItemArray)
        listResults = listResults + myItem.Text + "<br />";
ResultsLabel.Text = listResults;

.NET Framework
Available since 1.1
Return to top