2 list box item transfer together

Add Item into ListBox

Write below code in page load event for adding items in listbox1

if (!Page.IsPostBack)
{
ListBox1.Items.Add(“Baroda”);
ListBox1.Items.Add(“Anand”);
ListBox1.Items.Add(“Ahmedabad”);
ListBox1.Items.Add(“Pune”);
}

Now create another list box and one button to transfer multiple selected item from previous listbox

Write downbelow code in Button Click event………..

int sel = 0;

for (int i = 0; i < ListBox1.Items.Count; i++)
{
if (ListBox1.Items[i].Selected)
{
sel = sel + 1;
}
}

if (sel == 1)
{
ListBox2.Items.Add(ListBox1.SelectedItem.Value);
ListBox1.Items.Remove(ListBox1.SelectedItem.Value);

}
else
{
for (int i = 0; i < ListBox1.Items.Count; i++)
{
if (ListBox1.Items[i].Selected)
{
ListBox2.Items.Add(ListBox1.Items[i].Value);

}
}

for (int i = 0; i < ListBox1.Items.Count; i++)
{
if (ListBox1.Items[i].Selected)
{
ListBox1.Items.RemoveAt(i);
i = i – 1;
}
}
}

when selected items transfer from Listbox1, they are deleted also.

For remove all items of listbox

ListBox1.Items.Clear();

Share