Dave Burke : Freelance .NET Web Developer specializing in Online Communities

Controls in controls in controls [Updated - Solved! but ugly]

For a charity project I took the time to employ a barebones inheritance approach to web page design.  A basewebpage loads a basepagetemplate usercontrol which loads three page controls, one of which is the bodycontrol which itself loads one or more controls.  This allows me to isolate code to a single control, the only “new“ coding required for any given application.  This works great until I try to create an object reference between two controls loaded from the bodycontrol (which is loaded from the basepagetemplate control.)  As in

protected SiteNews sitenews;

Label lblNews = (Label) sitenews.FindControl("lblMenu");
lblNews.Text = "hello from Profile Control!";

...and many variants on the above code.

I can't seem to get past the “Object Reference not set to an instance of an object“ runtime error.

This is not something I have to do, just something I wanted to do to understand how to do it.  After several latenight hours I decided to put this on hold.  I GUESS it can be done, but since this quest was primarily an academic one, if it CAN be done I'll figure it out later.

So on the latenight R&D shelf for now: understanding how to talk between two controls deep in the loaded control heriarchy.

[UPDATE]

A big thanks to the guys who commented on this.  I thought about each comment, but what struck a chord was Scott Allen's statement that the control ID might be "munged" (that would be a soft "g").  Yes, when I had previously checked out the HTML source, I saw stuff like

<span id="_ctl0__ctl2_uc_sitenews_lblNews" class="mredstart"></span>

and knew I was seeing something related to the “object reference not set...” error.  So I grabbed the label's UNIQUEID property with lblNews.Text = lblNews.UniqueID.ToString().  It was something like

_ctl0:_ctl2:uc_sitenews:lblNews 

So I used the UniqueID with a Page.FindControl method, as in

Label.lblNews = (Label) Page.FindControl(”_ctl0:_ctl2:uc_sitenews:lblNews”);

which worked. 

As I said in the subject [update], this is ugly and it may in fact be wrong, but hey, it works.

In review, WHAT works?  I can now seem to reference any object in any control from any other control in a “multi-layered” inherited design with dynamically loaded control.  Sounds like a sweeping pronunciation, doesn't it?

 

Comments (0) | Post RSS RSS comment feed

Posted on 3/14/2004 2:03:00 PM by Dave Burke
Categories:
Tags:

Related posts


Powered by BlogEngine.NET 2.0.0.36
Theme by Dave Burke