Get all Server Side Controls in Asp.Net Web Application

Find or Return all Server Side Controls in Asp.Net Web Application

December 2, 2020

In this article we will cover how to find or return all server side controls in the Asp.Net Web Application

Get all Controls (This method will return all controls weather, it's visible true or false)

public IEnumerable<ControlGetAllControls(Control parent)
{
    foreach (Control control in parent.Controls)
    {
        yield return control;
        foreach (Control descendant in GetAllControls(control))
        {
            yield return descendant;
        }
    }
}

Get all Visible Controls (This method will return all visible controls)

public IEnumerable<ControlGetAllVisibleControls(Control parent)
{
    foreach (Control control in parent.Controls)
    {
        if (control.Visible)
            yield return control;
 
        foreach (Control descendant in GetAllVisibleControls(control))
        {
            if (control.Visible)
                yield return descendant;
        }
    }
}

Call Function to return all controls

List<Controllstweb = GetAllControls(this.Page).ToList();
List<Controllstweb_visible = GetAllVisibleControls(this.Page).ToList();
//Pass any parent control as parameter from which need to return all the controls.

Post Comments(0)

Leave a reply

Will not be displayed in comment box .

Loading...