Click here to Skip to main content
Click here to Skip to main content

Tagged as

Go to top

Remove multiple items from ListBox

, 9 Feb 2011
Rate this:
Please Sign up or sign in to vote.
There is no direct way to remove multiple selected items from ListBox when SelectionMode of ListBox is set to "Multiple". So there is a small solution to handle this situation.In following Example ListBox "lstCity" contains multiple cities and SelectionMode is set "Multiple".
There is no direct way to remove multiple selected items from ListBox when SelectionMode of ListBox is set to "Multiple". So there is a small solution to handle this situation.
 
In following Example ListBox "lstCity" contains multiple cities and SelectionMode is set "Multiple".
 
<!--List contains cities and SelectionMode is set to "Multiple"-->
    <asp:ListBox ID="lstboxTest" runat="server" SelectionMode=Multiple>
        <asp:ListItem>Mumbai</asp:ListItem>
        <asp:ListItem>Delhi</asp:ListItem>
        <asp:ListItem>Kolkata</asp:ListItem>
        <asp:ListItem>Pune</asp:ListItem>
        <asp:ListItem>Chennai</asp:ListItem>
        <asp:ListItem>Banglore</asp:ListItem>
        <asp:ListItem>Noida</asp:ListItem>
        <asp:ListItem>Gurgaon</asp:ListItem>
    </asp:ListBox>
 
  
    <!--On click of button remove all selected Cities from list-->
    <asp:Button ID="btnRemove" runat="server" Text="Remove" />
 
In Codebehind add following namespace at the top of the page.
 
using System.Collections.Generic; 
 
Write following lines of code to remove multiple Items from ListBox "lstCity" inside Click event of btnRemove.
 
1. Create List of ListItem "lstSelectedCities".
 
2. Loop through the ListBox lstCity's Items collection and add selected ListItem in the List "lstSelectedCities".
 
3. Loop through the List "lstSelectedCities" and remove ListItems from ListBox "lstCity" that are in lstSelectedCities List by using lstCity.Items.Remove(ListItems);
 
protected void btnRemove_Click(object sender, EventArgs e)
{
        //1. Create a List of ListItem
        List<ListItem> lstSelectedCities = new List<ListItem>();
   
        //2. Loop through lstCity's Item Collection
        // Add selected ListItem to the List "lstSelectedCities".
        foreach (ListItem liItems in lstCity.Items)
        {
            if (liItems.Selected == true)
            {
                lstSelectedCities.Add(liItems);
            }
        }
    
        //3. Loop through the List "lstSelectedCities" and
        // remove ListItems from ListBox "lstCity" that are in 
        // lstSelectedCities List
        foreach (ListItem liSelected in lstSelectedCities)
        {
            lstCity.Items.Remove(liSelected);
        }
}

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Nidhi P Bhargava
Software Developer (Senior)
India India
No Biography provided

Comments and Discussions

 
GeneralReason for my vote of 3 kl;kl;kl;kl; Pinmemberswatadas21-Nov-11 19:06 
Reason for my vote of 3
kl;kl;kl;kl;
Generalits cool, but i faced an error because of you put the id in ... Pinmemberashokkumar s s27-Mar-11 3:25 
GeneralReason for my vote of 4 Good one...quick and intelligent PinmemberRajibdotnet0513-Sep-10 4:50 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web01 | 2.8.140922.1 | Last Updated 9 Feb 2011
Article Copyright 2010 by Nidhi P Bhargava
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid