473,473 Members | 1,576 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

How do you loop through a web forms control collection?

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
3 13444
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
BJ
Thank you very much!!!!!

Nov 21 '05 #3
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: trenchmouth | last post by:
Does anybody know how I can loop through all the properties in a class I have created? Can I use Me to refer to the properties? I'm using VB6. Many thanks.
6
by: ALthePal | last post by:
Hi, I'm not sure if we are able to or even how to loop through the web forms in a VB.NET project during design time. In MSAccess we are able to go through the database -> forms collection and...
3
by: brett | last post by:
Using DOM in IE, how can I loop through FORMs and access FORM elements in a specific form? For example, www.hotmail.com has about 13 forms. I believe the one displayed is dependent on the URL. If...
4
by: Soryt | last post by:
Hi everyone ! 1) An example from help: "Each Form object has a Controls collection, which contains all controls on the form. You can refer to a control on a form either by implicitly or...
2
by: Greg Strong | last post by:
Hello All, Is there a simpler way to count text boxes with data in them when the 4 text boxes are in the header of a form? I've written the code below and it works. ,----- | Private Sub...
1
by: Mehr H | last post by:
I've been trying to figure out how i can embed a Windows.Forms.ProgressBar in my webform (aspx) file. I have tried putting a Windows.Forms.ProgressBar as public on a regular winform designer form...
4
by: Ryan Ternier | last post by:
Thanks for the previous help guys! I got my list box issue working, but now i'm trying to loop through all the items in my page. I want to find each listbox, once I do i strip the ID down to...
3
by: Kezza | last post by:
Hi There.. I have dynamically created some textbox and checkbox controls by adding them to a panel. Now I would like to get the values out. I have created a for each loop that I can see the...
8
by: dominique | last post by:
Hi, Is it possible (in vb.net with WinForms) to loop throw controls inside a container (form or panel) sorting the controls on a property (.tabindex for example) ? My problem : on several...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.