10

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?

mdb
  • 52,000
  • 11
  • 64
  • 62
balaweblog
  • 14,982
  • 28
  • 73
  • 95

5 Answers5

30

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
            }
        }
Cristian Libardo
  • 9,260
  • 3
  • 35
  • 41
4

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.

GeekyMonkey
  • 12,478
  • 6
  • 33
  • 39
2

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)
    {
        ret.Add(c);
        ret.AddRange(getCtls(c));
    }
    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;
}  
Community
  • 1
  • 1
theoski
  • 109
  • 1
  • 5
  • I think Christian's would be better since his approach seems to evaluate lazily, and mine wouldn't. – theoski Apr 17 '14 at 16:23
0

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).

http://intrepidnoodle.com/articles/24.aspx

Andrew Corkery
  • 1,024
  • 1
  • 8
  • 13
0

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.

Richard Dorman
  • 23,170
  • 16
  • 45
  • 49