I have a view where some labels and textboxes are generated once the user clicks on a button. When generating I pass the name as "dynamic_something" and it gets added
TextBox textBox = new TextBox();
textBox.Name = "dynamic_something";
this.Controls.Add(textBox);
When the user clicks on another button I want to remove all the generated fields. I loop through all the controls and find the controls with dynamic at the start and remove but all fields doesn't get removed.
foreach (Control currentControl in this.Controls)
{
if ((currentControl).Name.StartsWith("dynamic"))
{
Controls.Remove(currentControl);
}
}
How can I fix this?