Looping through the controls in webform? why this code doesn't work??
-
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
-
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
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).
-
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).
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
-
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
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
-
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).
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
-
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
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
-
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
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
-
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
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/