Dave Burke - Freelance .NET Developer specializing in Online Communities

A freelance .NET Developer

Back in Session, or, More on Controls in Controls

The dynamically loaded, multi-tiered control architecture of this afterhours project has its benefits, certainly, but with it I'm forced to use different approaches to communicating among objects throughout the app.

A particular child control has a different datasource and displays specific page elements (like a header image) based on a QueryString value.  In a “bottom up” approach, I set the header image in the child control load method, using the down and dirty Page.FindControl(.UniqueID) approach (which I'm going to review, thanks to good alternative ideas I'll investigate asap from Karl and Brendan)

Image imgHeader = (Image) Page.FindControl("_ctl0:_ctl1:imgHeader");
string img = ntid == 1 ? "header_members_memnews.gif" : "header_members_sitenews.gif";
imgHeader.ImageUrl = "/images/headers/" + img;

But then I considered Session.  For years (mostly in the ASP era) I would religiously avoid using Session.  I wanted to have me a SERVER FARM, and Session would only muck things up!  Anyway, I'm warming up to Session again and seeing some good uses for it. In this case I override the BasePage LoadTemplate() method in the control's parent page and set a Session value for headerImage.  The Config.Instance() populates the PageConfig class from an XML file, supplying such things as page title, page header, and so on, but I updated the Config.Instance() method to check for a specific headerImage value in the XML file (“QueryStringDependent“), and if found, pull the headerImage value from Session.

I should have more code to fully explain, but suffice it to say that using Session in conjunction with inheritance seems to be a valid alternative to a bottom-up approach of talking to application elements from child controls.

protected override void LoadTemplate()
{
if (Request.QueryString["ntid"] != null)
 Session["headerImage"] = Request.QueryString["ntid"].ToString() == "1" ? "header_members_memnews.gif" : header_members_sitenews.gif";
else
 Session["headerImage"] = "header_members_memnews.gif";
pageconfig = Config.Instance();
.
.
.
}
  

Comments

Jerry Pisk said:

The request's Context is a lot better place to store request related objects than the Session.
# April 24, 2004 5:08 PM

Dave Burke said:

Jerry, Ya know, you're absolutely right. I'll most likely re-architect to go with context rather than session. While I use it, I'm still not a big fan.
# April 24, 2004 6:13 PM
Leave a Comment

(required) 

(required) 

(optional)

(required)