There might be situations where you want to retrieve controls that are present in a web page and process them. In that case this article can be very use. Basically a web page is a container for all controls and for retrieving all controls we need to traverse the control tree. So for this this program can be used to disable all form controls at runtime

Partial Class _Default
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    End Sub
    Public Sub disableControls(ByVal controltree As Control)
        For Each tempcontrol As Control In controltree.Controls
            If TypeOf tempcontrol Is TextBox Then
                CType(tempcontrol, TextBox).Enabled = False
                Continue For
            ElseIf TypeOf tempcontrol Is Label Then
                CType(tempcontrol, Label).Enabled = False
                Continue For
            ElseIf TypeOf tempcontrol Is Button Then
                CType(tempcontrol, Button).Enabled = False
                Continue For
            ElseIf tempcontrol.Controls.Count > 0 Then
            End If
    End Sub
End Class

Before using this code there are some performance issues

  • If you are calling this function from different pages and then storing the state of page is very useful for example if you already disabled all the controls then there is no need to pass the page control to this method This can be easily done with storing the page state in session variable.