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]);
}
}
{
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