Solved

FindControl - In "Parent" page

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

679 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