手机
当前位置:查字典教程网 >编程开发 >asp.net教程 >a.sp.net清除ListBox的列表项(删除所有项目)
a.sp.net清除ListBox的列表项(删除所有项目)
摘要:如何清除ListBox的列表项(删除所有项目),今天开发程序时,有尝试使用此功能。一开始并不是很顺利。循环所有item去做remove时,需...

如何清除ListBox的列表项(删除所有项目), 今天开发程序时,有尝试使用此功能。一开始并不是很顺利。循环所有item去做remove时,需要执行两次才可以完成清除。debug进行步进跟踪,发现在Listbox.Items.Count 每移除一个,Count随之减少,而Capacity并没有作相应变化。

在网上搜索相关资料,相当多用户有相同要求,一次移除ListBox的列表所有项。方法均是用:

复制代码 代码如下:

for (int i = 0; i < Listbox1.Items.Count; i++)

{

Listbox1.Items.RemoveAt(i);

}

或者:

复制代码 代码如下:

foreach (ListItem li in ListBox1.Items)

{

ListBox1.Items.Remove(li);

}

而后者会出现异常: Collection was modified; enumeration operation may not execute.

不管怎样,下面是Insus.NET的解决方法,写一个迭代器:

复制代码 代码如下:

private void IterationRemoveItem(ListBox listbox)

{

for (int i = 0; i < listbox.Items.Count; i++)

{

this.ListBoxCondition.Items.RemoveAt(i);

}

for (int j = 0; j < listbox.Items.Count; j++)

{

IterationRemoveItem(listbox);

}

}

在清除铵钮事件中写:

复制代码 代码如下:

protected void ButtonClear_Click(object sender, EventArgs e)

{

IterationRemoveItem(this.ListBox1);

}

可以从下面看到操作效果:

a.sp.net清除ListBox的列表项(删除所有项目)1

【a.sp.net清除ListBox的列表项(删除所有项目)】相关文章:

asp.net 时间类 一周的周一和周末的日期

asp.net repeater实现批量删除第1/2页

aspx与ascx,ashx的用法总结

asp.net Timer的使用方法

asp.net repeater实现批量删除时注册多选框id到客户端

asp.net替换和恢复html特殊字符

asp.net(c#)利用构造器链的代码

asp.net导出excel的简单方法

asp.net操作xml增删改示例分享

asp.net下Linq To Sql注意事项小结

精品推荐
分类导航