title: Removing dynamic controls : Clear is working but not remove
body: <p>I faced a recent problem, where I was generating the dynamic control on the selection of drop down. When the selection changes, I have to generate another set of dynamic controls, removing the existing controls. </p>
  
  <p>So I was doing following which is not working: </p>
  
  <pre><code>
      private void ClearDynamicControls()
      {
          if (adapter != null)
          {
              //This has all the controls saved in some dictionary, key as control ID
              var controls = adapter.GetAllControls().Keys;
              Control mainControl = (PlaceHolder)this.Form.FindControl("MainContent");
  
              foreach (String controlName in controls)
              {
                  Control controlToRemove = this.Form.FindControl("MainContent").FindControl(controlName);
                  mainControl.Controls.Remove(controlToRemove);
  
              }
              var controls2 = mainControl.Controls;
              //clearing the controls in the dictionary
              adapter.ClearAllControls();
          }
  
  
      }
  </code>
  </pre>
  
  <p>But the similar code with Clear() method is working fine. So what shall I do about it?</p>
  
  <pre><code>
      private void ClearDynamicControls()
      {
          if (adapter != null)
          {
              //This has all the controls saved in some dictionary, key as control ID
              var controls = adapter.GetAllControls().Keys;
              Control mainControl = (PlaceHolder)this.Form.FindControl("MainContent");
              mainControl.Controls.Clear();
  
  
              //clearing the controls in the dictionary
              adapter.ClearAllControls();
          }
  
  
      }
  </code>
  </pre>
  
  <p>By this code, all the controls(both dynamic and static) are removed. So what shall be done about it? </p>
  
  <p>Please let me know if I am doing something wrong. </p>
  
  <p>I am calling this method on dropdown selection change event firing. These controls are added to the table...</p>