Removing Controls from asp.net page at runtime
<p>I have an asp.net dashboard site that allows a user to load HTML templates from a dropdownlist. There are multiple types of DevExpress components on the page, including the ASPxDockPanel. If a user changes templates I get an error that the dockpanel already exists, I would like to include a recursive function like the one below that checks to see if any ASPxDockPanels are present on the page, and if they are present remove them. This works for only the first dock panel then bombs out. I think this is because an enumerable set of controls cannot be modified while looping through it. How can I loop though the controls and remove the dock panels at runtime?</p>

<pre><code>protected void LoadTableTemplate(string selectedTemplate, int currentMode)
{
   FindAllDockPanels(this);  
}


public void FindAllDockPanels(Control ctrl)
{
    if (ctrl != null)
    {
        foreach (Control control in ctrl.Controls)
        {
            if (control is ASPxDockPanel)
            {
               ctrl.Controls.Remove(control);
               control.Dispose();
            }
            FindAllDockPanels(control);
        }
     }
}
</code></pre>
