473,398 Members | 2,393 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

Object with all of form's controls to loop through ASP.NET

Is there a way I can loop through a form object in ASP.NET codebehind like
in VB.NET and get all of the controls?
Nov 18 '05 #1
4 1795
Use the Controls collection.

dim ctrl as Control
for each ctrl in Page.Controls
.....
next

"David A. Beck" <da****@beckb.com> wrote in message
news:uY**************@TK2MSFTNGP10.phx.gbl...
Is there a way I can loop through a form object in ASP.NET codebehind like
in VB.NET and get all of the controls?

Nov 18 '05 #2
You can loop through the controls collection.
However it gets complicated if you have controls nested within other
container controls.
Here's some code Kevin Spencer posted a while back that provides a good
demonstration using recursion.

Private Function FindChildControl(ByVal objSearchControl As
System.Web.UI.Control, _
ByVal strControlID As String) As Object

Dim objChildControl As System.Web.UI.Control
Dim objControl As System.Web.UI.Control

If objSearchControl.Controls.Count = 0 Return Nothing

For Each objChildControl in objSearchControl.Controls
objControl = FindChildControl(objChildControl, strControlID)
If Not IsNothing(objControl) Return objControl
Next

Return Nothing
End Function

--
I hope this helps,
Steve C. Orr, MCSD, MVP
http://Steve.Orr.net
Hire top-notch developers at http://www.able-consulting.com

"David A. Beck" <da****@beckb.com> wrote in message
news:uY**************@TK2MSFTNGP10.phx.gbl...
Is there a way I can loop through a form object in ASP.NET codebehind like
in VB.NET and get all of the controls?

Nov 18 '05 #3
Steve:

Thanks. This code searches for a given control name in a control that itself
has controls. I have not been sucessful yet in adapting it to loop through
the Page.Controls object for my "Form1" to get all the controls.

Dab
"Steve C. Orr [MVP, MCSD]" <St***@Orr.net> wrote in message
news:e0**************@TK2MSFTNGP10.phx.gbl...
You can loop through the controls collection.
However it gets complicated if you have controls nested within other
container controls.
Here's some code Kevin Spencer posted a while back that provides a good
demonstration using recursion.

Private Function FindChildControl(ByVal objSearchControl As
System.Web.UI.Control, _
ByVal strControlID As String) As Object

Dim objChildControl As System.Web.UI.Control
Dim objControl As System.Web.UI.Control

If objSearchControl.Controls.Count = 0 Return Nothing

For Each objChildControl in objSearchControl.Controls
objControl = FindChildControl(objChildControl, strControlID)
If Not IsNothing(objControl) Return objControl
Next

Return Nothing
End Function

--
I hope this helps,
Steve C. Orr, MCSD, MVP
http://Steve.Orr.net
Hire top-notch developers at http://www.able-consulting.com

"David A. Beck" <da****@beckb.com> wrote in message
news:uY**************@TK2MSFTNGP10.phx.gbl...
Is there a way I can loop through a form object in ASP.NET codebehind like in VB.NET and get all of the controls?


Nov 18 '05 #4
Check out this article,
http://www.extremeexperts.com/Net/Ar...hControls.aspx

--
Saravana
Microsoft MVP - ASP.NET
www.extremeexperts.com

"David A. Beck" <da****@beckb.com> wrote in message
news:ex**************@TK2MSFTNGP12.phx.gbl...
Steve:

Thanks. This code searches for a given control name in a control that itself has controls. I have not been sucessful yet in adapting it to loop through the Page.Controls object for my "Form1" to get all the controls.

Dab
"Steve C. Orr [MVP, MCSD]" <St***@Orr.net> wrote in message
news:e0**************@TK2MSFTNGP10.phx.gbl...
You can loop through the controls collection.
However it gets complicated if you have controls nested within other
container controls.
Here's some code Kevin Spencer posted a while back that provides a good
demonstration using recursion.

Private Function FindChildControl(ByVal objSearchControl As
System.Web.UI.Control, _
ByVal strControlID As String) As Object

Dim objChildControl As System.Web.UI.Control
Dim objControl As System.Web.UI.Control

If objSearchControl.Controls.Count = 0 Return Nothing

For Each objChildControl in objSearchControl.Controls
objControl = FindChildControl(objChildControl, strControlID)
If Not IsNothing(objControl) Return objControl
Next

Return Nothing
End Function

--
I hope this helps,
Steve C. Orr, MCSD, MVP
http://Steve.Orr.net
Hire top-notch developers at http://www.able-consulting.com

"David A. Beck" <da****@beckb.com> wrote in message
news:uY**************@TK2MSFTNGP10.phx.gbl...
Is there a way I can loop through a form object in ASP.NET codebehind like in VB.NET and get all of the controls?



Nov 18 '05 #5

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

Similar topics

8
by: deko | last post by:
I'm hoping someone can sanity check my understanding of the Object Model for Forms/Controls. I'm having trouble drilling down into Control properties. First, I have a record set with the...
4
by: Ryan Liu | last post by:
How to loop though controls on the form? I set each control's TabStop and TabIndex, but at run time, I press Tab key, it jump though first control to last control and does not go back to the...
3
by: Bill | last post by:
I have a seating chart web form that has over 50 entry field controls (tables/booths) where I use a DropDownList box to select a single company name from a single large list of organizations (200...
1
by: Angus Lepper | last post by:
I'm writing a stock ticker for a stock market simulation, and can load the data into the xmlreader in the first place, but can't figure out how to refresh/update the data in it. Any ideas? Code:...
5
by: Varangian | last post by:
ImageButton ship; ship = new ImageButton; for (int i=0; i<5; i++) { ship.ImageUrl = pathofImage; ship.ID = "ShipNo" + i.ToString(); ship.Click += new...
1
by: Miguel Ribeiro | last post by:
Hi, The crux of my problem is that I want to destroy an object without using the reference (by using a collection etc.) and have the reference basically show 'nothing' I have a MDI form with...
6
by: Kevin | last post by:
I come up with these questions during the day, do some research, and then look for experienced users' input. 1. In Access, we already know it's pretty much an automatic save if you enter data. ...
35
by: Jamey Shuemaker | last post by:
I've seen multiple threads (several in the last 6 months or so) on this topic, but I wanted to clarify my practices. I understand the need for cleaning object variables by setting them to Nothing...
3
by: Shawn | last post by:
I'm trying to go through a large number of logically named textboxes using a loop and cannot figure out how short of explicitly referencing every single one.
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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
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...
0
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
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...

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.