CarnVanBeck
CarnVanBeck

Reputation: 194

Masterpage controllist only returns some controls

I have a little problem with my ASP.Net Page.

I'm trying to get all controls from my masterpage within the masterpage code.

I did this with the Child page using this code

foreach (Control ctrl in ContentPlaceHolder1.Controls)
{
    if (ctrl.GetType() == typeof(Label))
    {
        //Do Stuff...
    }
}

but when i try to get the other controls with

foreach (Control ctrl in this.form1.Controls)

it doesn't work completly.

I get 3 of my Labels but can't access the rest.

Here is a part of my ASP Code

<div style="float: right;">
    <ul class="main-language language-level main-language-level-0" >
        <li><a href="">
                <asp:Label runat="server" Text="Deutschland" ID="lbl_Language"/>
            </a>
            <ul class="language-level main-language-level-1">
                <li>
                    <a href="?Lng=EN">
                        <asp:Label ID="lbl_English" runat="server" Text="United Kingdom" ForeColor="#0D679C" Font-Names="Century Gothic" />
                    </a> 
                    <span>English</span>
                </li>
                <li>
                    <a href="?Lng=DE">
                        <asp:Label ID="lbl_German" runat="server" Text="Deutschland" ForeColor="#0D679C" Font-Names="Century Gothic" />
                    </a>
                    <span>Deutsch</span>
                </li>
            </ul>
            <img class="menu-image" src="Images/arrow_languageselection.png" />
        </li>
    </ul>
    <br />
    <a href="CustomerServiceLogin.aspx?Lgn=22TR" runat="server" id="LogLink">
        <asp:Label CssClass="ButtonOrange" runat="server" ID="lbl_Login" />
        <asp:Label CssClass="ButtonOrange" runat="server" ID="lbl_Logoff" Visible="false" />
    </a>
</div>

The only labels i can find are lbl_Language,lbl_English and lbl_German

Has somebody a solution for this?

Sincerely

CarnVanBeck

Upvotes: 0

Views: 88

Answers (1)

FastGeek
FastGeek

Reputation: 411

If the labels that you cannot access are nested inside another control then they will not be returned when you iterate over form1.controls. You will need a recursive solution to return all of your controls.

foreach (Control ctrl in this.form1.Controls)
{
    if (ctrl.Controls.Count > 0)
    {
        // do recursive call
    }
}

Upvotes: 1

Related Questions