Finding all controls in an ASP.NET Panel?

I am having a number of panels in my page in which I am collecting user information and saving the page details. The page panel has textbox, dropdown list, listbox.

When I need to come to this page. I need to show the Page if these controls have any values. How to do this?


Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

It boils down to enumerating all the controls in the control hierarchy:

    IEnumerable<Control> EnumerateControlsRecursive(Control parent)
        foreach (Control child in parent.Controls)
            yield return child;
            foreach (Control descendant in EnumerateControlsRecursive(child))
                yield return descendant;

You can use it like this:
        foreach (Control c in EnumerateControlsRecursive(Page))
            if(c is TextBox)
                // do something useful

Method 2

You can loop thru the panels controls

foreach (Control c in MyPanel.Controls) 
   if (c is Textbox) {
        // do something with textbox
   } else if (c is Checkbox) {
        /// do something with checkbox

If you have them nested inside, then you’ll need a function that does this recursively.

Method 3

I know this is an old post, and I really liked christian libardo’s solution. However, I do not like the fact that in order to yield an entire set of elements to the outer scope I would have to iterate over those elements yet again only to yield those to myself from an inner scope to the current scope. I prefer:

IEnumerable<Control> getCtls(Control par)
    List<Control> ret = new List<Control>();
    foreach (Control c in par.Controls)
    return (IEnumerable<Control>)ret;

Which allows me to use it like so:
foreach (Button but in getCtls(Page).OfType<Button>())
    //disable the button
    but.Enabled = false;

Method 4

Depeding on which UI library or language you are using, container controls such as panels maintain a list of child controls. To test if a form/page has any data you need to recursively search each panel for data entry controls such as text boxes. Then test if any of the data entry controls contain values other than default value.

A simpler solutions would be to implement an observer class that attaches to the changed events of your data controls. If the observer is triggered then your page has changes. You will need to take into consideration actions such as changing and then reverting data.

Method 5

Very similar solution to Cristian’s here, which uses recursion and generics to find any control in the page (you can specify the control at which to start searching).

All methods was sourced from or, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Notify of

Inline Feedbacks
View all comments
Would love your thoughts, please comment.x