views:

1142

answers:

6

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?

+2  A: 

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
+1  A: 

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
Worked like a charm! Thanks GeekyMonkey, and thanks, as ever Stack Overflow!
aape
A: 

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
A: 

As Dorman stated, I can use the Observer pattern. Check out the MSDN explanation and sample @ http://msdn.microsoft.com/en-us/library/ms954621.aspx

Hope it helps, Bruno Figueiredo http://www.brunofigueiredo.com

Bruno Shine
A: 

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
A: 

Very help full

thanks

Navin