ExtensionMethod.NET Home of 880 C#, Visual Basic, F# and Javascript extension methods

ForEachControl

Runs action delegate for all controls and subcontrols in ControlCollection.

Source

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;
        }
    }
});

Author: ivan

Submitted on: 30 aug. 2011

Language: C#

Type: System.Web.UI

Views: 5312