Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1013
  • Last Modified:

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

0
ABUZYED
Asked:
ABUZYED
  • 4
  • 3
1 Solution
 
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
 
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
Transaction-level recovery for Oracle database

Veeam Explore for Oracle delivers low RTOs and RPOs with agentless transaction log backup and transaction-level recovery of Oracle databases. You can restore the database to a precise point in time, even to a specific transaction.

 
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

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now