日期:2014-05-19  浏览次数:20716 次

ListBox移除
这样写对马?为什么没有移除  
                foreach   (ListItem   item   in   ListBox1.Items)
                {
                        if   (item.Selected   ==   true)
                        {
                              string     ItemValue   =   item.Value;
                              ListBox1.Items.Remove(item.Value);
                        }
                }
                ListBox1.DataBind();


------解决方案--------------------
在这里使用 if 不要用 foreach
------解决方案--------------------
for (int i = listbox1.Items.Count - 1; i > = 0; i++)
{
if ....
listbox1.Items.RemoveAt(i)
....
}

------解决方案--------------------
sorry,use for loop ,not if judge
------解决方案--------------------
LZ这样有两问题:
1、使用Foreach时不能修改枚举内容,改成for就可以了。
2、DataBind会再次把数据源中的数据填充到ListItem里,你必然看不到Item移除。