This question already has an answer here:
- How do I loop through items in a list box and then remove those item? 8 answers
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);
}