FindControl for Dynamic created Page controls

Posted on 2011-05-13
Last Modified: 2012-05-11
Hi All,

I am receiving the good old 'Object reference not set to an instance of an object.' error at  the moment when trying to use the FindControl within my code behind.

Here is what my Control Tree looks like when Trace is enabled:


The PlaceHolder control PlaceHolderQuestionType has been created manually and is there at development time.

ctl00$ContentPlaceHolder1$PlaceHolderQuestions and ctl00$ContentPlaceHolder1$phControlContainer are created dynamically within my code behind.

When trying to use the FindControl function like within my code attached I get 'Object reference not set to an instance of an object' which is referring to phControlContainer.

Can anyone see what I could be doing wrong?



PlaceHolder PlaceHolderQuestions = (PlaceHolder)PlaceHolderQuestionType.FindControl("PlaceHolderQuestions");

PlaceHolder phControlContainer = (PlaceHolder)PlaceHolderQuestions.FindControl("phControlContainer");

Open in new window

Question by:rito1
    LVL 21

    Accepted Solution

    You could try using a FindControlRecursive function if you want.  You can call it something like this as an example

    PlaceHolder PlaceHolderQuestions = (PlaceHolder)FindControlRecursive(PlaceHolderQuestionType,"PlaceHolderQuestions");
    public 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;

    Open in new window

    LVL 1

    Author Comment

    Thanks Alfred1, I am just giving it ago.
    LVL 13

    Expert Comment

    Have you tried .FindControl("ContentPlaceHolder1_PlaceHolderQuestions" ... ?

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Better Security Awareness With Threat Intelligence

    See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

    This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
    Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
    To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…
    This video discusses moving either the default database or any database to a new volume.

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    20 Experts available now in Live!

    Get 1:1 Help Now