Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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

1 Answer 1

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()) 
                ctls.AddLast(child); 
        } 
        ctl = ctls.First.Value; 
        ctls.Remove(ctl); 
    } 
    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

 
discard

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.