Thursday, January 24, 2013

Move ListBox selected Items to another ListBox in ASP.NET CSharp (C#)

Assume you have two  ListBoxes ListBox1 and ListBox2 in your ASP.NET application. You like to move selected items from ListBox1 to ListBox2. Usee the below code.
  1. if (ListBox.Items.Count > 0 && ListBox.SelectedItem != null)
  2. {
  3. while (ListBox.Items.Count > 0 && ListBox.SelectedItem != null)
  4. {
  5. ListItem selectedItem = new ListItem();
  6. selectedItem = ListBox.SelectedItem;
  7. selectedItem.Selected = false;
  8. ListBox2.Items.Add(selectedItem);
  9. ListBox1.Items.Remove(selectedItem);
  10. }
  11. }

you can move multiple items by selecting multiple items in list box. To do that you have to set the ListBox "SelectionMode" properties to "Multiple"

No comments:

Post a Comment