How do you loop through a web forms control collection?

B

BJ

I am trying to loop through the control collection of my ASP.NET web
form in order to access the contents of each field.
It seems as if there is a parent control (Literal?) in ASP.NET.
Attached are my failed attemps as well as the
hardcoded lines that do work.

TIA


Private Sub cmdReset_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles cmdReset.Click

'Dim ctrl As Control
'Dim txtBox As TextBox
'Dim crtls As ControlCollection = Me.CreateControlCollection

'For Each ctrl In Me.Controls
' If ctrl.GetType Is GetType(WebControls.TextBox) Then
' DirectCast(ctrl, TextBox).Text = ""
' ElseIf ctrl.GetType Is GetType(WebControls.CheckBox) Then
' ElseIf ctrl.GetType Is GetType(WebControls.DropDownList)
Then
' Else
' 'do nothing
' End If
'Next

'Dim myEnumerator As IEnumerator = Me.Controls.GetEnumerator()
'While (myEnumerator.MoveNext())

' Dim myObject As Object = myEnumerator.Current
' If (myObject.GetType().Equals(GetType(TextBox))) Then
' DirectCast(myObject, TextBox).Text = ""
' End If

'End While

'For Each ctrl In Me.Controls
' If ctrl.GetType.ToString =
GetType(WebControls.TextBox).ToString Then
' DirectCast(ctrl, TextBox).Text = ""
' End If
'Next

'For Each ctrl In Me.CreateControlCollection()
' If ctrl.GetType.ToString =
GetType(WebControls.TextBox).ToString Then
' DirectCast(ctrl, TextBox).Text = ""
' End If
'Next

'For Each ctrl In crtls
' If ctrl.GetType.ToString =
GetType(WebControls.TextBox).ToString Then
' DirectCast(ctrl, TextBox).Text = ""
' End If
'Next

Me.txtDateNeeded.Text = ""
Me.txtName.Text = ""
Me.txtPhone.Text = ""
Me.txtProductName.Text = ""
Me.txtPurchaseQty.Text = ""
Me.txtRecieved.Text = ""

Me.ckbAuthList.Checked = False
Me.ckbOneTimePurchase.Checked = False
Me.ckbStocked.Checked = False

End Sub
 
B

BJ

The answer is below. The first example shows how to reset all
textboxes, checkboxes, or dropdownlist on container form1. If you have
a panel that is a sub container with in form1, all textboxes,
checkboxes, and dropdownlist within this control is not modified. I
can now isolate a section of my master page and reset all data entry
fields usnig a generic reset function passing the containers id.

Thank you again Cor.

Private Sub cmdReset_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles cmdReset.Click
Dim frm As Control = Me.FindControl("Form1") 'Form1 is the HTML
form Id
Dim pnl As Control = Me.FindControl("panel1")

For Each ctrl As Control In frm.Controls
If TypeOf ctrl Is TextBox Then
DirectCast(ctrl, TextBox).Text = ""
ElseIf TypeOf ctrl Is CheckBox Then
DirectCast(ctrl, CheckBox).Checked = False
ElseIf TypeOf ctrl Is DropDownList Then
DirectCast(ctrl, DropDownList).SelectedIndex = 0
End If
Next

For Each ctrl As Control In pnl.Controls
If TypeOf ctrl Is TextBox Then
DirectCast(ctrl, TextBox).Text = ""
ElseIf TypeOf ctrl Is CheckBox Then
DirectCast(ctrl, CheckBox).Checked = False
ElseIf TypeOf ctrl Is DropDownList Then
DirectCast(ctrl, DropDownList).SelectedIndex = 0
End If
Next
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top