Tip of the Day

18. June 2009

In asp.net instead of

Response.End

Use

HttpContext.Current.ApplicationInstance.CompleteRequest()

Code Snippets, Tip of the Day

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

16. June 2009

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 , ,

Tip of the Day

15. June 2009

Sometimes your asp.net pages work better when you don't accidently delete the whole Page_Load method.

Tip of the Day

Using Resource File to Populate DropDownList

13. June 2009

I had desire to populate a DropDownList with values from a resource file. Using the resource name as the ddl value and the resource value as ddl text.  There’s other reasons I need to do this but I’m simplifying it here.

The resource file name is: “BatchFields_ADMRACClaim
The DropDownList is called “ddlFeilds”

So first I get the resource file object and then I do a GetType() to initialize it. If anyone has any other suggestions for this, please let me know. Without the GetType() line or pulling out a value first, the GetResourceSet later is Nothing.

Dim BatchFileResource As ResourceManager
BatchFileResource = Resources.BatchFields_ADMRACClaim.ResourceManager()
Dim Value As String = BatchFileResource.GetType.Assembly.FullName
 

Then once I have the resource file in the “BatchFileResource” object, I can then loop through the items and dump them into a DropDownList.

Dim BatchResourceSet As ResourceSet = BatchFileResource.GetResourceSet(System.Globalization.CultureInfo.CurrentCulture, True, False)
Dim BatchEnumerator As IDictionaryEnumerator = BatchResourceSet.GetEnumerator()
ddlFields.Items.Clear()
While BatchEnumerator.MoveNext
Dim ddlItem As New ListItem(BatchEnumerator.Value, BatchEnumerator.Key)
ddlFields.Items.Add(ddlItem)
End While

And that’s it. I’ve found other samples on the web but I’ve combined the best of both worlds here.

ASP.NET, Code Snippets, Programming, VB.NET , ,

My Cascading Nested FindControl Function

5. June 2009

There are many examples out there of cascading methods to find a control nested down in another control which is in some other control.  So here’s my version.  Maybe there’s better ways, but this one works for me.  Returns Nothing if it’s not found. So capture for that.

''' <summary>
''' Does a FindControl on the given Control and if not found,
''' cascades down into the controls of the control until
''' found or no more controls and returns nothing
''' </summary>
''' <param name="StartingControl">Control to start with</param>
''' <param name="FindThisID">ID of control you are looking for</param>
''' <returns></returns>
''' <remarks></remarks>
Private Function FindControlCascading(ByVal StartingControl As Control, _
ByVal FindThisID As String) As Control
Try
If StartingControl.FindControl(FindThisID) Is Nothing Then
If StartingControl.Controls.Count > 0 Then
For Each objControl In StartingControl.Controls
FindControlCascading(objControl, FindThisID)
Next
End If
Else
Return StartingControl.FindControl(FindThisID)
End If
Return Nothing
Catch ex As Exception
Throw ex
End Try
End Function

Got a better way? I’d love to hear it.

Code Snippets, Programming, VB.NET ,