Recursive method to reset controls in a ControlCollection

This is a shared public method used to recursively interate through the controls in a control collection and set specified controls to an empty or unselected state.

First, I build two private shared helper methods to deal with an individual control. The first one checks to see what kind of control it is:

‘Helper function to check for type of control in a collection
Private Shared Function CheckControlType(ByVal ctl As Control) As Boolean

If TypeOf (ctl) Is TextBox Or TypeOf (ctl) Is DropDownList Or _
TypeOf (ctl) Is CheckBox Or TypeOf (ctl) Is RadioButton Or _
TypeOf (ctl) Is ListBox Or _
(TypeOf (ctl) Is Label And ctl.ID.StartsWith("lbl")) Then

Return True

End If
End Function

You’ll notice that for a label control I also test to see if the label’s ID starts with the characters ‘lbl’. This is because the ASP.NET Validation Controls are treated as Labels when looping through the collection, and I don’t want to evaluate those. So I make sure I name labels I want to check lblSomething so I can specify that only those will be checked.

The second helper method resets the control:

‘Helper function to reset fields
Private Shared Sub ResetFields(ByVal ctl As Control)

If TypeOf (ctl) Is TextBox Then
CType(ctl, TextBox).Text = “”
ElseIf TypeOf (ctl) Is Label Then
CType(ctl, Label).Text = “”
ElseIf TypeOf (ctl) Is DropDownList Then
CType(ctl, DropDownList).SelectedValue = “-1”
ElseIf TypeOf (ctl) Is CheckBox Then
CType(ctl, CheckBox).Checked = False
ElseIf TypeOf (ctl) Is RadioButton Then
CType(ctl, RadioButton).Checked = False
ElseIf TypeOf (ctl) Is ListBox Then
CType(ctl, ListBox).SelectedValue = “”
End If

End Sub

Once these helpers are in place, I recursively loop through the control collection and call the helpers as needed.

‘Clear fields in a collection
Public Shared Sub ClearFields(ByVal Controls As ControlCollection)

‘Iterate through control collection – if type of control is textbox,
‘dropdownlist, checkbox, radiobutton, label or listbox, clear control
For Each ctl As Control In Controls

‘clear main controls in collection
If CheckControlType(ctl) Then

‘Recursion – if control has its own control collection (e.g., panel, div, etc)
‘method calls itself to clear that collection
ElseIf ctl.HasControls Then

End If

End Sub

I keep this method in a class called Presentation, which contains a variety of utility methods having to do with formatting and configuring pages and controls. I can then call this public shared method from anywhere in my code, passing it whatever ControlCollection I want it to process:


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s