You create a Microsoft ASP.NET application by using the Microsoft .NET Framework version 3.5.
You write the following code fragment.
<asp:ListBox SelectionMode=”Multiple” ID=”ListBox1″ runat=”server”></asp:ListBox>
<asp:ListBox ID=”ListBox2″ runat=”server”></asp:ListBox>
<asp:Button ID=”Button1″ runat=”server” Text=”Button” onclick=”Button1_Click” />
You need to ensure that when you click the Button1 control, a selected list of items move from the ListBox1 control to the ListBox2 control.
Which code segment should you use?
A.
foreach (ListItem li in ListBox1.Items) {
if (li.Selected) {
ListBox2.Items.Add(li); ListBox1.Items.Remove(li);
}
}
B.
foreach (ListItem li in ListBox1.Items) {
if (li.Selected) {
li.Selected = false; ListBox2.Items.Add(li);
ListBox1.Items.Remove(li);
}
}
C.
foreach (ListItem li in ListBox1.Items) {
if (li.Selected) {
li.Selected = false;
ListBox2.Items.Add(li);
}
}
foreach (ListItem li in ListBox2.Items) {
if (ListBox1.Items.Contains(li)) ListBox1.Items.Remove(li);
}
D.
foreach (ListItem li in ListBox1.Items) {
if (li.Selected) {
li.Selected = false;
ListBox2.Items.Add(li);
}
}
foreach (ListItem li in ListBox1.Items) {
if (ListBox2.Items.Contains(li)) ListBox1.Items.Remove(li);
}