Programmaticly enumerate all controls and properties on an ASP.NET page

J

J

I'm trying to make a list of certain properties of all the controls on an
ASP.NET page. Does anyone know of an easy way to do this? I've tried to
do it a couple of different ways but I'm not getting the hang of it I'm
afraid. Right now I'm parsing text file but that's a pain, not very
elegant, and I'm not sure it'll give me all the info I need. Right now
I'm using VB.Net as the back end but I'll take C#.Net code in a pinch. :)

Tanx in advance,

J
 
P

Peter Rilling

Could you, for instance, use the Page.Controls collection and recursively
walk through each control?
 
J

J

Hi Peter,

I've tried something similar but it seems more complicated than that. If
I look through the list of objects on the property list on the right hand
side I see six objects (including the web form). When I use
page.controls.count I get 3. Those objects are 'ctl0', 'form1', and
'ctl1' respectively. It looks like I can access the controls on the form
through 'form1'. Page.Controls(1).Controls.Count yields the correct
number. I suspect that means that I first have to iterate through the
page controls and find out which one is the form. Is that always the way
it works? If not then this could get messy really quickly. :(

J
 
J

J

SWEET!

Here's what I did.

Private Sub CtrlList(ByVal ctrlCol As ControlCollection)
Dim ctrl As Control
For Each ctrl In ctrlCol
If ctrl.HasControls Then
CtrlList(ctrl.Controls)
Else
System.Diagnostics.Debug.WriteLine(ctrl.ClientID)
End If
Next
End Sub

I love recursion. ;)

J
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top