简体   繁体   中英

Looping through controls in ASP.Net C#

I have a page, which contains a table, with a couple of rows, and in that row there are checkboxes.

Now the thing I want, is to loop trough all checkboxes to see if they are checked or not.

This is my current approach:

foreach (Control c in Page.Controls)
{
    if(c is Checkbox){
    }
}

Now the problem is that I receive only 2 controls, the page and the Table. So the checkboxes are in: Table -> TableRow -> TableCell -> CheckBox

Is there a way to get ALL controls on a page, instead of having to nest into it to get out the controls?

control.Controls will return the first level child controls only. For details, check this question .

Since the controls are hierarchical, every control sees only it's child controls. You have to iterate through every control to get them all.

You can find an example on how to do it here: http://www.atrevido.net/blog/CommentView,guid,c792adbf-ce0a-4bf9-a61c-ca1a4296d0ea.aspx

A Control can act as a parent to a collection of controls as in your case .You should Iterate through the child of the parent control you are using in you page. refer http://msdn.microsoft.com/en-us/library/system.windows.forms.control.controls%28VS.71%29.aspx

I just did a nested foreach loop like this:

List<Control> allControls = new List<Control>();
            List<string> selectedIDs = new List<string>();

        foreach (Control c in this.pnlTable.Controls)
        {
            allControls.Add(c);

            if (c.Controls.Count > 0)
            {
                foreach (Control childControl in c.Controls)
                {
                    allControls.Add(childControl);

                    if (childControl.Controls.Count > 0)
                    {
                        foreach (Control childControl2 in childControl.Controls)
                        {
                            allControls.Add(childControl2);

                            if (childControl2.Controls.Count > 0)
                            {
                                foreach (Control childControl3 in childControl2.Controls)
                                {
                                    allControls.Add(childControl3);
                                }
                            }
                        }
                    }
                }
            }
        }

        foreach (Control control in allControls)
        {
            if (control is CheckBox)
            {
                if (((CheckBox)(control)).Checked)
                {
                    selectedIDs.Add(((CheckBox)(control)).ID);
                }
            }
        }

Depending of the depth of the control I added a if and foreach..

Hope this helps someone else with the same issue...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM