To loop thru controls on an ASP.NET page
-
I know I'm posting alot today but the project I'm working on seems to be giving me a real taste of basic asp.net. I need to like run a loop on my asp.net page and disable a number of textboxes on the page within the loop. I know how it used to be done in good old vb6 but its not working in the exact same way on an asp.net page. I get stuck when I try to disable the control. My code flow is like this
for each ctl in page.controls if typeof ctl is textbox ctl.enabled = false ' This line doesn't work infact during design time ' the IDE doesn't even recognise ".enabled" as ' a property of the control endif next
I really need all the help I can get:^) -
I know I'm posting alot today but the project I'm working on seems to be giving me a real taste of basic asp.net. I need to like run a loop on my asp.net page and disable a number of textboxes on the page within the loop. I know how it used to be done in good old vb6 but its not working in the exact same way on an asp.net page. I get stuck when I try to disable the control. My code flow is like this
for each ctl in page.controls if typeof ctl is textbox ctl.enabled = false ' This line doesn't work infact during design time ' the IDE doesn't even recognise ".enabled" as ' a property of the control endif next
I really need all the help I can get:^)You need to cast the control as a textbox. I don't know VB, but in C# it's
TextBox tb = (TextBox)ctl; tb.Enabled = false;
I believe it is the CType() function in VB that you need. Jeff Martin My Blog -
I know I'm posting alot today but the project I'm working on seems to be giving me a real taste of basic asp.net. I need to like run a loop on my asp.net page and disable a number of textboxes on the page within the loop. I know how it used to be done in good old vb6 but its not working in the exact same way on an asp.net page. I get stuck when I try to disable the control. My code flow is like this
for each ctl in page.controls if typeof ctl is textbox ctl.enabled = false ' This line doesn't work infact during design time ' the IDE doesn't even recognise ".enabled" as ' a property of the control endif next
I really need all the help I can get:^)Here's the code for finding the controls on the page: For Each ctrl As WebControl In Page.Controls If TypeOf ctrl Is TextBox Then Dim txtBox As TextBox = DirectCast(ctrl, GetType(TextBox)) txtBox.Enabled = True End If Next If you have any questions, let me know. ~Javier Lozano (blog)