Solved

FindControl - In "Parent" page

Posted on 2008-06-19
4
2,424 Views
Last Modified: 2008-07-12
I have a usercontrol being bound to a placeholder - there is a button within the usercontrol that, when clicked, would like to find a control (a placeholder) and load/bind another usercontrol - so need to know how to find a control, on the parent, from within the child usercontrol...
0
Comment
Question by:tbaseflug
  • 2
4 Comments
 
LVL 16

Expert Comment

by:ororiole
ID: 21827804
You can use:
      PlaceHolder ph =     (PlaceHolder)  Page.FindControl("PlaceHolderID");
0
 

Author Comment

by:tbaseflug
ID: 21845868
Actually, this is more of what I am looking for - which works, but looks pretty ugly

 Parent.Parent.Parent.Parent.Parent.FindControl("UpdatePanel2") as UpdatePanel;

Open in new window

0
 
LVL 16

Accepted Solution

by:
ororiole earned 500 total points
ID: 21849283
Well you didnt mention several Parent.Parent.Parent levels. Why would you want to do that, when you can start at Page and recurse thru the control collections? Plus you are hard coded, so if you add or remove Parents, you have to change your code. This way you can start at Page (or lower if you wish) and it will find the control no matter what the level.
       private Control SearchControl(Control container, string id)

        {

            Control control = container.FindControl(id);

            if (control == null)

            {

                foreach (Control c in container.Controls)

                {

                    if (c.HasControls())

                    {

                        control = SearchControl(c, id);

                        if (control != null) break;

                    }

                }

            }

            return control;

        }

Open in new window

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…
As a trusted technology advisor to your customers you are likely getting the daily question of, ‘should I put this in the cloud?’ As customer demands for cloud services increases, companies will see a shift from traditional buying patterns to new…

896 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

14 Experts available now in Live!

Get 1:1 Help Now