Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. Looping through the controls in webform? why this code doesn't work??

Looping through the controls in webform? why this code doesn't work??

Scheduled Pinned Locked Moved ASP.NET
winformsdesignhelpquestion
8 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    Rahman Mahmoodi
    wrote on last edited by
    #1

    Hello guys, I want to loop through all the controls in the webform and see if there is any checkbox and if so see if they are selected or not. The code doesn't raise any error but doesn't do anything either. It works fine in Windows forms application!! Even i changed the variable types to System.Web.UI.Control and System.Web.UI.WebControls.CheckBox. Still does nothing. Can anyone make it working please?? Any tips? Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click Dim ctl As Control Dim mychk As CheckBox ''Dim ctl As System.Web.UI.Control ''Dim mychk As System.Web.UI.WebControls.CheckBox For Each ctl In Me.Controls If (TypeOf ctl Is CheckBox) Then mychk = CType(ctl, CheckBox) If (mychk.Checked) Then Response.Write(mychk.Text) End If End If Next End Sub

    M V 2 Replies Last reply
    0
    • R Rahman Mahmoodi

      Hello guys, I want to loop through all the controls in the webform and see if there is any checkbox and if so see if they are selected or not. The code doesn't raise any error but doesn't do anything either. It works fine in Windows forms application!! Even i changed the variable types to System.Web.UI.Control and System.Web.UI.WebControls.CheckBox. Still does nothing. Can anyone make it working please?? Any tips? Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click Dim ctl As Control Dim mychk As CheckBox ''Dim ctl As System.Web.UI.Control ''Dim mychk As System.Web.UI.WebControls.CheckBox For Each ctl In Me.Controls If (TypeOf ctl Is CheckBox) Then mychk = CType(ctl, CheckBox) If (mychk.Checked) Then Response.Write(mychk.Text) End If End If Next End Sub

      M Offline
      M Offline
      minhpc_bk
      wrote on last edited by
      #2

      Hi there, It does not work because the Controls collection of the Page instance (Me) does not contain the CheckBox control. The CheckBox control is a child control of the Form html control which in turn is a child control of the Page. If you run your application in debug mode and take a look at the Controls collection, you will see something like that. Another way to see the control hierarchy of the web page is to set the Trace=true in the Page directive. The reason it works in the window-based application is that the CheckBox control is the child of the Form control (Me).

      R 2 Replies Last reply
      0
      • M minhpc_bk

        Hi there, It does not work because the Controls collection of the Page instance (Me) does not contain the CheckBox control. The CheckBox control is a child control of the Form html control which in turn is a child control of the Page. If you run your application in debug mode and take a look at the Controls collection, you will see something like that. Another way to see the control hierarchy of the web page is to set the Trace=true in the Page directive. The reason it works in the window-based application is that the CheckBox control is the child of the Form control (Me).

        R Offline
        R Offline
        Rahman Mahmoodi
        wrote on last edited by
        #3

        Hello thanks for the reply and tips. So how can you make it work then? I couldn't find something like: Me.Page.htmlForm.Controls??? How can i loop through the HTMLForm Controls? many thanks

        M 1 Reply Last reply
        0
        • R Rahman Mahmoodi

          Hello thanks for the reply and tips. So how can you make it work then? I couldn't find something like: Me.Page.htmlForm.Controls??? How can i loop through the HTMLForm Controls? many thanks

          M Offline
          M Offline
          minhpc_bk
          wrote on last edited by
          #4

          Rahman Mahmoodi wrote: So how can you make it work then? I couldn't find something like: Me.Page.htmlForm.Controls??? Do you see any child control of the System.Web.UI.HtmlControls.HtmlForm type in the Page's Controls collection? Once you got the form control, you can loop through its Controls collection as you would with the Page instance. Another way to look for a control is to use the FindControl[^] method which searches for a specific control based on the id value

          R 1 Reply Last reply
          0
          • M minhpc_bk

            Hi there, It does not work because the Controls collection of the Page instance (Me) does not contain the CheckBox control. The CheckBox control is a child control of the Form html control which in turn is a child control of the Page. If you run your application in debug mode and take a look at the Controls collection, you will see something like that. Another way to see the control hierarchy of the web page is to set the Trace=true in the Page directive. The reason it works in the window-based application is that the CheckBox control is the child of the Form control (Me).

            R Offline
            R Offline
            Rahman Mahmoodi
            wrote on last edited by
            #5

            Ok, here we are!! I did as this and now it works though very funny! Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click Dim ctl As Control Dim mychk As CheckBox For Each ctl In Me.Page.FindControl("Form1").Controls If (TypeOf ctl Is CheckBox) Then mychk = CType(ctl, CheckBox) If (mychk.Checked) Then Response.Write(mychk.Text) End If End If Next End Sub

            1 Reply Last reply
            0
            • M minhpc_bk

              Rahman Mahmoodi wrote: So how can you make it work then? I couldn't find something like: Me.Page.htmlForm.Controls??? Do you see any child control of the System.Web.UI.HtmlControls.HtmlForm type in the Page's Controls collection? Once you got the form control, you can loop through its Controls collection as you would with the Page instance. Another way to look for a control is to use the FindControl[^] method which searches for a specific control based on the id value

              R Offline
              R Offline
              Rahman Mahmoodi
              wrote on last edited by
              #6

              Thanks for your help. Looks we posted at the same time ;-) I did it in the 2nd option looks though I still don't know the first option of yours. I am doing something like this for the first option: Dim frm As System.Web.UI.HtmlControls.HtmlForm For Each ctl In frm.Controls but it raises the null reference error? that is something you meant? TA

              M 1 Reply Last reply
              0
              • R Rahman Mahmoodi

                Thanks for your help. Looks we posted at the same time ;-) I did it in the 2nd option looks though I still don't know the first option of yours. I am doing something like this for the first option: Dim frm As System.Web.UI.HtmlControls.HtmlForm For Each ctl In frm.Controls but it raises the null reference error? that is something you meant? TA

                M Offline
                M Offline
                minhpc_bk
                wrote on last edited by
                #7

                Rahman Mahmoodi wrote: Looks we posted at the same time I guess so :) Rahman Mahmoodi wrote: Dim frm As System.Web.UI.HtmlControls.HtmlForm For Each ctl In frm.Controls but it raises the null reference error? that is something you meant? In fact, I mean you first loop through the Controls collection of the page to access the Form control, then you do the same with the Controls collection of the Form instance. Also, you can declare the Form element in the code-behind as you did and loop through its Controls collection, but remember that the variable name is the same as the id of the form declared in the web page .aspx:

                Protected Form1 As System.Web.UI.HtmlControls.HtmlForm

                -- modified at 22:17 Monday 3rd October, 2005

                1 Reply Last reply
                0
                • R Rahman Mahmoodi

                  Hello guys, I want to loop through all the controls in the webform and see if there is any checkbox and if so see if they are selected or not. The code doesn't raise any error but doesn't do anything either. It works fine in Windows forms application!! Even i changed the variable types to System.Web.UI.Control and System.Web.UI.WebControls.CheckBox. Still does nothing. Can anyone make it working please?? Any tips? Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click Dim ctl As Control Dim mychk As CheckBox ''Dim ctl As System.Web.UI.Control ''Dim mychk As System.Web.UI.WebControls.CheckBox For Each ctl In Me.Controls If (TypeOf ctl Is CheckBox) Then mychk = CType(ctl, CheckBox) If (mychk.Checked) Then Response.Write(mychk.Text) End If End If Next End Sub

                  V Offline
                  V Offline
                  Vasudevan Deepak Kumar
                  wrote on last edited by
                  #8

                  Rahman, You have looped only the top level. Actually you need to recurse into each till the lowest ebb to find out the type of control. There may be UserControls which would contain Checkboxes too. :):) Vasudevan Deepak Kumar Personal Web: http://www.lavanyadeepak.tk/ I Blog At: http://deepak.blogdrive.com/

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups