How to send selected data from one ListBox to another ListBox?

In this Article i am going to explain you how to transfer selecetd Item from one ListBox to another ListBox. Here I have taken two ListBoxes,I added one Some Items in first ListBox. When the user select a Item from first ListBox it has to
show in another listBox. Add some Items in First ListBox. Add 4 Buttons for select,Remove,Select all,remove all on asp page. Here Select Button is used to Select a user Only one Item. Remove Button is used to remove onely one Item from second listbox. SelectAll Button is used to send all items from first listbox to second ListBox.RemoveAll Button is used to remove all selected Items from Second ListBox.

Code for 'Select Button'



Use the Below code in Select Butteon to send a SelectedItem
from first ListBox to Second ListBox

protected void Select_Click1(object sender, EventArgs e)
{
bool flag = false;
string selecteditem = ListBox1.SelectedItem.Text;
if (ListBox2.Items.Count >= 1)
{
for (int i = 0; i < ListBox2.Items.Count; i++)
{

if (selecteditem == ListBox2.Items[i].Text)
{

Response.Write("Already Item Selected");
flag =true;
}
}
if (flag == false)
{
ListItem li = new ListItem();
li.Text = selecteditem;
li.Value = selecteditem;
ListBox2.Items.Add(li);
}
}
else
{
ListItem li = new ListItem();
li.Text = selecteditem;
li.Value = selecteditem;
ListBox2.Items.Add(li);
}
}


Code for 'remove button' selected Item From Second ListBox use the Below code in Remove Button.




protected void Remove_Click(object sender, EventArgs e)
{
try
{

if (ListBox2.Items.Count >= 1)
{
if (ListBox2.SelectedValue != null)
{
string selecteditem2 = ListBox2.SelectedItem.Text;

ListBox2.Items.Remove(selecteditem2);
}
}
else

{

Response.Write("No ITEMS Found");
}
}
catch ( Exception e1)
{

}

}


code for 'SelectAll button' when user wants to select all Items at a Time and transer to second ListBox.



protected void SelectAll_Click(object sender, EventArgs e)
{
ListBox2.Items.Clear();
for (int i = 0; i < ListBox1.Items.Count; i++)
{
ListItem li = new ListItem();
li.Text = ListBox1.Items[i].Text;
li.Value = ListBox1.Items[i].Value;
ListBox2.Items.Add(li);
}
}


Code forDeSelect (or) Remove all Items from second ListBox write the below code in RemoveAll Button:



protected void RemoveAll_Click(object sender, EventArgs e)
{
ListBox2.Items.Clear();
}
Above Clear() will remove all Items from a ListBox at a Time.


Thanks for Reading my article

Syed Shakeer Hussain


Comments

Author: Phagu Mahato27 Feb 2014 Member Level: Gold   Points : 10

You can Add the following


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Collections;

public partial class _Default : System.Web.UI.Page
{
ArrayList arraylist1 = new ArrayList();
ArrayList arraylist2 = new ArrayList();
protected void Page_Load(object sender, EventArgs e)
{

}

protected void btn1_Click(object sender, EventArgs e)
{
lbltxt.Visible = false;
if (ListBox1.SelectedIndex >= 0)
{
for (int i = 0; i < ListBox1.Items.Count; i++)
{
if (ListBox1.Items[i].Selected)
{
if (!arraylist1.Contains(ListBox1.Items[i]))
{
arraylist1.Add(ListBox1.Items[i]);

}
}
}
for (int i = 0; i < arraylist1.Count; i++)
{
if (!ListBox2.Items.Contains(((ListItem)arraylist1[i])))
{
ListBox2.Items.Add(((ListItem)arraylist1[i]));
}
ListBox1.Items.Remove(((ListItem)arraylist1[i]));
}
ListBox2.SelectedIndex = -1;
}
else
{
lbltxt.Visible = true;
lbltxt.Text = "Please select atleast one in Listbox1 to move";
}
}

protected void btn2_Click(object sender, EventArgs e)
{
lbltxt.Visible = false;
while(ListBox1.Items.Count!=0)
{
for(int i=0;i{
ListBox2.Items.Add(ListBox1.Items[i]);
ListBox1.Items.Remove(ListBox1.Items[i]);
}
}
}

protected void btn3_Click(object sender, EventArgs e)
{
lbltxt.Visible = false;
if (ListBox2.SelectedIndex >= 0)
{
for (int i = 0; i < ListBox2.Items.Count; i++)
{
if (ListBox2.Items[i].Selected)
{
if (!arraylist2.Contains(ListBox2.Items[i]))
{
arraylist2.Add(ListBox2.Items[i]);
}
}
}
for (int i = 0; i < arraylist2.Count; i++)
{
if (!ListBox1.Items.Contains(((ListItem)arraylist2[i])))
{
ListBox1.Items.Add(((ListItem)arraylist2[i]));
}
ListBox2.Items.Remove(((ListItem)arraylist2[i]));
}
ListBox1.SelectedIndex = -1;
}
else
{
lbltxt.Visible = true;
lbltxt.Text = "Please select one in Listbox2 to move";
}
}

protected void btn4_Click(object sender, EventArgs e)
{
lbltxt.Visible = false;
while (ListBox2.Items.Count != 0)
{
for (int i = 0; i < ListBox2.Items.Count; i++)
{
ListBox1.Items.Add(ListBox2.Items[i]);
ListBox2.Items.Remove(ListBox2.Items[i]);
}
}
}
}



  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: