从控件删除所有,但一个项目 [英] Removing all but one item from Controls
本文介绍了从控件删除所有,但一个项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我现在有一个 SharePoint 2010中
网络的一部分,其中包括几个标签。我希望以编程方式删除所有,但这些标签之一。
我试过下面的代码,但是有一个 System.InvalidOperationException
因为很明显,而通过它遍历一个人不能修改的集合。但是,我不知道该怎么尝试。
私人无效clearLabels()
{
的foreach(在this.Controls控制续)
如果(续是标签和放大器;&安培;!cont.ID =错误)
this.Controls.Remove(续)
}
解决方案
迭代它倒退。
的for(int i = this.Controls.Count - 1; I> = 0;我 - )
{
如果(this.Controls [i]为标签和放大器;&安培; this.Controls [I] .ID =错误!)
{
this.Controls.Remove(这一点。控制[I]);
}
}
I currently have a Sharepoint 2010
web part which includes several labels. I want to programmatically remove all but one of these labels.
I tried the code below but got a System.InvalidOperationException
because obviously one can't modify a collection while iterating through it. However, I don't know how else to try this.
private void clearLabels()
{
foreach (Control cont in this.Controls)
if (cont is Label && cont.ID != "error")
this.Controls.Remove(cont);
}
解决方案
Iterate over it backwards.
for(int i = this.Controls.Count - 1; i >= 0; i--)
{
if (this.Controls[i] is Label && this.Controls[i].ID != "error")
{
this.Controls.Remove(this.Controls[i]);
}
}
这篇关于从控件删除所有,但一个项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文