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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Luis PérezSoftware 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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.