a.sp.net清除ListBox的列表項(刪除所有項目)
更新時間:2012年01月09日 21:43:34 作者:
在網(wǎng)上搜索相關(guān)資料,相當多用戶有相同要求,一次移除ListBox的列表所有項
如何清除ListBox的列表項(刪除所有項目), 今天開發(fā)程序時,有嘗試使用此功能。一開始并不是很順利。循環(huán)所有item去做remove時,需要執(zhí)行兩次才可以完成清除。debug進行步進跟蹤,發(fā)現(xiàn)在Listbox.Items.Count 每移除一個,Count隨之減少,而Capacity并沒有作相應(yīng)變化。
在網(wǎng)上搜索相關(guān)資料,相當多用戶有相同要求,一次移除ListBox的列表所有項。方法均是用:
for (int i = 0; i < Listbox1.Items.Count; i++)
{
Listbox1.Items.RemoveAt(i);
}
或者:
foreach (ListItem li in ListBox1.Items)
{
ListBox1.Items.Remove(li);
}
而后者會出現(xiàn)異常: 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);
}
可以從下面看到操作效果:
在網(wǎng)上搜索相關(guān)資料,相當多用戶有相同要求,一次移除ListBox的列表所有項。方法均是用:
復(fù)制代碼 代碼如下:
for (int i = 0; i < Listbox1.Items.Count; i++)
{
Listbox1.Items.RemoveAt(i);
}
或者:
復(fù)制代碼 代碼如下:
foreach (ListItem li in ListBox1.Items)
{
ListBox1.Items.Remove(li);
}
而后者會出現(xiàn)異常: Collection was modified; enumeration operation may not execute.
不管怎樣,下面是Insus.NET的解決方法,寫一個迭代器:
復(fù)制代碼 代碼如下:
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);
}
}
在清除銨鈕事件中寫:
復(fù)制代碼 代碼如下:
protected void ButtonClear_Click(object sender, EventArgs e)
{
IterationRemoveItem(this.ListBox1);
}
可以從下面看到操作效果:
相關(guān)文章
ASP.NET?MVC實現(xiàn)登錄后跳轉(zhuǎn)到原界面
這篇文章介紹了ASP.NET?MVC實現(xiàn)登錄后跳轉(zhuǎn)到原界面的方法,文中通過示例代碼介紹的非常詳細。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-09-09ynamic LINQ創(chuàng)建高級查詢服務(wù)
這篇文章主要介紹了ynamic LINQ創(chuàng)建高級查詢服務(wù),如何使用Dynamic LINQ輕松實現(xiàn)更強大的高級查詢服務(wù),下面文章內(nèi)容具有一的的參考價值,需要的小伙伴可以參考一下2022-03-03