Click here to Skip to main content
15,884,388 members
Articles / Web Development / ASP.NET
Article

How to Move List Box Items to another List Box in C#.

Rate me:
Please Sign up or sign in to vote.
2.44/5 (20 votes)
22 Jul 2007CPOL 131.7K   21   13
The article demonstrates how to Move List Box Items to another List Box in ASP.NET and C# .
Screenshot - ListBox.jpg

Introduction

An article to demonstrate How to Move List Box Items to another List Box in ASP.NET and C#.

About the solution

In many web applications it is required use list boxes. Sometimes it is necessary to select some items from one list to another. In this case we use two listboxes. This article demonstrates how to perform basic operations in such case. This operations include Add/Remove/Add All / Remove All functionalities.

Background

List Box control is a standard control in Asp.net. It is similar to a dropdownlist.

Code Snippets

<p style="TEXT-ALIGN: justify">Code to add selected item to the list 


</p>

<p style="TEXT-ALIGN: justify">Protected void btnAdd_Click(object sender, EventArgs e)
 {
 if (lstEmployees.SelectedIndex > -1)
   {
     string _value = lstEmployees.SelectedItem.Value; //Gets the value of  items in list.
     string _text = lstEmployees.SelectedItem.Text;  // Gets the Text of items in the list.  
     ListItem item = new ListItem (); //create a list item
     item.Text = _text;               //Assign the values to list item   
     item.Value = _value;
     lstSelectedEmployees.Items.Add(item); //Add the list item to the selected list of employees   
     lstEmployees.Items.Remove(item); //Remove the details from employee list   
   }

<p style="TEXT-ALIGN: justify">}

Code to Remove  selected item from the list 
 
protected void btnRemove_Click(object sender, EventArgs e)
    {
        if (lstSelectedEmployees.SelectedIndex > -1)
        {
            string _value = lstSelectedEmployees.SelectedItem.Value; //Gets the value of items in list.
            string _text = lstSelectedEmployees.SelectedItem.Text;  // Gets the Text of items in the list.  
            ListItem item = new ListItem(); //create a list item
            item.Text = _text;               //Assign the values to list item   
            item.Value = _value;
            lstSelectedEmployees.Items.Remove(item); //Remove from the selected list
            lstEmployees.Items.Add(item); //Add in the Employee list 

        }

    }

Code to Remove All   items from the list 

   protected void btnReset_Click(object sender, EventArgs e)
    {
        int _count=lstSelectedEmployees.Items.Count;
        if (_count != 0)
        {
         for (int i = 0; i < _count; i++)
            {
               ListItem item = new ListItem();
               item.Text = lstSelectedEmployees.Items[i].Text;
                item.Value = lstSelectedEmployees.Items[i].Value;
                lstEmployees.Items.Add(item);   
            }
        }

       lstSelectedEmployees.Items.Clear();//clear the items  
   }

 
 Code to Add All   items to the list 


  protected void btnAddAll_Click(object sender, EventArgs e)
    {
        int _count = lstEmployees.Items.Count;
        if (_count != 0)
         {
            for (int i = 0; i < _count; i++)
             {
               ListItem item = new ListItem();
               item.Text = lstEmployees.Items[i].Text;
               item.Value = lstEmployees.Items[i].Value;
               //Add the item to selected employee list
               lstSelectedEmployees.Items.Add(item);  
          }

        }

      //clear employee list
        lstEmployees.Items.Clear();  

  }


</p>

About Me

I am a Software Engineer handling web application projects using Visual studio 2005 .I work in bangalore.

History

Created on 23-07-2007 by George Zacharia

License

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


Written By
Web Developer
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionDelete these examples with no code. Pin
Member 118062153-Sep-15 9:14
Member 118062153-Sep-15 9:14 
Generalsend me full code Pin
Member 1107685011-Sep-14 19:53
Member 1107685011-Sep-14 19:53 
Questionplease give full code Pin
Shaikh Sameer7-Mar-14 5:45
Shaikh Sameer7-Mar-14 5:45 
QuestionMultiple select in single "Add Click" Pin
Sajan S Jabbar20-Aug-12 22:33
Sajan S Jabbar20-Aug-12 22:33 
GeneralMy vote of 5 Pin
Franco Cipriano20-Jul-12 1:50
Franco Cipriano20-Jul-12 1:50 
QuestionPage always reloads Pin
Franco Cipriano20-Jul-12 1:47
Franco Cipriano20-Jul-12 1:47 
AnswerRe: Page always reloads Pin
Sajan S Jabbar20-Aug-12 22:34
Sajan S Jabbar20-Aug-12 22:34 
put your list boxes and the add button inside asp:updatepanels Laugh | :laugh:
GeneralMy vote of 3 Pin
Joaquin Garcia27-Jun-12 5:30
Joaquin Garcia27-Jun-12 5:30 
QuestionExcellent.... Pin
thothoy28-Jul-11 21:43
thothoy28-Jul-11 21:43 
GeneralExcellent work Pin
Tanmay Ghosh1-Jun-11 3:43
Tanmay Ghosh1-Jun-11 3:43 
GeneralGreat Pin
khrizz_tell8-May-09 7:35
khrizz_tell8-May-09 7:35 
QuestionNikzad.Behrouz has question Pin
Sean Ewington17-Aug-07 3:40
staffSean Ewington17-Aug-07 3:40 
QuestionListItem??? Pin
kboyette16-Aug-07 6:02
kboyette16-Aug-07 6:02 

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

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