Add and remove ListBox instances

Source: Internet
Author: User

Front-end code:
Copy codeThe Code is as follows:
<Div>
<Asp: ListBox ID = "ListBox1" runat = "server" Height = "123px" Width = "113px" SelectionMode = "Multiple">
<Asp: ListItem> tom </asp: ListItem>
<Asp: ListItem> jion </asp: ListItem>
<Asp: ListItem> j </asp: ListItem>
<Asp: ListItem> l </asp: ListItem>
<Asp: ListItem> k </asp: ListItem>
</Asp: ListBox>
<Asp: Button ID = "btnAdd" runat = "server" OnClick = "btnAdd_Click" Text = "add"/>

<Asp: Button ID = "btnRemove" runat = "server" Text = "Remove" OnClick = "btnRemove_Click"/>
<Asp: ListBox ID = "ListBox2" runat = "server" Height = "123px" SelectionMode = "Multiple" Width = "113px"> </asp: ListBox>
</Div>

Background code:
Copy codeThe Code is as follows:
Protected void btnAdd_Click (object sender, EventArgs e)
{
# Region listbox An error occurred while adding a record
// When multiple records are selected, one record is not added because after a record is removed, the index of the original second record is 0.
// For (int I = 0; I <ListBox1.Items. Count; I ++)
//{
// If (ListBox1.Items [I]. Selected = true)
//{
// ListBox2.Items. Add (ListBox1.SelectedValue );
// ListBox1.Items. Remove (ListBox1.SelectedValue );
//}
//}
# Endregion
# Region listbox simple syntax for adding indexes using index Numbers
// While (0 <= ListBox1.SelectedIndex)
//{
// ListBox2.Items. Add (ListBox1.SelectedItem );
// ListBox1.Items. Remove (ListBox1.SelectedItem );
//}
# Endregion
# Another method for successfully adding region listbox
List <ListItem> list = new List <ListItem> ();
For (int I = ListBox1.Items. Count-1; I> = 0; I --)
{
If (ListBox1.Items [I]. Selected = true)
{
List. Add (ListBox1.Items [I]);
ListBox1.Items. Remove (ListBox1.Items [I]);
}
}
For (int I = 0; I <= list. Count-1; I ++)
{
ListBox2.Items. Add (list [I]);
}
# Endregion
}
Protected void btnRemove_Click (object sender, EventArgs e)
{
While (0 <= ListBox2.SelectedIndex)
{
ListBox1.Items. Add (ListBox2.SelectedItem );
ListBox2.Items. Remove (ListBox2.SelectedItem );
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.