Register | Login
Friday, July 04, 2008

Sections
  
About Us
  
Hosting Provided by Server Intellect
Partners
Downloads
  
 WWWCoder.com Resource Directory

Lock the Controls Down on a Page
2/21/2006 9:20:50 AM

The following blocks of code will provide you with a method of iterating through all the controls on a page and performing some change on the properties of the control. In this particular example, we wanted to provide a simple means of locking controls down to users who did not have edit permissions in the application. This allowed the data to still be viewable to the user, but no means to edit the information.

The following blocks of code will provide you with a method of iterating through all the controls on a page and performing some change on the properties of the control. In this particular example, we wanted to provide a simple means of locking controls down to users who did not have edit permissions in the application. This allowed the data to still be viewable to the user, but no means to edit the information.

Implementing the class is fairly easy, the security mechanisms are all contained within a single class called Security.vb. Contained in this class are three methods, one method to iterate through the controls on the page, another to lockdown the page, and another helper method to determine the control's type and do the actual lockdown. The first methods we'll look at is the LockDownPage. This methods is going to be called by the page where we want to lockdown the controls. It accepts the page to be locked down, and an optional value to manually setting a read or write access.

Public Sub LockDownPage(ByVal Page As System.Web.UI.Page, _
       Optional
ByVal Access As String = "R")
        Dim mycontrol As Control
        For Each mycontrol In Page.Controls
            If Access = "R" Then
                IterateControl(mycontrol, True)
            ElseIf Access = "W" Or Access = "A" Then
                IterateControl(mycontrol, False)
            End If
        Next
    End Sub

In the previous methods you can see if the access is set to Read or "R", we're going to call IterateControl and pass the current control as well as the boolean value to tell the method to lock down the control.

    'routine to iterate through all controls on the page.
    Private Sub IterateControl(ByVal controlObject As Control, _
        
ByVal Lock As Boolean)
        Dim currentControl As Control
        If Lock Then
            LockDown(controlObject)
        Else
            LockDown(controlObject, True)
        End If
        For Each currentControl In controlObject.Controls
            IterateControl(currentControl, Lock)
        Next
    End Sub

Here we checked the Boolean value and then called the LockDown, we also call IterateControl again to see if there are child controls of the current control. Many controls are nested within other controls so in order to make sure we hit every control on the page we need to do this.

'lockdown routine, checks control type and applies appropriate disable action.

    Private Sub LockDown(ByVal UIControl As Control, _
              Optional
ByVal ControlEnabled As Boolean = False)
        Try
            Select Case UIControl.GetType.Name
                Case "TextBox"
                    If UIControl.ID = "txtNeedToConfirm2" Then
                        CType(UIControl, _
                          Web.UI.WebControls.TextBox).Enabled = ControlEnabled
                    Else
                        CType(UIControl, _
                          Web.UI.WebControls.TextBox).Enabled = ControlEnabled
                    End If
                Case "DropDownList"
                    CType(UIControl, _
                       Web.UI.WebControls.DropDownList).Enabled = ControlEnabled
                Case "RadioButtonList"
                    CType(UIControl, _
                      Web.UI.WebControls.RadioButtonList).Enabled = ControlEnabled
                Case "CheckBox"
                    CType(UIControl, _
                      Web.UI.WebControls.CheckBox).Enabled = ControlEnabled
                Case "HtmlTextArea"
                    CType(UIControl, _
                      Web.UI.HtmlControls.HtmlTextArea).Disabled = Not ControlEnabled
                Case "Button"
                    CType(UIControl, _
                      Web.UI.WebControls.Button).Enabled = ControlEnabled
                Case "LinkButton"
                    If UIControl.ID = "lnkDelete" Then
                        CType(UIControl, _
                         Web.UI.WebControls.LinkButton).Enabled = ControlEnabled
                    End If
            End Select
        Catch ex As Exception
        End Try
End Sub

In this last method have the control passed to the method. Here we determine the control type. It seems like a lot to do instead of just making the control disabled. But, we wanted to have some room to grow in case we wanted to change the properties of the individual control In addition certain controls like HTMLControls do not have the "Enabled" property, rather their corresponding property is "Disabled".

Finally we wrap the above methods in our Security class and then make the call from the page we want to lockdown. We make this call in the Page_Load method within the calling page like so:

If Session("AccessLevel") = 0 Then
    Dim uc As New Security
    uc.LockDownPage(Me.Page)
End If

You can see we have some kind of security and authentication method built into the application. In this example, it's a Session variable we're checking. If the level is zero, then we just call the lockdown, no value is passed to the optional parameter of the function since by default it will lockdown the page.


Page Options:
format for printing  Format for Printer
email article  Email Page
add to your favorites   Add to Favorites
How would you rate the quality of this content?
Poor - - Excellent
Comments?
Overall Rating:
Comments Left:
Left on 4/24/2008 1:51:20 PM by Anonymous
Comments: This was exactly what I was looking for...Thank You.
Left on 4/9/2007 12:14:47 AM by Anonymous
Comments:
Left on 2/25/2007 10:10:15 PM by Anonymous
Comments: I cannot get this to work - always get casting errors to literal controls errors..
No ratings available.
Left on 4/8/2006 6:37:04 AM by Anonymous
Comments: I like the concept and parts of the implementation. This is something that I had planned on doing to some registered content areas of a site I'm working on. Although, for my needs, this is not exactly the way I would do the implementation, but the article did make me think, therefore I must give it a good rating. Thanks. :D
  

 Latest Articles
  

 Latest News
  

 

Spotlight
Syndication

 


 


Digg This
 


DotNetNuke Platinum Benefactor

  
 

 Terms Of Use | Privacy Statement
 Copyright 2008 - Santry Technology Solutions, Box 172, Girard, PA 16417, (814) 774-0970