More in this section

Forums / Developing with Sitefinity / Weirdness in ContentPlaceholder childcontrols

Weirdness in ContentPlaceholder childcontrols

3 posts, 0 answered
  1. Mike
    Mike avatar
    208 posts
    Registered:
    10 Dec 2007
    23 Feb 2009
    Link to this post
    I have a page with a content placeholder with an ID of MainLeftContent.

    I edit the page, and place (in order) a generic content control, an image control and a rad upload control.  The GC and Image controls are "out of the box" controls, and the rad upload control is basically an OOTB control that I've preconfigured with a number of properties, and added a single asp:literal with an ID of HubImageFilename.

    When I render the page, I'm trying to set the Image control to a particular image, and the HubImageFilename literal to a string value.  In order to do this, I have to get a reference to the controls.  FindControl doesn't work, because I'm setting this from another control on the page.  So I use a recursive version of FindControl, like:

    private Control FindControlRecursive(Control root, string id)  
    {  
        if (root.ID == id)  
        {  
            return root;  
        }  
     
        foreach (Control c in root.Controls)  
        {  
            Control t = FindControlRecursive(c, id);  
            if (t != null)  
            {  
                return t;  
            }  
        }  
     
        return null;  
    }  
     

    I call this like:

    System.Web.UI.WebControls.Image imgControl =   
        FindControlRecursive(Page, "HubImage1"as System.Web.UI.WebControls.Image;  
    Literal imageFilename = FindControlRecursive(Page, "HubImageFilename"as Literal;  
     

    The problem is very strange.  imgControl is ALWAYS null, unless I add an extra GenericContent control in the placeholder, after all the controls.  If I do that, then FindControlRecursive will find the Image control.  If I don't have this extra GC control, then FindControlRecursive won't find the image.

    But it NEVER finds the literal.  It's like I can get it to find one more control, but not the whole control tree.

    I can't imagine why adding a control that is completely unrelated to another one would suddenly make the other one findable.  Any clues out there?

    Thanks,
    Mike
  2. Mike
    Mike avatar
    208 posts
    Registered:
    10 Dec 2007
    23 Feb 2009
    Link to this post
    I forgot to mention that the problem with FindControlRecursive is taking place in the Page_Load method.

    But on another page I'm doing the exact same thing in a button click handler, and everything works just fine--I can find both controls.

    Mike
  3. Mike
    Mike avatar
    208 posts
    Registered:
    10 Dec 2007
    23 Feb 2009
    Link to this post
    Oh heck, never mind.  My last post was the clue I needed. 

    I should be looking for these controls in the OnPreRender event instead of page_load.

    Doh. (smacking myself on the forehead)


    Mike
Register for webinar
3 posts, 0 answered