title: Recursively find page controls and add attributes?
body: <p>I need a recursive function that will find all controls on a page and allow me to add javascript control attributes based on the control type.</p>
  
  <p>The issue is that I have a page with several panels which have controls.  The panels could even have nested panels/controls.</p>
  
  <p>Unfortunately the following doesn't do what I want, but I'm looking for something similar....</p>
  
  <pre><code>                Action&lt;Control&gt; traverse = null;
  
                  //in a function:
                  traverse = (ctrl) =&gt;
                  {
                      //ctrl.Enabled = false; //or whatever action you're performing
                      foreach (Control c in ctrl.Controls)
                      {
                          Response.Write(c.GetType().ToString() + " : " + c.ID.ToString() + "&lt;br /&gt;");
  
                          if (c.GetType() == typeof(TextBox))
                          {
                              ((TextBox)(c)).Attributes["onKeypress"] = "javascript:return FormEdited();";
                          }
                          else if (c.GetType() == typeof(DropDownList))
                          {
                              ((DropDownList)(c)).Attributes["onchange"] = "javascript:return FormEdited();";
                          }
                          else if (c.GetType() == typeof(CheckBox))
                          {
                              ((CheckBox)(c)).Attributes["onClick"] = "javascript:return FormEdited();";
                          }
  
                      }
  
                      traverse = (ctrl2) =&gt; ctrl.Controls.GetEnumerator();
                  };
  </code></pre>