In this article I will explain with an example, how to move ListBox Items from one ListBox to another in ASP.Net using C# and VB.Net.
When the Move Button is clicked, the selected items from the source ListBox will be added to the destination ListBox and then the selected items will be removed from the source ListBox in ASP.Net using C# and VB.Net.
HTML Markup
The HTML Markup consists of two ListBox and two Buttons.
<table border="0" cellpadding="0" cellspacing="5">
<tr>
<td>
<asp:ListBox ID="lstLeft" runat="server" SelectionMode="Multiple" Width="80">
<asp:ListItem Text="Apple" Value="Apple" />
<asp:ListItem Text="Mango" Value="Mango" />
<asp:ListItem Text="Grapes" Value="Grapes" />
<asp:ListItem Text="Pineapple" Value="Pineapple" />
<asp:ListItem Text="Guava" Value="Guava" />
<asp:ListItem Text="Cherry" Value="Cherry" />
<asp:ListItem Text="Banana" Value="Banana" />
<asp:ListItem Text="Papaya" Value="Papaya" />
</asp:ListBox>
</td>
<td>
<asp:Button ID="btnLeft" Text="<<" runat="server" OnClick="LeftClick" />
<asp:Button ID="btnRight" Text=">>" runat="server" OnClick="RightClick" />
</td>
<td>
<asp:ListBox ID="lstRight" runat="server" SelectionMode="Multiple" Width="80"></asp:ListBox>
</td>
</tr>
</table>
Moving ListBox Items from one ListBox to another in ASP.Net
When the Left Button is clicked, a loop is executed over the Right ListBox and the selected Items are added to the Left ListBox.
Finally, the selected Items are removed from the Right ListBox.
In similar manner, when the Right Button is clicked, selected items are added to the Right ListBox and ultimately removed from the Left ListBox.
C#
protected void LeftClick(object sender, EventArgs e)
{
//List will hold items to be removed.
List<ListItem> removedItems = new List<ListItem>();
//Loop and transfer the Items to Destination ListBox.
foreach (ListItem item in lstRight.Items)
{
if (item.Selected)
{
item.Selected = false;
lstLeft.Items.Add(item);
removedItems.Add(item);
}
}
//Loop and remove the Items from the Source ListBox.
foreach (ListItem item in removedItems)
{
lstRight.Items.Remove(item);
}
}
protected void RightClick(object sender, EventArgs e)
{
//List will hold items to be removed.
List<ListItem> removedItems = new List<ListItem>();
//Loop and transfer the Items to Destination ListBox.
foreach (ListItem item in lstLeft.Items)
{
if (item.Selected)
{
item.Selected = false;
lstRight.Items.Add(item);
removedItems.Add(item);
}
}
//Loop and remove the Items from the Source ListBox.
foreach (ListItem item in removedItems)
{
lstLeft.Items.Remove(item);
}
}
VB.Net
Protected Sub LeftClick(ByVal sender As Object, ByVal e As EventArgs)
'List will hold items to be removed.
Dim removedItems As List(Of ListItem) = New List(Of ListItem)
'Loop and transfer the Items to Destination ListBox.
For Each item As ListItem In lstRight.Items
If item.Selected Then
item.Selected = False
lstLeft.Items.Add(item)
removedItems.Add(item)
End If
Next
'Loop and remove the Items from the Source ListBox.
For Each item As ListItem In removedItems
lstRight.Items.Remove(item)
Next
End Sub
Protected Sub RightClick(ByVal sender As Object, ByVal e As EventArgs)
'List will hold items to be removed.
Dim removedItems As List(Of ListItem) = New List(Of ListItem)
'Loop and transfer the Items to Destination ListBox.
For Each item As ListItem In lstLeft.Items
If item.Selected Then
item.Selected = False
lstRight.Items.Add(item)
removedItems.Add(item)
End If
Next
'Loop and remove the Items from the Source ListBox.
For Each item As ListItem In removedItems
lstLeft.Items.Remove(item)
Next
End Sub
Screenshot
Demo
Downloads