Monday, April 26, 2010

Delete asp.net Listbox item...(Multiple Items) !!

 Suppose  ListBox1 be the list box with some items in it we can delete the selected items in the Listbox with the below code..

for Multiple selection, enable the Multiple option of the List box to "Multiple"..


        for (int i = 0; i < ListBox1.Items.Count; i++)
        {
            int myindx = ListBox1.SelectedIndex;
            if (myindx >= 0)
            {
                if (ListBox1.Items[myindx].Selected == true)
                {
                    ListBox1.Items.RemoveAt(myindx);

                }
            }
        }

6 comments:

  1. yea its working ...
    all left listbox items are removing by this way..
    thanx yaar

    ReplyDelete
  2. This comment has been removed by the author.

    ReplyDelete
  3. Why can'e we use foreach loop instead of for loop?
    foreach has better performance I think...

    ReplyDelete
  4. looping take an amount of system time whether for or foreach

    ReplyDelete