ListItemCollection.Remove Method (String)
.NET Framework 3.0
Removes a ListItem from the collection that represents the specified string.
Namespace: System.Web.UI.WebControls
Assembly: System.Web (in system.web.dll)
Assembly: System.Web (in system.web.dll)
Use the Remove method to remove a ListItem object from the collection. This implementation of the method creates a ListItem object using the text in the item parameter and then removes this ListItem from the collection. The specified item parameter must match the Value property of an existing ListItem object, or no item is removed from the collection.
The following code example demonstrates the Remove method the ListItemCollection class. It creates a ListBox control with some list items in it. The user enters the text of the item to delete in a TextBox control named Delete. The Click event handler of the Button1 control deletes the selected item from the ListBox control.
ListItem myListItem = new ListItem(Delete.get_Text().ToLower(),
Delete.get_Text().ToLower());
// Check whether the 'ListItem' is present in the 'ListBox' or not.
if(itemCollection.Contains(myListItem)) {
String deleteString=Delete.get_Text();
// Delete the listitem entered by the user in textfield.
itemCollection.Remove(deleteString.ToLower());
Message.set_Text("<font color='green'><b>Deleted "
+ "Successfully</b></font>");
}
else {
Message.set_Text("<font color='red'><b>No ListItem with the "
+ "given name is present in the ListBox for "
+ "deletion.</b></font>");
}
Community Additions
ADD
Show: