Enable/Disable All Controls, and Cascade down to child controls

16. June 2009

Read this article in your language IT | EN | DE | ES

I have some pages I work on for a project where if the user does not have access to modify the page, we disable all the controls. A ReadOnly version.

Well the current system had a method that went control by control, disabling or setting to readonly for textboxes and setting buttons to visible false.

So anyway, I was working on a new page and thought, hell no I’m going to manually type in all those controls and disable it all.  So I wrote my own function.

So use it if you like. It basically is similar to my cascading findcontrol function. I loop through controls and controls of controls and disable, hide or whatever based on the type of control it is. You may want to add more types of controls.

Here is the function you would fun. Sending it a Server Control and telling it if you want it to cascade or not.

''' <summary>
''' Loops through each control in the sent control and disables each control if it can.
''' </summary>
''' <param name="ControlContainer">Server control to check for child controls</param>
''' <param name="DisableControlsOnEachChildControl">If true, then recursively disable all the controls for each child control</param>
''' <remarks></remarks>
Protected Sub DisableAllControlsInGivenControl(ByVal ControlContainer As Control, Optional ByVal DisableControlsOnEachChildControl As Boolean = False)
    For Each Control In ControlContainer.Controls
        If DisableControlsOnEachChildControl AndAlso (Control.Controls IsNot Nothing AndAlso Control.Controls.Count > 0) Then
            DisableAllControlsInGivenControl(Control, True)
        End If
        DisableControl(Control)
    Next
End Sub

Once you have that on your page, you’ll notice it has a supporting method. DisableControl. This is the meat and potatoes of it. Here is where you would want to make your changes if you wanted to add more controls.

 

''' <summary>
    ''' This will take any control type that inherited from ui.webcontrol and attempt to disable it.
    ''' Good if you want to loop through all controls in a container and disable everything.
    ''' </summary>
    ''' <param name="ControlToDisable">The Control You wish to Disable (if it inherits from the WebControl Class)</param>
    ''' <remarks></remarks>
    Private Sub DisableControl(ByVal ControlToDisable As Control)
        DisableControl(ControlToDisable, GetType(Object))
    End Sub
 
    ''' <summary>
    ''' Will take a control and then check to see if it's of a certain system.type and then will try to disable it.
    ''' Good if you want to loop through a container and disable all X type controls.
    ''' </summary>
    ''' <param name="ControlToDisable">The Control You wish to Disable (if it inherits from the WebControl Class)</param>
    ''' <param name="ControlType">The System.Type of Control you wish to verify that the ControlToDisable is of said Type</param>
    ''' <remarks>Ex. Disable Control if it's a RadioButton: DisableControl(SomeControl, GetType(RadioButton))</remarks>
    Private Sub DisableControl(ByVal ControlToDisable As Control, ByVal ControlType As Type)
        If (ControlToDisable.GetType() Is ControlType) OrElse (ControlType Is GetType(Object)) Then
            Dim control As Object = CType(ControlToDisable, Object)
            Try
 
                If (ControlToDisable.GetType() Is GetType(ImageButton)) Then 'If ImageButton, then visible false
                    CType(control, ImageButton).Visible = False
                ElseIf (ControlToDisable.GetType() Is GetType(Button)) Then 'If Button then visible false
                    CType(control, Button).Visible = False
                ElseIf (ControlToDisable.GetType() Is GetType(TextBox)) Then 'If textbox then set ReadOnly
                    CType(control, TextBox).ReadOnly = True
                Else
                    control.Enabled = False 'Else just disable
                End If
 
            Catch ex As Exception
            End Try
        End If
    End Sub

Enjoy. Got a better way? Let me know.

 

 

Update: For those of you who asked for it. Here’s an EnableAllControlsInGiven…. I didn’t need this as controls are only disabled is you don’t have access.

''' <summary>
   ''' Loops through each control in the sent control and Enables each control if it can.
   ''' </summary>
   ''' <param name="ControlContainer">Server control to check for child controls</param>
   ''' <param name="EnableControlsOnEachChildControl">If true, then recursivly Enable all the controls for each child control</param>
   ''' <remarks></remarks>
   Protected Sub EnableAllControlsInGivenControl(ByVal ControlContainer As Control, Optional ByVal EnableControlsOnEachChildControl As Boolean = False)
       For Each Control In ControlContainer.Controls
           If EnableControlsOnEachChildControl AndAlso (Control.Controls IsNot Nothing AndAlso Control.Controls.Count > 0) Then
               EnableAllControlsInGivenControl(Control, True)
           End If
           EnableControl(Control)
       Next
   End Sub

Code Snippets, Programming, VB.NET , ,

Add comment


(Will show your Gravatar icon)

  Country flag

Click to change captcha
biuquote
  • Comment
  • Preview
Loading