Loop through controls with MaterPage ContentHolder

G

Guest

I changed my aspx page to use a master page. The problem is that I can no
longer loop through the controls on the content page. My question is how do
you loop through the controls on the master page and how do you loop through
the controls on the content page when the content is inside the ContentHolder
control?

Below is an example of the control loop before using a master page.

Sub FindControlsInWebForm()
Dim strValue As String = ""

'Loop through controls
For Each ctlPage As Control In Page.Controls
If TypeOf ctlPage Is HtmlForm Then
For Each ctlChild As Control In ctlPage.Controls
If TypeOf ctlChild Is Label Then strValue &= "Page.Label.ID = " &
ctlChild.ID & ".ID<br>"

'Panel
If TypeOf ctlChild Is Panel Then
strValue = "Panel.ID = " & ctlChild.ID & ".ID<br>"

For Each ctlGrndChild As Control In ctlChild.Controls
If TypeOf ctlGrndChild Is TextBox Then strValue &=
"Panel.TextBox.ID = " & ctlGrndChild.ID & ".ID<br>"
Next ctlGrndChild
End If
Next ctlChild
End If
Next ctlPage
End Sub
 
K

Kevin Yu [MSFT]

Hi sck10,

We have reviewed this issue and are currently researching on it. We will
update you ASAP. Thanks for your patience!

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."
 
S

Scott Allen

Hi sck10:

The hierarchy of controls with MasterPages looks like this:

Page
- MasterPage
- HtmlForm
- ContentPlaceHolder
- The TextBoxes, etc.

So your logic could look something like the following.

For Each ctlMaster As Control In Page.Controls
If TypeOf ctlMaster Is MasterPage Then
For Each ctlForm As Control In ctlMaster.Controls
If TypeOf ctlForm Is HtmlForm Then
For Each ctlContent As Control In ctlForm.Controls
If TypeOf ctlContent Is ContentPlaceHolder Then
For Each ctlChild As Control In ctlContent.Controls
' Do work...
Next
End If
Next
End If
Next
End If
Next
 
S

Steven Cheng[MSFT]

Thanks for Scott's informative suggestions.

Hi sck10,

Scott has provide the detailed description on the MasterPage customized
page's control hierarchy and here is another blog article which has also
mentioned this hehavior:

http://dotnetjunkies.com/WebLog/jpalermo/archive/2004/09/01/23919.aspx

Hope also helps. Thanks.

Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 
S

Steven Cheng[MSFT]

Hi Sck10,

Have you had a chance to check the former messages or have you got any
further ideas on this problem. If there are any thing else we can help,
please feel free to post here. Thanks.

Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 

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

Top