how to disable all contrlos in ASP.NET page by using VB.NET code?

Hi
how to disable all controls in ASP.NET page by using VB.NET code?

i try by using this code but it was working in C# (after conversion) but not working in VB.NET
Private Sub ChangeControlStatus(ByVal status As Boolean)
   
    For Each c As Control In Page.Controls
        For Each ctrl As Control In c.Controls
           
            If TypeOf ctrl Is TextBox Then
               
                DirectCast(ctrl, TextBox).Enabled = status
           
            ElseIf TypeOf ctrl Is Button Then
               
                DirectCast(ctrl, Button).Enabled = status
           
            ElseIf TypeOf ctrl Is RadioButton Then
               
                DirectCast(ctrl, RadioButton).Enabled = status
           
            ElseIf TypeOf ctrl Is ImageButton Then
               
                DirectCast(ctrl, ImageButton).Enabled = status
           
            ElseIf TypeOf ctrl Is CheckBox Then
               
                DirectCast(ctrl, CheckBox).Enabled = status
           
            ElseIf TypeOf ctrl Is DropDownList Then
               
                DirectCast(ctrl, DropDownList).Enabled = status
           
            ElseIf TypeOf ctrl Is HyperLink Then
               
                DirectCast(ctrl, HyperLink).Enabled = status
            End If
        Next
    Next
   
End Sub

Open in new window

ABUZYEDAsked:
Who is Participating?
 
Luis PérezConnect With a Mentor Software Architect in .NetCommented:
Mmm... try better this:

Public Sub DisableControls(ByVal Container As Object)
    'First, call recursive this same function to disable all children controls
    'of the container object
    For Each children As Object In Container.Controls
        Try
            'Do it into a Try as not all objects may have the Controls property
            If children.Controls.Count > 0 Then
                Call DisableControls(children)
            End If
        Catch ex As Exception
        End Try
        Try
            'Dot it into a Try as not all objects may have the Enabled property
            children.Enabled = False
        Catch ex As Exception
        End Try
    Next
    Try
        Container.Enabled = False
    Catch ex As Exception
    End Try
End Sub
0
 
Luis PérezSoftware Architect in .NetCommented:
Sorry, forgot that you can call this function passing it what you want (including a page, of course):

Public Sub DisableWholePage()
    Call DisableControls(Me)
End Sub
0
 
ABUZYEDAuthor Commented:
how to exclude labels or buttons from this disable??
0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
Luis PérezSoftware Architect in .NetCommented:
You can add this function:
Public Function DisableThisControl(ByVal ControlType As String) As Boolean
    Select Case ControlType
        Case "System.Web.UI.WebControls.Label", "System.Web.UI.WebControl.Button"
            'Include all other that you want here
            Return False
        Case Else
            Return True
    End Select
End Function

And in the DisableControls function, in the 2 lines that executes [object].Enabled = false, call first the function. This is the definitive DisableControls function:
Public Sub DisableControls(ByVal Container As Object)
    'First, call recursive this same function to disable all children controls
    'of the container object
    For Each children As Object In Container.Controls
        Try
            'Do it into a Try as not all objects may have the Controls property
            If children.Controls.Count > 0 Then
                Call DisableControls(children)
            End If
        Catch ex As Exception
        End Try
        Try
            'Dot it into a Try as not all objects may have the Enabled property
            If DisableThisControl(children.GetType.ToString) Then children.Enabled = False
        Catch ex As Exception
        End Try
    Next
    Try
        If DisableThisControl(Container.GetType.ToString) Then Container.Enabled = False
    Catch ex As Exception
    End Try
End Sub

Hope that helps.
0
 
ABUZYEDAuthor Commented:
the function DisableThisContro() is not working when the controls inside panel.

0
 
Luis PérezSoftware Architect in .NetCommented:
Can you post any error message?
0
 
ABUZYEDAuthor Commented:
There is no error message but it was not working
now it is working after adding this adding this condition

"System.Web.UI.WebControls.Panel"

thank you .




0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.