title: Selectively disabling WebControl elements
body: <p>I have an ASP.Net MasterPage with a PlaceHolder element.
  The contents of the PlaceHolder can be viewed in two modes: read-write, and read-only.</p>
  
  <p>To implement read only, I wanted to disable all inputs inside the PlaceHolder.<br>
  I decided to do this by recursively looping through the controls collection of the PlaceHolder, finding all the ones which inherit from WebControl, and setting <code>control.Enabled = false;</code>.</p>
  
  <p>Here's what I originally wrote:</p>
  
  <pre><code>private void DisableControls(Control c)
  {
      if (c.GetType().IsSubclassOf(typeof(WebControl)))
      {
          WebControl wc = c as WebControl;
          wc.Enabled = false;
      }
  
      //Also disable all child controls.
      foreach (Control child in c.Controls)
      {
          DisableControls(child);
      }
  }
  </code></pre>
  
  <p>This worked fine, and all controls are disabled... But then the requirement changed ;)<br>
  NOW, we want to disable all controls <em>except</em> ones which have a certain CssClass.</p>
  
  <p>So, my first attempt at the new version:</p>
  
  <pre><code>private void DisableControls(Control c)
  {
      if (c.GetType().IsSubclassOf(typeof(WebControl)))
      {
          WebControl wc = c as WebControl;
          if (!wc.CssClass.ToLower().Contains("someclass"))
              wc.Enabled = false;
      }
  
      //Also disable all child controls.
      foreach (Control child in c.Controls)
      {
          DisableControls(child);
      }
  }
  </code></pre>
  
  <p>Now I've hit a problem. If I have (for example) an <code>&lt;ASP:Panel&gt;</code> which contains an <code>&lt;ASP:DropDownList&gt;</code>, and I want to keep the DropDownList enabled, then this isn't working.</p>
  
  <p>I call DisableControls on the Panel, and it gets disabled. It then loops through the children, and calls DisableControls on the DropDownList, and leaves it enabled (as intended). However, because the Panel is disabled, when the page renders, everything inside the <code>&lt;div&gt;</code> tag is disabled!</p>
  
  <p>Can you think of a way round this? I've thought about changing <code>c.GetType().IsSubclassOf(typeof(WebControl))</code> to <code>c.GetType().IsSubclassOf(typeof(SomeParentClassThatAllInputElementsInheritFrom))</code>, but I can't find anything appropriate!</p>