Click here to Skip to main content
Click here to Skip to main content

ASP.NET Move Items from One Listbox to Another Listbox

, 1 Apr 2013
Rate this:
Please Sign up or sign in to vote.
How to move items from one listbox to another in ASP.NET.

Introduction

This tip will help you to move items from one listbox to another in ASP.NET.

In our ASP.NET application, we use arraylists. To get arraylist in code behind, we need to have System.Collection namespace in our application:

using System.Collections;

System.Collections namespace contains interfaces and classes that define various collections of objects, such as lists, queues, bit arrays, hash tables and dictionaries.

In code-behind, write the following code:

ArrayList arraylist1 = new ArrayList();
ArrayList arraylist2 = new ArrayList();
protected void Page_Load(object sender, EventArgs e)
{

}
/// <summary>
/// btn1_Click event is used to move single or multiple items from Listbox1 to Listbox2
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
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";
    }
}

/// <summary>
/// btn2_Click event is used to move all items from Listbox1 to Listbox2
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void btn2_Click(object sender, EventArgs e)
{
    lbltxt.Visible = false;
    while(ListBox1.Items.Count!=0)
    {
        for(int i=0;i<ListBox1.Items.Count;i++)
        {
            ListBox2.Items.Add(ListBox1.Items[i]);
            ListBox1.Items.Remove(ListBox1.Items[i]);
        }
    }
}

/// <summary>
/// btn3_Click event is used to move single or multiple items from Listbox2 to Listbox1
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
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 atleast one in Listbox2 to move";
    }
}

/// <summary>
/// btn4_Click event is used to move all items form Listbox2 to Listbox1
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
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]);
        }
    }
}

Demo

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

haneef from secondrabad

United States United States
No Biography provided

Comments and Discussions

 
QuestionSuresh article Pinprofessionalmuhammadraheel9-Apr-14 22:16 
GeneralMy vote of 3 PinmemberSarojKumar8716-Mar-14 21:26 
GeneralCopied Article from Aspdotnet-Suresh.com site Pinmembersuresh44614-Jun-13 1:22 
GeneralRe: Copied Article from Aspdotnet-Suresh.com site [modified] Pinprofessionalmuhammadraheel9-Apr-14 22:22 
GeneralMy vote of 1 Pinmembersuresh44614-Jun-13 1:20 
Questionsort the list Pinmemberpree_kh3-Apr-13 22:44 
hi i am doing exactly like you,but i want that if the removed items from one list are added again in the previous list,then the list should occur alphabetically.
currently,the removed items are being added at the last.

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web02 | 2.8.140709.1 | Last Updated 1 Apr 2013
Article Copyright 2013 by haneef from secondrabad
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid