.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Code Snippets >> ASP.NET Controls >> Post New Resource Bookmark and Share   

 Subscribe to Code Snippets

Remove multiple items from ListBox

Posted By:Nidhi Bhargava       Posted Date: May 04, 2010    Points: 10    Category: ASP.NET Controls    URL: http://www.dotnetspark.com  

There is no direct way to remove multiple selected items from ListBox when SelectionMode of ListBox is "Multiple". So there is a small solution to handle this situation.
 

1. Add following namespace at the top of the page -

using System.Collections.Generic;

2. Create list of ListItem inside your function

List lstSelected = new List;

3. Loop through the ListBox's Items collection say ListBox Name is "lstboxTest" and add selected list item in List.


foreach (ListItem liItems in lstboxTest.Items)
{
   if (liItems.Selected == true)
   {
       stSelected.Add(liItems);
   }
}


4. Now loop through list of selected items lstSelected and remove all ListItem from ListBox that are in lstSelected.


foreach (ListItem liSelected in lstSelected)
{

     lstboxTest.Items.Remove(liSelected);

}


     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More code samples in C#, ASP.Net, Vb.Net and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend