The subtitle of this post is "If Karl can't reference it, no one can!" Big fat thank you, Karl!
In a comment to one of my previous posts on this subject, Karl (last name not known...yet) very generously provided a killer approach to referencing dynamically loaded parent controls: using Strongly Type Classes. I never thought in those terms. He even whipped out a complete outline of the inheritance process in his comments (found at bottom in red and in the original post comments.) The guy must have inheritance down COLD! I spent about an hour working through Karl's code outline and comparing it to my existing logic, which was almost exactly the same except for a slight difference which I'll point out shortly.
The essential crux of the referencing issue is found in a BasePage : Page - OnLoad method. pageTemplate is a base page control. pageconfig is a class with .BodyControl and other properties being retrieved from an XML file:
pageTemplate.FindControl("pageBody").Controls.Add((Control)Page.LoadControl(pageconfig.BodyControl));
It is here where using strongly-named classes rocked my nerd world. In this same BasePage class I added
public Control body;
and replaced the above FindControl statement with
body = Page.LoadControl(pageconfig.BodyControl);
pageTemplate.FindControl("pageBody").Controls.Add(body);
NOW I HAVE SOMETHING TO REFERENCE!
Let's travel now to the child UserControl (DirectoryPhotos) loaded by the dynamically loaded body control (DirectoryBody) from the PageTemplate base template, we can use the strongly typed body class reference to the dynamically loaded body control (after casting it a couple of times..)
Old way using FindControl(.UniqueID)
//DirectoryBody dirbody = (DirectoryBody) Page.FindControl("_ctl0:_ctl2");
//dirbody.ShowDetails(user_id);
New way using strong-typed classes
DirectoryBody dirbody = (DirectoryBody) ((Directory) Page).body;
dirbody.ShowDetails(user_id);
Its all so clear to me now. In fact, I can see all the way to Mount Mansfield!
On a similar but different issue, in the same post comments on referencing parent controls, Brendan Tompkins provided another completely new and different (for me) approach to communicating between controls: event bubbling. That will be another post.
Karl's code outline in a previous post's comments is below. There is a slight logical difference. Karl is declaring the "public body as control" in the base usercontrol template, where I am doing this in the BasePage page class
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 base 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