Link to home
Start Free TrialLog in
Avatar of ABUZYED
ABUZYED

asked on

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

ASKER CERTIFIED SOLUTION
Avatar of Luis Pérez
Luis Pérez
Flag of Spain image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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
Avatar of ABUZYED
ABUZYED

ASKER

how to exclude labels or buttons from this disable??
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.
Avatar of ABUZYED

ASKER

the function DisableThisContro() is not working when the controls inside panel.

Can you post any error message?
Avatar of ABUZYED

ASKER

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 .