Move items from one listbox to another listbox and vice versa in asp.net - Free Asp.Net,MVC,AngularJs,Jquery,Javascript,Sql Server,WCF,Entity framework snippets and tutorial

Move items from one listbox to another listbox and vice versa in asp.net

Introduction: In this article today I will explain how we can move items from one listbox to another listbox and vice versa in asp.net
Description:
I have added the two listboxes to webform. One listbox contain the country name and second one city name.  In this example I have move single item from country listbox to city listbox and vice versa. Also move all items to between each other.
Design the page as Html Markup shown below:
<fieldset>
    <legend>ListBox Example</legend>  
    <table style="height: 168px; width: 279px">
    <tr><td>  <asp:ListBox ID="lstbcountry" runat="server" SelectionMode="Multiple"
            Height="102px" Width="99px">
        <asp:ListItem>India</asp:ListItem>
        <asp:ListItem>England</asp:ListItem>
        <asp:ListItem>Cuba</asp:ListItem>
        <asp:ListItem>Russia</asp:ListItem>
        <asp:ListItem>Singapore</asp:ListItem>
        </asp:ListBox></td>
    <td colspan="2">
        <asp:Button ID="Button1" runat="server" Text=">" onclick="Button1_Click" />      
        <br />
        <asp:Button ID="Button3" runat="server" Text=">>" onclick="Button3_Click" />
        <br />
        <asp:Button ID="Button2"
            runat="server" Text="<" onclick="Button2_Click" />
        <br />
        <asp:Button ID="Button4" runat="server" Text="<<" onclick="Button4_Click" />
        </td>
    <td><asp:ListBox ID="lstbcity" runat="server" SelectionMode="Multiple"
            Height="96px" Width="97px">
        <asp:ListItem>Delhi</asp:ListItem>
        <asp:ListItem>London</asp:ListItem>
        <asp:ListItem>Tokyo</asp:ListItem>
        <asp:ListItem>Boston</asp:ListItem>
        <asp:ListItem>Moscow</asp:ListItem>
        </asp:ListBox></td></tr>
    </table>
       </fieldset>


C# code:

int i = 0;

protected void Button1_Click(object sender, EventArgs e)
    {
        try
        {
            lstbcity.Items.Add(lstbcountry.SelectedItem);
            i = lstbcountry.SelectedIndex;
            lstbcountry.Items.RemoveAt(i);
        }
        catch (Exception ex)
        {
        }
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        try
        {
            lstbcountry.Items.Add(lstbcity.SelectedItem);
            i = lstbcity.SelectedIndex;
            lstbcity.Items.RemoveAt(i);
        }
        catch (Exception ex)
        {
        }
    }
    protected void Button3_Click(object sender, EventArgs e)
    {
        try
        {
            for (i = 0; i <= lstbcountry.Items.Count - 1; i++)
            {
                lstbcity.Items.Add(lstbcountry.Items[i]);
            }
            lstbcountry.Items.Clear();
        }
        catch (Exception ex)
        {
        }       
    }
    protected void Button4_Click(object sender, EventArgs e)
    {
        try
        {
            for (i = 0; i <= lstbcity.Items.Count - 1; i++)
            {
                lstbcountry.Items.Add(lstbcity.Items[i]);
            }
            lstbcity.Items.Clear();
        }
        catch (Exception ex)
        {
        }
    }

VB code:

Dim i As Integer = 0
    Protected Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button2.Click
        lstbcountry.Items.Add(lstbcity.SelectedItem)
        i = lstbcity.SelectedIndex
        lstbcity.Items.RemoveAt(i)
    End Sub

    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
        lstbcity.Items.Add(lstbcountry.SelectedItem)
        i = lstbcountry.SelectedIndex
        lstbcountry.Items.RemoveAt(i)
    End Sub

    Protected Sub Button3_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button3.Click
        Try
            For i = 0 To lstbcountry.Items.Count - 1
                lstbcity.Items.Add(lstbcountry.Items(i))
            Next
            lstbcountry.Items.Clear()
        Catch ex As Exception
        End Try
    End Sub

    Protected Sub Button4_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button4.Click
        Try
            For i = 0 To lstbcity.Items.Count - 1
                lstbcountry.Items.Add(lstbcity.Items(i))
            Next
            lstbcity.Items.Clear()
        Catch ex As Exception
        End Try
    End Sub

Build and run the project.

Output:


Is this article helpful for you?
If yes post your comment to appreciate my work and fell free to contact me. You can like me on Facebook, Google+, Linkedin and Twitter via hit on Follow us Button and also can get update follow by Email.

What do you think about this article?

If you found this article useful, please share and follow on Facebook, Twitter, Google Plus and other social media websites. To get free updates subscribe to newsletter. Please put your thoughts and feedback in comments section.

Share this

Share on FacebookTweet on TwitterPlus on Google+


EmoticonEmoticon