473,503 Members | 1,749 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Loop thru controls on a html form

I am new to .net programming and I want to know if I can loop thru a
specified number of controls on an html form.

In Access, the code looks like this:
For i = 1 to 10
Me("txtAlternate" & i) = ""
Next i

Is there an equivalent code in asp.net?

TIA
Mike
Nov 21 '05 #1
2 1505
"Michael Beck" <Sp*************@pacbell.net> wrote in message news:%2****************@TK2MSFTNGP12.phx.gbl...
I am new to .net programming and I want to know if I can loop thru a
specified number of controls on an html form.


' Someplace within the WebForm1 class, most likely in Page_Load.
'
Dim c As System.Web.UI.Control
For Each c In Me.Controls
' Do something with c
Next c

This will go through all of the top level controls on the web form,
but not nested controls (each c will have it's own Controls collection
containing it's children, so you can choose to descend recursively if
you so choose).

Public Sub DescendThroughControls( ByVal controlSet As System.Web.UI.ControlCollection )
Dim c As System.Web.UI.Control

For Each c In controlSet
' Do something with c.
If ( c.Controls.Count > 0 ) Then
' This step will do something with c's child controls.
DescendThroughControls( c.Controls )
End If
Next c

End Sub
' . . .
' called with the following statement in Page_Load
' . . .
Me.DescendThroughControls( Me.Controls)

Finally, if you're looking for a Control on the Page with a specific ID then you
can use Me.FindControl( name ) to return an instance of that Control to do
something with. Again, it only works within your immediate ControlCollection
so you may need to descend into the child control collections.
Derek Harmon
Nov 21 '05 #2
Michael,

The format from Derek did never works for me direct on a webpage, however
because his convincing way of writting I am not sure, however when it does
not go you can try this on.

\\\\
Dim frm As Control = Me.FindControl("Form1")
Dim ctl As Control
For Each ctl In frm.Controls
Dim tb As TextBox
If TypeOf ctl Is TextBox Then
tb = DirectCast(ctl, TextBox)
tb.Text = String.Empty
End If
Next
///

I hope this helps?

Cor
Nov 21 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
6239
by: Jeremy Langworthy | last post by:
Hi I have a dynamicly generated form (well the elements are at least) that looks something like this: while( not end of returned records): <input name="plan_id" type="checkbox" id=""...
3
2563
by: Joe | last post by:
I'm wondering how to loop through controls in VB.NET. I have the code from VB6 ok, but I can't figure out how to do it correctly in .NET. This is an example from my VB6 code that loops through...
3
5179
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
3699
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...
4
2171
by: VB Programmer | last post by:
How can I loop thru all textboxes on a form? I want to remove any single quotes in the data entry. I was thinking something like this: For Each c As Control In Me.Controls Dim t As TextBox =...
3
2226
by: Neil Zanella | last post by:
Hello, I have a bunch of HTML input controls which I need to generate from within a for loop. I would like them to have id attributes set to values foo1, foo2, foo3, foo4, etc... ...
3
1311
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
2513
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...
2
5049
by: ckerns | last post by:
I have a page with a bunch of drop down boxes. They are named: MonEquipAMWk1 MonEquipPMWk1 thru FriEquipAMWk1 FriEquipPMWk1
0
7202
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7086
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...
1
6991
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
7460
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
5578
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,...
1
5014
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3154
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1512
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
380
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.