Click here to Skip to main content
15,909,193 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm trying to make a function to enable disable all controls in a specific page.

When I loop through, all controls are disabled except the ones inside the div that is set to runat="server"

This is a general view of the design:

ASP.NET
<form id="form1"  runat="server">
   <div id="wrapper">
   <%-- 1st set of ASP controls --%>
      <div id="Main"  runat="server">
         <%-- 2nd set ASP of controls --%>
      </div>
   </div>
<form>


My code looks like this:

VB
For Each c As Control In Page.Controls
   For Each ctrl As Control In c.Controls
      'disabling controls 
   Next
Next


I want to use this function in all my pages, please could you let me know how to loop through the divs that are runat="Server"?
Posted

Have you looked this link Enable or disable all controls in the page using simple code[^]

Regards,
Pavan N
 
Share this answer
 
Comments
Salem Rady 22-Jan-14 5:33am    
Yea man already checked it my code is similar to it, my problem is that my loop won't go through the div that is runat="server" it will loop through all divs and containers except the runat="server" ones
By using this code below, I will be able to disable the not only the children of the page but also controls that are children of controls that are children of the page
VB
Private Sub DisableChildControls(controls As ControlCollection)

        For Each ctrl As Control In controls
           'I disable what I want to disable -- for example

           If TypeOf ctl Is TextBox Then

                DirectCast(ctl, TextBox).Enabled = False

            ElseIf TypeOf ctl Is Button Then

                DirectCast(ctl, Button).Enabled = False

            ElseIf TypeOf ctl Is RadioButton Then

                DirectCast(ctl, RadioButton).Enabled = False

            ElseIf TypeOf ctl Is RadioButtonList Then

                DirectCast(ctl, RadioButtonList).Enabled = False

            ElseIf TypeOf ctl Is CheckBox Then

                DirectCast(ctl, CheckBox).Enabled = False

            ElseIf TypeOf ctl Is CheckBoxList Then

                DirectCast(ctl, CheckBoxList).Enabled = False

            ElseIf TypeOf ctl Is DropDownList Then

                DirectCast(ctl, DropDownList).Enabled = False

            ElseIf TypeOf ctl Is ListBox Then

                DirectCast(ctl, ListBox).Attributes.Add("disabled", "disabled")

            End If

          If ctrl.Controls.Count > 0 Then
                DisableChildControls(ctrl.Controls)
            End If
        Next
    End Sub

In the desired event, I call the traversal using:
VB
If Me.Controls.Count > 0 Then
    DisableChildControls(Me.Controls)
End If
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900