Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All

ListItemCollection.Clear Method

Removes all ListItem objects from the collection.

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

public void Clear()

Implements

IList.Clear()

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

Caution noteCaution

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.
ListBox1.Items.Clear();
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

Supported in: 4.6, 4.5, 4, 3.5, 3.0, 2.0, 1.1
Show:
© 2015 Microsoft