Server Side Transfer ListItems from one ListBox to another ListBox Using Asp.Net C# VB.Net

In many cases we need to move or transfer one ListBox items into another ListBox. In my previous examples I have showed the example using javascript & JQuery. But if you need to move items from server side then this post is for you. I will also describe how to move all ListItems from left side ListBox to Right side ListBox

Related Articles:
1. JQuery to Move Data from one ListBox to another ListBox considering Postback using Asp.Net C# VB.Net
2. Javascript to Transfer Data from one ListBox to another ListBox considering Postback using Asp.Net C# VB.Net

Objective/Outcome:
Transfer Items from one ListBox to another ListBox Using Asp.Net Csharp VB.Net

To do the Example prepare the HTML like below:

<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
    <table border="0" cellpadding="0" cellspacing="0">
    <tr>
        <td>
            <asp:ListBox ID="lstLeft" runat="server" SelectionMode="Multiple" Width="100" Height="200">
                <asp:ListItem Text="January" Value="1" />
                <asp:ListItem Text="Februray" Value="2" />
                <asp:ListItem Text="March" Value="3" />
                <asp:ListItem Text="April" Value="4" />
                <asp:ListItem Text="May" Value="5" />
                <asp:ListItem Text="June" Value="6" />
                <asp:ListItem Text="July" Value="7" />
                <asp:ListItem Text="August" Value="8" />
                <asp:ListItem Text="September" Value="9" />
                <asp:ListItem Text="October" Value="10" />
                <asp:ListItem Text="November" Value="11" />
                <asp:ListItem Text="December" Value="12" />
            </asp:ListBox>
        </td>
        <td>
        <asp:Button ID="BtnRight" runat="server" Text=">" onclick="BtnRight_Click" /><br /><br />
        <asp:Button ID="BtnLeft" runat="server" Text="<" onclick="BtnLeft_Click"  /><br /><br />
        <asp:Button ID="BtnRightAll" runat="server" Text=">>" 
                onclick="BtnRightAll_Click"  /><br /><br />
        <asp:Button ID="BtnLeftAll" runat="server" Text="<<" onclick="BtnLeftAll_Click" />        
        </td>
        <td>
            <asp:ListBox ID="lstRight" runat="server" SelectionMode="Multiple" Width="100" Height="200"></asp:ListBox>
        </td>
    </tr>
</table>
<br />
<asp:Button ID="btnSubmit" Text="Submit" runat="server" onclick="btnSubmit_Click"/>
<br />
<hr />

<table>
    <tr>
        <td  valign="top"><asp:Label ID="Label1" runat="server" Text="Left ListBox Items:<br/>"></asp:Label></td>
        <td valign="top"><asp:Label ID="Label2" runat="server" Text="Right ListBox Items:<br/>"></asp:Label></td>
    </tr>
</table>
</asp:Content>

Now individually write Server Side Method for Move Right, Left , All etc.:
C# Code:

    protected void BtnRight_Click(object sender, EventArgs e)
    {
        MoveItems(lstLeft,lstRight,false);
    }
    
    protected void BtnLeft_Click(object sender, EventArgs e)
    {
        MoveItems(lstRight, lstLeft,false);
    }

    protected void BtnRightAll_Click(object sender, EventArgs e)
    {
        MoveItems(lstLeft, lstRight, true);
    }
   
    protected void BtnLeftAll_Click(object sender, EventArgs e)
    {
        MoveItems(lstRight, lstLeft, true);
    }

    private void MoveItems(ListBox leftListBox, ListBox RightListBox,bool bAll)
    {
        ListItemCollection oCollection = new ListItemCollection();
        foreach (ListItem oItem in leftListBox.Items)
            if (oItem.Selected || bAll)
            {
                RightListBox.Items.Add(oItem);
                oCollection.Add(oItem);
            }
        foreach (ListItem oItem in oCollection)
            leftListBox.Items.Remove(oItem);
    }

VB.Net Code:

Protected Sub BtnRight_Click(sender As Object, e As EventArgs)
        MoveItems(lstLeft, lstRight, False)
    End Sub

    Protected Sub BtnLeft_Click(sender As Object, e As EventArgs)
        MoveItems(lstRight, lstLeft, False)
    End Sub

    Protected Sub BtnRightAll_Click(sender As Object, e As EventArgs)
        MoveItems(lstLeft, lstRight, True)
    End Sub

    Protected Sub BtnLeftAll_Click(sender As Object, e As EventArgs)
        MoveItems(lstRight, lstLeft, True)
    End Sub

    Private Sub MoveItems(leftListBox As ListBox, RightListBox As ListBox, bAll As Boolean)
        Dim oCollection As New ListItemCollection()
        For Each oItem As ListItem In leftListBox.Items
            If oItem.Selected OrElse bAll Then
                RightListBox.Items.Add(oItem)
                oCollection.Add(oItem)
            End If
        Next
        For Each oItem As ListItem In oCollection
            leftListBox.Items.Remove(oItem)
        Next
    End Sub

Now read all Moved Items of the ListBoxes:
C# Code:

    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        Label1.Text = "Left ListBox Items:<br/>";
            foreach (ListItem oItem in lstLeft.Items)
                Label1.Text = Label1.Text + oItem.Text + "<br/>";

        Label2.Text = "Right ListBox Items:<br/>";
        foreach (ListItem oItem in lstRight.Items)
            Label2.Text = Label2.Text + oItem.Text + "<br/>";
    }

VB.Net Code:

    Protected Sub btnSubmit_Click(sender As Object, e As EventArgs)
        Label1.Text = "Left ListBox Items:<br/>"
        For Each oItem As ListItem In lstLeft.Items
            Label1.Text = Label1.Text + oItem.Text + "<br/>"
        Next

        Label2.Text = "Right ListBox Items:<br/>"
        For Each oItem As ListItem In lstRight.Items
            Label2.Text = Label2.Text + oItem.Text + "<br/>"
        Next
    End Sub

Now run the project. Hope you can move items from Left to Right successfully.

Download Code Example C#        Download Code Example VB.Net

Posted in .Net, Asp.net, C#, ListBox, VB.Net

Leave a Reply

Your email address will not be published. Required fields are marked *

AlphaOmega Captcha Classica  –  Enter Security Code
     
 

*