Dave Burke - Freelance .NET Developer specializing in Online Communities

A freelance .NET Developer

Referencing a dynamically loaded usercontrol from a child control

This has been kicking my butt for a while; I've written about it before, and I'm still not happy with the solution.  But I'm moving on for now.  The problem is how to reference methods and properties in a Parent Usercontrol which is loaded dynamically from another Usercontrol.

I'm using a dynamically loaded Pagetemplate.ascx (from a base page) which loads header and sidebar usercontrols dynamically as well as a “body“ usercontrol, which is determined at runtime by an XML page description lookup file.  Referencing this body control has been problematic.  Referencing same tier usercontrols is cake, since the comfortable Parent.FindControl() method is available for casting and reference.  But referencing the dynamically loaded parent usercontrol, that's a bit more sticky.

Trace is a wonderful thing.  A screenshot is below which describes my situation.  I'm in the control in red and want to get to the control in blue.  Not a clearcut path to follow.  Using the .UniqueID property does the job, but I'm not thrilled with it.  I played with going down through the tiers starting with the PageMemberTemplate.ascx control [ foreach (Control c in pagememtemplate.Controls) ], looking for the type [ if (c.GetType().ToString() == “ASP._dirbody_ascx“) ] , and if true, grab the UniqueID property for a resulting cast and method call.  But my understanding is that the Unique ID of the parent control isn't going to change unless I change the page structure, so going through the tiers finding the runtime UniqueID was an academic exercise only. 

But hey, it works.

 

Comments

Karl said:

Couldn't you solve your problem using strongly typed classes?

'cast you base page to its type and declare a property which'll hold your Template

public class Directory
Inherits Page

public Template as MyTemplateBase

Sub Page_Load
Template = ctype(Page.LoadControl("~/template.ascx"), MyTemplatePage)
placeholder.Controls.Add(template)
End Sub

End class


declare a bass template:
public class MyTemplateBase
inherits usercontrol

public body as control
end class

Then, in your template:

public class PageMemberTemplate
Inherits MyTemplateBase

sub page_load
'body is defined in the base class
body = Page.LoadControl("dirbody.acx")
placeHolderBody.controls.add(body)
end sub
end class


and finally, from your red control, you can get to the blue control via:

ctype(Page, Directory).Template.Body

or something similar...
# April 23, 2004 8:37 AM

Dave Burke said:

Karl, Thanks! I'll pursue this tonight and post back. I never considered strongly typed classes. I very much appreciate you taking the time to provide this possible solution.
# April 23, 2004 8:46 AM

Brendan Tompkins said:

Dave,

I've considered this same problem, and I've decided that referencing parent objects is not the best OO pattern to use. When I've needed to do this, I'll raise an event in the child control, to which the parent subscribes. 9 times out of 10, this gets around the problem of parent-referencing.

-Brendan
# April 23, 2004 1:33 PM

Dave Burke said:

Brendan, Events in the child control subscribed to by the parent. Wow. This is something new on my "definitely must explore" list. Thanks!
# April 23, 2004 1:55 PM

TrackBack said:

# April 23, 2004 11:32 PM

Brendan Tompkins said:

Dave,

There's an article here describing exactly what I was talking about. -Brendan

http://www.odetocode.com/Code/94.aspx
# May 7, 2004 10:22 AM

Dave Burke said:

Brendan, I read through this and will work through it tonight. I've wrestled with this issue again in last night's R&D session. I tried adding the dynamically loaded control as an HttpContext item and referencing it that way, but it was a no go. I'm still using the Page.FindControl([ACSX's UniqueID value string]) and want to get away from it. Thanks very much! I'll be posting back on this issue, for sure.
# May 7, 2004 1:02 PM

TrackBack said:

# May 8, 2004 4:30 PM

Hilary Swenson said:

Pretty cool blog you've got here. Thanx for it. I like such themes and anything that is connected to them. I definitely want to read more on that blog soon.  

Hilary Swenson    

<a href="www.pickescort.com/">best escorts</a>

# April 15, 2011 2:26 PM

condom said:

Este mensaje simplemente incomparable )      

http://www.shampes.com/      

thania

# September 4, 2011 12:07 PM
Leave a Comment

(required) 

(required) 

(optional)

(required)