Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Shaun E on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Iterating through controls - how do I display child controls? 1

Status
Not open for further replies.

faithful1

Programmer
Sep 27, 2002
49
US
Hello...
I am using the code below to display all controls on my page, however the result is only the parent controls(as follows):

System.Web.UI.ResourceBasedLiteralControl
System.Web.UI.HtmlControls.HtmlForm
System.Web.UI.LiteralControl

I do have textboxes on my page. I believe the textbox is a child of the HTML form control. Does anyone know how I can get to the child level of controls?

Code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim myControl As Control
Dim controltype As String
For Each myControl In Page.Controls
controltype = myControl.GetType.ToString
Response.Write(controltype)
Next
Response.Write(controltype)
Response.Write(&quot;<br>&quot;)
Next
End Sub
 
Hey faithful,

You've got the right code, you just need to tweak it a bit.

First, put it in its own sub/function, Like

Public Sub ShowControls(control as Control)

End Sub


Its important that you put a parameter of type control in the definition. Now, the only other change you need to make is:

For each myControl in Control.Controls


and add this line before the Next:

ShowControl(myControl)

When you call this, pass in the page
i.e. Public Sub ShowControls(Me)

Now here's the explanation:
You pass in the page control in, which has controls on it. For each of those controls, it will display the info you want, and will then take each of those controls and recursivly call the sub. The process starts over for each of the controls (i.e. a page has 2 panels, a panel has 2 buttons, etc.)

hth

D'Arcy
 
Thanks for the help!! As soon as I get a chance to try it, I will let you know how it goes!!!!
Thanks again....
[upsidedown]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top