By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,061 Members | 1,539 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,061 IT Pros & Developers. It's quick & easy.

How do you loop through a web forms control collection?

P: n/a
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

Nov 21 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
BJ,

Looping through the controls on a webform need a simple trick

Have a look how we do it.

http://www.windowsformsdatagridhelp....8-ec5d24c916a6

I hope this helps,

Cor

"BJ" <ju**********@yahoo.com> schreef in bericht
news:11**********************@g49g2000cwa.googlegr oups.com...
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

Nov 21 '05 #2

P: n/a
BJ
Thank you very much!!!!!

Nov 21 '05 #3

P: n/a
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

Nov 21 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.