Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

FindControl - In "Parent" page

Posted on 2008-06-19
4
Medium Priority
?
2,471 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 2000 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .

971 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