This tutorial will allow you to move data from one list box to another, as well as remove them again – either one by one, or altogether. Also, you can choose to allow duplicates or not, as well as output the chosen selection. C# version.

We need a Web User Control, which will have two list boxes, and four buttons: Add All, Add One, Delete One (from Selection List), and also Delete One (from Source List).

Now we add code to the controls:

ASPX code for these buttons should look something like this:

[csharp]
protected void AllowDuplicates_CheckedChanged(object sender, EventArgs e)
{
ListPicker1.AllowDuplicates = AllowDuplicates.Checked;
}
protected void AddItem_Click(object sender, EventArgs e)
{
ListPicker1.AddSourceItem(Server.HtmlEncode(NewItem.Text));
}
protected void LoadFiles_Click(object sender, EventArgs e)
{
String path = Server.MapPath(Request.ApplicationPath);
String[] files = System.IO.Directory.GetFiles(path);
foreach(String filename in files)
{
ListPicker1.AddSourceItem(filename);
}
}
protected void ShowSelection_Click(object sender, EventArgs e)
{
String selectedItemsString = “”;
foreach(ListItem lItem in ListPicker1.SelectedItems)
{
selectedItemsString += “
” + lItem.Text;
}
Selection.Text = selectedItemsString;
}
protected void ClearSelection_Click(object sender, EventArgs e)
{
ListPicker1.ClearAll();
Selection.Text = “”;
}
[/crayon]

Download Source Files