why can not i use foreach loop to drop items from listbox:
protected void btnRemove_Click(object sender, EventArgs e)
{
ListBox listbox = Controltest2.FindControl("ListBox1") as ListBox;
if (Controltest2.Items.Count > 0)
{
foreach (ListItem li in listbox.Items)
{
if (li.Selected)
{
Controltest2.Remove(li.Value);
}
}
}
}
This codes give me error to drop item from listbox. On the other hand;
ListBox listbox = Controltest2.FindControl("ListBox1") as ListBox;
if (Controltest2.Items.Count > 0)
{
int count = Controltest2.Items.Count;
for (int i = count - 1; i > -1; i--)
{
if (listbox.Items[i].Selected)
{
Controltest2.Remove(listbox.Items[i].Value);
}
}
}
Why cannot i use "Foreach loop" instead of "for loop"...