Organizational Research By

Surprising Reserch Topic

c sharp get all web controls on page


c sharp get all web controls on page  using -'c#,asp.net'

I have been pondering how I can get all controls on a page and then perform a task on them this is a related question:

How to Search Through a C# DropDownList Programmatically

So I want the code to scan the page, get all DropDownList Controls and put them into a list of some sort. Then I want to know how I can access them in this list.

Using c# asp.net ofcourse.

I am currently having to edit each individual control would rather do one function to do them all for me.
    

asked Sep 11, 2015 by IngeMacdermo
0 votes
8 views



Related Hot Questions

5 Answers

0 votes

Check my previous SO answer.

Basically, the idea is to wrap the recursion of iterating through the controls collection using :

private void GetControlList(ControlCollection controlCollection, List resultCollection)
where T : Control
{
    foreach (Control control in controlCollection)
    {
        //if (control.GetType() == typeof(T))
        if (control is T) // This is cleaner
            resultCollection.Add((T)control);

        if (control.HasControls())
            GetControlList(control.Controls, resultCollection);
    }
}

and to use it :

List allControls = new List();
GetControlList(Page.Controls, allControls )
foreach (var childControl in allControls )
{
//     call for all controls of the page
}

[Edited 11/26/2013]: here is a more elegant way to reach this goal. I wrote two extensions methods that can walk the control tree in both directions. The methods are written in a more Linq way as it produces an enumerable:

/// 
/// Provide utilities methods related to  objects
/// 
public static class ControlUtilities
{
    /// 
    /// Find the first ancestor of the selected control in the control tree
    /// 
    /// Type of the ancestor to look for
    /// The control to look for its ancestors
    /// The first ancestor of the specified type, or null if no ancestor is found.
    public static TControl FindAncestor(this Control control) where TControl : Control
    {
        if (control == null) throw new ArgumentNullException("control");

        Control parent = control;
        do
        {
            parent = parent.Parent;
            var candidate = parent as TControl;
            if (candidate != null)
            {
                return candidate;
            }
        } while (parent != null);
        return null;
    }

    /// 
    /// Finds all descendants of a certain type of the specified control.
    /// 
    /// The type of descendant controls to look for.
    /// The parent control where to look into.
    /// All corresponding descendants
    public static IEnumerable FindDescendants(this Control parent) where TControl : Control
    {
        if (parent == null) throw new ArgumentNullException("control");

        if (parent.HasControls())
        {
            foreach (Control childControl in parent.Controls)
            {
                var candidate = childControl as TControl;
                if (candidate != null) yield return candidate;

                foreach (var nextLevel in FindDescendants(childControl))
                {
                    yield return nextLevel;
                }
            }
        }
    }
}

Thanks to the this keyword, these methods are extensions methods and can simplify the code.

For example, to find all DropDownList in the page, you can simply call:

var allDropDowns = this.Page.FindControl();

Because of the use of the yield keyword, and because Linq is smart enough to defer execution of the enumeration, you can call (for example):

var allDropDowns = this.Page.FindControl();
var firstDropDownWithCustomClass = allDropDowns.First(
    ddl=>ddl.CssClass == "customclass"
    );

The enumeration will stop as soon as the predicate in the First method is satisfied. The whole control tree won't be walked.

answered Sep 11, 2015 by AlexisHorgan
0 votes

Had this very question and while I found Steve B's answer useful, I wanted an extension method, so re-factored it:

    public static IEnumerable GetControlList(this ControlCollection controlCollection) where T : Control
    {
        foreach (Control control in controlCollection)
        {
            if (control is T)
            {
                yield return (T)control;
            }

            if (control.HasControls())
            {
                foreach (Control childControl in control.Controls.GetControlList())
                {
                    yield return childControl;
                }
            }
        }
    }
answered Sep 11, 2015 by JanMoyer
0 votes

Looping through controls on a page isn't hard - you just have to look within each control for more controls.

You could do something like

foreach(var control in Page)
{
    if(control is DropDownList)
    {
        //Do whatever
    }
    else
    {
        //Call this function again to search for controls within this control
    }
}
answered Sep 11, 2015 by MindaXQOdjqo
0 votes
        var dropDownLists = new List();
        foreach (var control in this.Controls)
        {
            if (control is DropDownList)
            {
                dropDownLists.Add( (DropDownList)control );
            }
        }
answered Sep 11, 2015 by BrookLopesp
0 votes

You can use recursive logic to get all of the controls, like this:

private void PopulateSelectList(Control parentCtrl, List selectList)
{
    foreach (Control ctrl in parentCtrl.Controls)
    {
        if (ctrl is DropDownList)
        {
            selectList.Add(((DropDownList)ctrl);
            continue;
        }
        FindAllControls(ctrl, selectList);
    }
}
answered Sep 11, 2015 by FFBGin

...