How to read User Controls from a place holder
Any help on this would be great.
I have a huge online registration application. It is about 8 pages long with about 5-8 User Controls loaded on each one dynamically. I need to find a way to read through the place holder to find each user control. I was told this was the best way to pull in user information, but nothing seems to work.
Exampls:
Dim x As Control
For Each x In Me.Controls
If TypeOf x Is PlaceHolder Then
End If
Next
This does not find the place holder. Of course once I do find the place holder, how can I capture the control as a whole and send it to a class, where that will parse out the data and update the database.
I hope someone out there can help me.
Thanks!
[773 byte] By [
adharbert] at [2007-11-11 8:50:37]

# 1 Re: How to read User Controls from a place holder
I went round and round with this one myself. The problem is that there are controls inside other controls, which can themselves contain other controls, and the page itself is a control, so you need a recursive function- one that calls itself, so that you get down to the bottom level of each container control and the sub-controls contained within it.
Try something like this:
Private Sub WalkControls(ByVal c As Control)
' This recursive Sub walks through ALL controls on the page, and any contained controls,
' checking if the control is a placeholder:
For Each childc As Control In c.Controls
' If there are Controls within this Control, call the function on them:
If childc.Controls.Count > 0 Then
WalkControls(childc)
Else
' Otherwise, check for Textboxes and Buttons:
If TypeOf childc Is PlaceHolder Then
' It's a PlaceHolder, so ...
Dim pl As New PlaceHolder
pl = Page.FindControl(childc.UniqueID)
' Now you can access its properties,
' or its child controls, like: pl.Controls
End If
End If
Next
End Sub
The basic idea is to keep drilling down into every control you find, then into its sub-controls, etc. Then, when you find a PlaceHolder, you can't access it directly; you need to create a new PlaceHolder, and set it equal to the one you found. You can then access the one you created (which is pointing at the actual one on the page). It's a little indirect, but it's the only way that works. You can then read the values of the individual controls. I don't know about sending the whole thing to a class...
As far as whether this is the right approach-- I would try to find another way, but if you have to create the controls dynamically, you may not have a choice. If it's a simple situation where you have 3 or 4 different cases, I would create the controls for all 3 or 4 situations ahead of time, placing each one in a Panel control. You make the panel controls invisible, which hides ALL the controls. Then, when the user makes the choice that indicates which set of controls you need, you make that one panel visible and you're good to go. Another option would be multiple separate pages for each option, where you use Response.Redirect or Server.Transfer to send the user to the proper page depending on his choices.
I would think that creating that many controls dynamically would make things realy slow, and wouldn't scale well at all.
You might also look at the Wizard control - it's designed for cases where you're taking the user through a series of pages, getting information from him/her; or even the MultiView control-- depending on which version of Visual Studio you're using.
HTH,
-Andrew