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