In this article I will explain how to dynamically delete (remove) multiple selected items from ListBox control on Button click in ASP.Net using C# and VB.Net.
The HTML Markup consists of an ASP.Net ListBox control and a Button. I have set the SelectionMode property as Multiple for the ListBox in order to select and delete multiple items.
<asp:ListBox ID="lstFruits" runat="server" Width = "150" Height = "60" SelectionMode = "Multiple">
<asp:ListItem Text = "Mango" Value = "1"></asp:ListItem>
<asp:ListItem Text = "Apple" Value = "2"></asp:ListItem>
<asp:ListItem Text = "Banana" Value = "3"></asp:ListItem>
<asp:ListItem Text = "Guava" Value = "4"></asp:ListItem>
<asp:ListItem Text = "Pineapple" Value = "5"></asp:ListItem>
<asp:ListItem Text = "Papaya" Value = "6"></asp:ListItem>
<asp:ListItem Text = "Grapes" Value = "7"></asp:ListItem>
<asp:Button Text="Delete" runat="server" OnClick = "DeleteValues" />
Dynamically deleting (removing) multiple items from ListBox control in ASP.Net
When the Delete button is clicked, a loop is executed over the ListBox items and each selected ListBox item is added to a Generic List of ListItem class.
Note: It is not permitted to remove an item from a collection inside its own loop and hence we need to create another collection to hold the Items to be deleted from the ListBox control.
Finally another loop is executed over the objects of the Generic List and the ListBox items are removed from the ListBox control.
protected void DeleteValues(object sender, EventArgs e)
List<ListItem> deletedItems = new List<ListItem>();
foreach (ListItem item in lstFruits.Items)
foreach (ListItem item in deletedItems)
Protected Sub DeleteValues(sender As Object, e As EventArgs)
Dim deletedItems As New List(Of ListItem)()
For Each item As ListItem In lstFruits.Items
If item.Selected Then
For Each item As ListItem In deletedItems