Wednesday, May 16, 2012

Listbox to Listbox Data transfer in Asp.net C#




List Box control is a standard control in Asp.net. It is similar to a dropdownlist.
An article to demonstrate How to Move List Box Items to another List Box in ASP.NET and C#. 



CODE

Listbox1 items adding  to listbox2

protected void btnadditemlist_Click(object sender, EventArgs e)
     {
         foreach (ListItem item in listbox1.Items)
         {
             if (item.Selected)
             {
                 if (listbox2.Items.Contains(item))
                 {
                     // Display message
                     return;
                 }




                 string _value = item.Value; //Gets the value of items in list.
                 string _text = item.Text;  // Gets the Text of items in the list. 
               

                 ListItem items = new ListItem(); //create a list item
                 items.Text = _text;               //Assign the values to list item  
                 items.Value = _value;

               
                 listbox2.Items.Add(items);
             }
         }
     }


Removing Data from Listbox 2

protected void Remove_Click(object sender, EventArgs e)
    {
        foreach (ListItem item in ListBox2.Items)
        {
            if (item.Selected)
            {


                //ListBox2.Items.Remove(item);
                selectedItems.Add(item);

            }
        }
        for (var i = 0; i < selectedItems.Count; i++)
        {
                       ListBox2.Items.Remove((ListItem)selectedItems[i]);

        }
    }

No comments:

Post a Comment

Save a PDF generated from html2pdf in C#, MVC , html2pdf , Ajax code

 To save on server or disk a PDF generated from HTML in C# code example, you can use the      html2pdf js script     html2pdf   converts any...