• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1852
  • Last Modified:

How do I access a parent's property from a nested child user control?

Hi all.  I'm probably missing something obvious.  I have a UserControl inside a PlaceHolder which is inside a Panel.  That panel is a child control of the parent UserControl that contains the property I need to access. Using the code below, I get a casting error, obviously because my upload control's direct parent is the placeholder control. What is the best way for me to access the property I need

Should I just abandon trying to seek out the property in the parent above and just pass the values I need in custom event args? My parent control has an "upload files" button that exposes the visibility of the upload control's panel. I could probably pass what I need through event args, but I would still need to get a reference to that parent control though just so I could listen for the event. I'm just looking for the simplest solution.  

Just to recap:  My control heirarchy is:  Parent UserControl -> Panel -> PlaceHolder -> Upload UserControl and I need to get a reference to a property in the Parent contriol from the Upload Control.  Thanks for any help.
FileManager40 ucFileManager40 = (FileManager40)this.Parent;
			Category _currentFolder = ucFileManager40._CurrentFolder;

Open in new window

0
svcoder
Asked:
svcoder
2 Solutions
 
Alpesh PatelCommented:
here is the scenario for your requirement
i am using asp.net langugage so you can get logic and build your own

Declaration:


dim l_instanceUcParent as UserControls_UcParent            ' get one instatnce of your parent usercontrol UserControls_UCParent may be change according to your Parent usercontrol name

Dim l_instancePanel As  Panel

dim l_instanceUcUploadChild as UserControls_UcUploadChild          '' get one instatnce of your UploadChild usercontrol UserControls_UCUploadChild may be change according to your UploadChild usercontrol name


Code:

l_instanceUcParent = me.FindControl("HereWriteYourParentUserControlID")
l_panel= l_instanceUcParent.FindControl("HereWriteYourPanelIDgeretedforPanelofUCParent")
l_instanceUploadChild = l_panel.FindControl("HereWriteYourUploadChildUserControlID")

now you can access each and ever property  l_instanceUploadChild over here

thanx and regards
0
 
Smacky311Commented:
How about something like this...


UserControl myParentUserControl = 
(UserControl)FileUpload1.Parent.Parent.Parent;
        
AttributeCollection myParentsAttributes = myParentUserControl.Attributes;

Open in new window

0
 
svcoderAuthor Commented:
I actually went with a different approach. Since this approach would couple the user control to the parent page, I had to take another approach. Instead of crawling up the control heirarchy, I simply created a public method within the user control and called that method when I load the control. I passed the objects I needed in the method call. That way the control could receive the required data and could still be re-usable in other pages. The solutions provided would probably work with some massaging to be able to crawl up the container controls to get to the parent, but I needed a different solution. Thanks for the replies.
0

Featured Post

[Webinar] Improve your customer journey

A positive customer journey is important in attracting and retaining business. To improve this experience, you can use Google Maps APIs to increase checkout conversions, boost user engagement, and optimize order fulfillment. Learn how in this webinar presented by Dito.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now