.NETGURU
Looping thru controls on a page
Messages   Related Types
This message was discovered on ASPFriends.com 'aspngcontrolsvb' list.
Responses highlighted in red are from those people who are likely to be able to contribute good, authoratitive information to this discussion. They include Microsoft employees, MVP's and others who IMHO contribute well to these kinds of discussions.

Frans, Stephen
I'd like to loop thru all controls on a web form, but cannot find a
collection that I can enumerate thru (kind of like the old classic ASP "for
each x in Request.Forms" functionality).

Moreover, it would be nice to know the object type (TypeOf kind of
functionality to determine "user control" versus "web control" versus "html
control"). Once I knew the type of object, I could cast the object and
issue method calls on that object appropriate for that object.

Could someone point me in the right direction?

TIA,

Steve Frans
Impact Innovations Group
Reply to this message...
 
    
Brian Bilbro (VIP)
Page.Controls is a collection of all of the controls.

There are some GetType functions available and even reflection (probably =
simipler than that though) to figure out the object type.

HTHs,
Brian'

----- Original Message -----
From: "Frans, Stephen" <Click here to reveal e-mail address>
Sent: 11/6/2001 8:38:00 PM

I'd like to loop thru all controls on a web form, but cannot find a
collection that I can enumerate thru (kind of like the old classic ASP =
"for
each x in Request.Forms" functionality). =20
=20
Moreover, it would be nice to know the object type (TypeOf kind of
functionality to determine "user control" versus "web control" versus =
"html
control"). Once I knew the type of object, I could cast the object and
issue method calls on that object appropriate for that object.
=20
Could someone point me in the right direction?
=20
TIA,
=20
Steve Frans
Impact Innovations Group
=20
=20

Reply to this message...
 
    
Daryl Pietrocarlo
This isn't pretty code (and it is C#). A lot of copy and paste as I
needed it, but here is a function that will "rip" through any heiarchy
of controls if you pass the Enumerator using some recursion to pound
through the levels.

        void ripper(IEnumerator indexEnumerator )
        {
            while (indexEnumerator.MoveNext())
            {
                Debug.WriteLine("Control:" +
indexEnumerator.Current.ToString());
                Debug.WriteLine("Type:" +
indexEnumerator.Current.GetType().Name);

                if (
indexEnumerator.Current.GetType().Name =3D=3D "Label")=20
                {
                    Label l =3D
(Label)indexEnumerator.Current;
                    Debug.WriteLine("----------->
Label Text:" + l.Text);
                }
            =09
                if (
indexEnumerator.Current.GetType().Name =3D=3D "TextBox")=20
                {
                    TextBox l =3D
(TextBox)indexEnumerator.Current;
                    Debug.WriteLine("----------->
TextBox Text:" + l.Text);
                }

                if (
indexEnumerator.Current.GetType().Name =3D=3D "DataBoundLiteralControl") =

                {
                    DataBoundLiteralControl l =3D
(DataBoundLiteralControl)indexEnumerator.Current;
                    Debug.WriteLine("----------->
DataBoundLiteralControl Text:" + l.Text);
                }

                if (
indexEnumerator.Current.GetType().Name =3D=3D "LiteralControl" ||
indexEnumerator.Current.GetType().Name=3D=3D =
"ResourceBasedLiteralControl")=20
                {
                    LiteralControl l =3D
(LiteralControl)indexEnumerator.Current;
                    Debug.WriteLine("----------->
LiteralControl Text:" + l.Text);
                =09
                }                =09
            =09
                Debug.WriteLine("Assembly:" +
indexEnumerator.Current.GetType().Assembly);
                Control c =3D
(Control)indexEnumerator.Current;

                if (c.Controls.GetEnumerator() !=3D null)
                {
=09
ripper(c.Controls.GetEnumerator() );
                }
            }
        }

Call it like this:

    ripper(Page.Controls.GetEnumerator());

Remember if you have another control like a Datagrid, you can pass
MyDataGrid.Controls.GetEnumerator() just to go through the datagrid
etc.

Daryl Pietrocarlo
Vibrant Solutions
Click here to reveal e-mail address

-----Original Message-----
From: Brian Bilbro [mailto:Click here to reveal e-mail address]=20
Sent: Wednesday, November 07, 2001 11:52 PM
To: aspngcontrolsvb
Subject: [aspngcontrolsvb] RE: Looping thru controls on a page

Page.Controls is a collection of all of the controls.

There are some GetType functions available and even reflection (probably
simipler than that though) to figure out the object type.

HTHs,
Brian'

----- Original Message -----
From: "Frans, Stephen" <Click here to reveal e-mail address>
Sent: 11/6/2001 8:38:00 PM

I'd like to loop thru all controls on a web form, but cannot find a
collection that I can enumerate thru (kind of like the old classic ASP
"for each x in Request.Forms" functionality). =20
=20
Moreover, it would be nice to know the object type (TypeOf kind of
functionality to determine "user control" versus "web control" versus
"html control"). Once I knew the type of object, I could cast the
object and issue method calls on that object appropriate for that
object.
=20
Could someone point me in the right direction?
=20
TIA,
=20
Steve Frans
Impact Innovations Group
=20
=20

| [aspngcontrolsvb] member Click here to reveal e-mail address =3D YOUR ID=20
| http://www.asplists.com/asplists/aspngcontrolsvb.asp =3D JOIN/QUIT=20
| http://www.asplists.com/search =3D SEARCH Archives

Reply to this message...
 
    
Paul D. Murphy
FWIW, if you turn on tracing ASP.NET will lay out the control hierarchy
at the foot of every page.

Paul

[Original message clipped]

Reply to this message...
 
    
Rob Waggoner (VIP)
And a VB Example:

Public Class frmMain
Inherits System.Windows.Forms.Form
...

Dim ctlToWork As Control ' declare control type

...

' loop through control collection searching for panel types
For Each ctlToWork In Controls
Select Case ctlToWork.GetType.FullName
Case "System.Windows.Forms.Panel"
ctlToWork.Visible = False
End Select
Next

Rob Waggoner
Master Applications Craftsman
WAGGS
Web based Advanced Graphics and Graphing Solutions
http://www.waggs.net
"Applying Old world craftsmanship to New world technologies"

Reply to this message...
 
 
System.Collections.IEnumerator
System.Diagnostics.Debug
System.Web.UI.DataBoundLiteralControl
System.Web.UI.LiteralControl
System.Web.UI.MobileControls.TextBox
System.Web.UI.Page
System.Web.UI.WebControls.TextBox
System.Windows.Forms.Form
System.Windows.Forms.Panel
System.Windows.Forms.TextBox




ExamGuru IT Solutions - .Net Guru is owned and operated by ExamGuru, Inc., the man behind .Net Guru. If you're in the market for bespoke software or software consultancy, why not get him and his highly trained team to help? - www.examguru.net/ITCertification
Ad


Need Dot Net Interview Questions?
Ask ExamGuru, Inc. for advice and help on Passing .Net Interviews
.Net Projects
Best-of-breed application framework for .NET projects, developed by ExamGuru, Inc. and ExamGuru IT
Free .net Help
Commission ExamGuru, Inc. and his team for your next bespoke software project
FogBUGZ
The only bug tracking system carefully crafted with one goal in mind: helping teams create great software.
Awesome Tools
If you don't know about these, you're missing out... IT Certification Questions
IT Interview Questions
Free Oracle 10g Training
MCSE Boortcamp
Cisco Study Guides
Cheap Study Guides
Exact Questions
Dot Net Interview Questions
Oracle OCP
Cheap Travel
Designer Perfumes - Wholesale Prices
Free Programming Tutorials
 
ExamGuru IT Solutions - .Net Guru is owned and operated by ExamGuru, Inc., the man behind .Net Guru. If you're in the market for bespoke software or software consultancy, why not get him and his highly trained team to help? - www.examguru.net/ITCertification
 Copyright © ExamGuru, Inc. 2001-2006
Contact Us - Terms of Use - Privacy Policy - www.dot-net-guru.com - www.examguru.net - www.oraclesource.net - www.itinterviews.net - www.examguru.net/ITCertification