Looping through controls in ASP.Net C#












1














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?










share|improve this question





























    1














    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?










    share|improve this question



























      1












      1








      1







      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?










      share|improve this question















      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?







      c# asp.net visual-studio-2010






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 13 '18 at 4:43









      Cœur

      17.4k9103145




      17.4k9103145










      asked Aug 30 '10 at 6:50









      CyberK

      1,04722643




      1,04722643
























          4 Answers
          4






          active

          oldest

          votes


















          1














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






          share|improve this answer































            0














            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






            share|improve this answer





























              0














              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






              share|improve this answer





























                0














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






                share|improve this answer





















                  Your Answer






                  StackExchange.ifUsing("editor", function () {
                  StackExchange.using("externalEditor", function () {
                  StackExchange.using("snippets", function () {
                  StackExchange.snippets.init();
                  });
                  });
                  }, "code-snippets");

                  StackExchange.ready(function() {
                  var channelOptions = {
                  tags: "".split(" "),
                  id: "1"
                  };
                  initTagRenderer("".split(" "), "".split(" "), channelOptions);

                  StackExchange.using("externalEditor", function() {
                  // Have to fire editor after snippets, if snippets enabled
                  if (StackExchange.settings.snippets.snippetsEnabled) {
                  StackExchange.using("snippets", function() {
                  createEditor();
                  });
                  }
                  else {
                  createEditor();
                  }
                  });

                  function createEditor() {
                  StackExchange.prepareEditor({
                  heartbeatType: 'answer',
                  autoActivateHeartbeat: false,
                  convertImagesToLinks: true,
                  noModals: true,
                  showLowRepImageUploadWarning: true,
                  reputationToPostImages: 10,
                  bindNavPrevention: true,
                  postfix: "",
                  imageUploader: {
                  brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
                  contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
                  allowUrls: true
                  },
                  onDemand: true,
                  discardSelector: ".discard-answer"
                  ,immediatelyShowMarkdownHelp:true
                  });


                  }
                  });














                  draft saved

                  draft discarded


















                  StackExchange.ready(
                  function () {
                  StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f3598420%2flooping-through-controls-in-asp-net-c-sharp%23new-answer', 'question_page');
                  }
                  );

                  Post as a guest















                  Required, but never shown

























                  4 Answers
                  4






                  active

                  oldest

                  votes








                  4 Answers
                  4






                  active

                  oldest

                  votes









                  active

                  oldest

                  votes






                  active

                  oldest

                  votes









                  1














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






                  share|improve this answer




























                    1














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






                    share|improve this answer


























                      1












                      1








                      1






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






                      share|improve this answer














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







                      share|improve this answer














                      share|improve this answer



                      share|improve this answer








                      edited May 23 '17 at 9:57









                      Community

                      11




                      11










                      answered Aug 30 '10 at 6:52









                      Danny Chen

                      30k1484137




                      30k1484137

























                          0














                          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






                          share|improve this answer


























                            0














                            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






                            share|improve this answer
























                              0












                              0








                              0






                              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






                              share|improve this answer












                              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







                              share|improve this answer












                              share|improve this answer



                              share|improve this answer










                              answered Aug 30 '10 at 6:58









                              mamoo

                              7,63022137




                              7,63022137























                                  0














                                  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






                                  share|improve this answer


























                                    0














                                    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






                                    share|improve this answer
























                                      0












                                      0








                                      0






                                      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






                                      share|improve this answer












                                      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







                                      share|improve this answer












                                      share|improve this answer



                                      share|improve this answer










                                      answered Aug 30 '10 at 7:02









                                      Sandeep Pathak

                                      7,97953855




                                      7,97953855























                                          0














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






                                          share|improve this answer


























                                            0














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






                                            share|improve this answer
























                                              0












                                              0








                                              0






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






                                              share|improve this answer












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







                                              share|improve this answer












                                              share|improve this answer



                                              share|improve this answer










                                              answered Aug 30 '10 at 13:23









                                              CyberK

                                              1,04722643




                                              1,04722643






























                                                  draft saved

                                                  draft discarded




















































                                                  Thanks for contributing an answer to Stack Overflow!


                                                  • Please be sure to answer the question. Provide details and share your research!

                                                  But avoid



                                                  • Asking for help, clarification, or responding to other answers.

                                                  • Making statements based on opinion; back them up with references or personal experience.


                                                  To learn more, see our tips on writing great answers.





                                                  Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                                                  Please pay close attention to the following guidance:


                                                  • Please be sure to answer the question. Provide details and share your research!

                                                  But avoid



                                                  • Asking for help, clarification, or responding to other answers.

                                                  • Making statements based on opinion; back them up with references or personal experience.


                                                  To learn more, see our tips on writing great answers.




                                                  draft saved


                                                  draft discarded














                                                  StackExchange.ready(
                                                  function () {
                                                  StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f3598420%2flooping-through-controls-in-asp-net-c-sharp%23new-answer', 'question_page');
                                                  }
                                                  );

                                                  Post as a guest















                                                  Required, but never shown





















































                                                  Required, but never shown














                                                  Required, but never shown












                                                  Required, but never shown







                                                  Required, but never shown

































                                                  Required, but never shown














                                                  Required, but never shown












                                                  Required, but never shown







                                                  Required, but never shown







                                                  Popular posts from this blog

                                                  Bressuire

                                                  Vorschmack

                                                  Quarantine