Working with Two ListBoxes

Syedshakeer
Posted by in ASP.NET category on for Intermediate level | Views : 7549 red flag

In this Article I am going to explain how to send a Selected Item from one ListBox to another ListBox

In this Article i am going to explain you how to transfer a Item from one ListBox to another ListBox.Here i taken two ListBoxes,I added one Some Items in first ListBox.When the user select a Item from first ListBox it has to show in another listBox.

Below Image shows how to Add Items in a ListBox


After Adding Click Ok. Add Some Buttons for select, Remove, Select all, Remove all. Then your Listbox will be as shown as in below image


Here Select Button is used to Select a user Only one Item. Remove Button is used to remove onely one Item from second listbox. SelectAll Button is used to send  all items from first listbox to second ListBox. RemoveAll Button is used to remove all selected Items from Second ListBox.

Use the Below code in Select Butteon to send a SelectedItem from first ListBox to Second ListBox

protected void Select_Click1(object sender, EventArgs e)

   {

 

string selecteditem = ListBox1.SelectedItem.Text;

                ListItem li = new ListItem();

                li.Text = selecteditem;

                li.Value = selecteditem;

                ListBox2.Items.Add(li);

}

If the user dont want to allow repeted Items in second ListBox then use below code

protected void Select_Click1(object sender, EventArgs e)

    {

      bool  flag = false;

        string selecteditem = ListBox1.SelectedItem.Text;

        if (ListBox2.Items.Count >= 1)

        {

            for (int i = 0; i < ListBox2.Items.Count; i++)

            {

                if (selecteditem == ListBox2.Items[i].Text)

                {

                    Response.Write("Already Item Selected");

                    flag =true;

                }

            }

 

            if (flag == false)

            {

                ListItem li = new ListItem();

                li.Text = selecteditem;

                li.Value = selecteditem;

                ListBox2.Items.Add(li);

             }  

           

        }

        else

        {

 

            ListItem li = new ListItem();

            li.Text = selecteditem;

            li.Value = selecteditem;

            ListBox2.Items.Add(li);

        }

       

      

       

    }

The output as Shows Below after selecting one item


When user wants to remove selected Item From Second ListBox use the Below code in Remove Button.It removes only one selected Item

    protected void Remove_Click(object sender, EventArgs e)

    {

        try

        {

        if (ListBox2.Items.Count >= 1)

        {

            if (ListBox2.SelectedValue != null)

            {

                string selecteditem2 = ListBox2.SelectedItem.Text;

                ListBox2.Items.Remove(selecteditem2);

 

            }                  

        }

        else

        {

            Response.Write("No ITEMS Found");

        }

        }

 

        catch ( Exception e1)

        {

 

        }

             

    }

Write Below code in SelectAll button when user wants to select all Items at a Time and transer to second ListBox.

    protected void SelectAll_Click(object sender, EventArgs e)

    {

        ListBox2.Items.Clear();

        for (int i = 0; i < ListBox1.Items.Count; i++)

        {

 

            ListItem li = new ListItem();

            li.Text = ListBox1.Items[i].Text;

            li.Value = ListBox1.Items[i].Value;

            ListBox2.Items.Add(li);

 

 

        }

    }

The outPut of SelectAll will displays a Below:

When the user wants to DeSelect (or) Remove all Items from second ListBox write the below code in RemoveAll Button:

protected void RemoveAll_Click(object sender, EventArgs e)

    {

        ListBox2.Items.Clear();

    }

Above Clear() method of Items will remove all Items from a ListBox at a Time.


Page copy protected against web site content infringement by Copyscape

About the Author

Syedshakeer
Full Name: Syed Shakeer Hussiain P
Member Level:
Member Status: Member
Member Since: 2/5/2009 3:12:18 AM
Country: India
Syed Shakeer Hussain
http://www.dotnetfunda.com
Shakeer Hussain has completed his Master of Computer Applications degree from Deccan College of engg and technology of Osmania University.He is a MVM of www.dotnetspider.com.He has good experience in the areas of ASP.NET, C#.NET, VB.NET, SQL SERVER 2000/2005 and Windows Mobile. He has worked in Windows Mobile,Web Applicatin and ERP projects.

Login to vote for this post.

Comments or Responses

Login to post response

Comment using Facebook(Author doesn't get notification)