ForEachControl

public static void ForEachControl(this ControlCollection controlCollection, Action<Control> action)
{
    if (controlCollection == null)
        return;
    foreach (Control c in controlCollection)
    {
        action(c);
        if (c.HasControls())
        {
            ForEachControl(c.Controls, action);
        }
    }
}
Example:
page.Controls.ForEachControl(c =>
                {
                    if (c is HtmlControl)
                    {
                        var ctrl = (HtmlControl)c;
                        if (ctrl.Attributes["class"] != null && ctrl.Attributes["class"] == "hidden")
                        {
                            ctrl.Visible = false;
                        }
                    }
                });

Description

Runs action delegate for all controls and subcontrols in ControlCollection.

Details

Double click on the code to select all.

 

;