Solved

FindControl - In "Parent" page

Posted on 2008-06-19
4
2,421 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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

759 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

24 Experts available now in Live!

Get 1:1 Help Now