SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

My question about the placeholder, basically I have to perfrom a check in my custom user control for some controls, but does controls are loaded from a page in a placeholder.

And my user is located on the master page of course.

So in a user control I wrote something like this:

        protected void Page_Load(object sender, EventArgs e)
            this.Page.LoadComplete += new EventHandler(Page_LoadComplete);

        void Page_LoadComplete(object sender, EventArgs e)
             //looping thru all the controls in the page

So basically i call a method on page load complete, BUT I don't find the control which I'm looking for, so my question WHEN do the place holders are beeing loaded with the contenct from pages?

How can I get access to controls of those pages from master page, when are they available?

share|improve this question
up vote 0 down vote accepted

There is probably one of two things going on here:

  1. Your Page Load Event is getting called before the Master Page event.
  2. Your control is in a naming container that's buried in your control tree.

To solve item 2, you could create a recursive method that walks the control tree. But this is pretty expensive, especially if the control is not found.

private static Control FindControlRecursive(Control Root, string Id) 
    if (Root.ID == Id) 
    return Root; 

    foreach (Control Ctl in Root.Controls) 
        Control FoundCtl = FindControlRecursive(Ctl, Id); 
        if (FoundCtl != null) 
            return FoundCtl; 
    return null; 

If you know how deep the control is, you can do .FindControl().FindControl.FindControl() etc. But this is fragile.

I recently saw someone (Popa D. Stelian) use a linked list for this. I thought this was an interesting approach:

private static Control FindControlIterative(Control root, string id) 
    Control ctl = root; 
    LinkedList ctls = new LinkedList(); 

    while (ctl != null) 
        if (ctl.ID == id) 
            return ctl; 
        foreach (Control child in ctl.Controls) 
            if (child.ID == id) 
                return child; 
            if (child.HasControls()) 
        ctl = ctls.First.Value; 
    return null; 

Regards, Mike Sharp

share|improve this answer
yeah i'm serching recursively, but it doesn't find, so yeah i'm thinking that the method is called before the placeholder from the page is loaded :(. So that is why i want to verify .... – Shkipper Oct 5 '12 at 5:24

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.