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.
 
HTML Markup
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:ListBox>
<br />
<br />
<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.
C#
protected void DeleteValues(object sender, EventArgs e)
{
    List<ListItem> deletedItems = new List<ListItem>();
    foreach (ListItem item in lstFruits.Items)
    {
        if (item.Selected)
        {
            deletedItems.Add(item);
        }
    }
 
    foreach (ListItem item in deletedItems)
    {
        lstFruits.Items.Remove(item);
    }
}
 
VB.Net
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
            deletedItems.Add(item)
        End If
    Next
 
    For Each item As ListItem In deletedItems
        lstFruits.Items.Remove(item)
    Next
End Sub
 
 
Screenshot
Delete (remove) multiple items from ListBox on Button click in ASP.Net using C# and VB.Net
 
 
Demo
 
 
Downloads