Wednesday, January 25, 2006

Seperate Processes Sometimes Break ASP.NET Session

I ran across an interesting problem this morning. A user was browsing the Property Information System, and when he clicked on the map to identify a property the popup window would launch but it would display an ASP.NET Error Message, "Object reference not set to an instance of an object." I examed the source, and on page load the popup gets stuff from the ASP.NET session.

It was behaving like his session had timed out, even though it shouldn't have for 20 minutes.

After some time on the phone we managed to track it down to a setting in Windows Explorer (not Internet Explorer). In Tools|Folder Options on the View tab, there is a setting called "Launch folder windows in a seperate process." As it turns out, if you have this set, and type the URL into the address bar of Windows Explorer, popups launch in a seperate process. This was creating a new session on the server. So any session information from the parent window was not available to the popup window.

The solution: Don't use Windows Explorer that way (launch I.E. for browsing) or don't rely on the session like we used to say back in the days of ASP 3.0.

My CSS References

I was going to just email this to a couple of co-workers, but I decided it would make a decent little blog post. These are the some of the CSS sites that I read and could help you out with learning and working with CSS.

Good Sites:
CSS Vault (lots of good examples)

Blue Robot (free templates)

Glish (free templates, good examples)

CSS Zen Garden (a classic, 1 HTML page styled by your choice of CSS)

Position is Everything (they talk a lot about hacks, in fact Holly Bergevin is the creator of the "Holly Hack")

References:
A Bunch of CSS Links (a bunch of links, important ones highlighted)

MSDN (I use this for reference a lot)

Interesting Articles:
A CSS Framework (About making a reusable CSS to build upon)

The 10 Best Resources for CSS (a good article with more links, some of which are here)

One clean HTML markup, many layouts (this is cool because he doesn't use any hacks)

Image Map for Detailed Information (a neat little example)

Blogs and the like
Dustin Diaz (a good writer, he talks about design, CSS, and Javascript)

A List Apart (good articles, multiple contributers)

Content with Style (another great one with multiple contributers)

Mezzoblue (beautiful colors and some good articles)

Jason Santa Maria (he mostly talks about page design, but I really like his post on Grey Box Methodology)

Jason Graphix (he mostly talks about page design too)

Clagnut (a nice looking site built with CSS, and some articles)

Wednesday, January 11, 2006

Dynamically Add a Stylesheet Reference to a MasterPage

Today I was having a little problem with ASP.NET 2.0. My site uses a Master Page and a couple of .css files. The problem was that I wanted one stylesheet to be used on the whole site, and an additional stylesheet to be used on one page. The problem was that in a child page I couldn't find a way to add an HTML link element, since the page's head section was outside of the content.

What I ended up doing was adding a public method in the Master Page that I could call in the child's Load event to add a stylesheet reference. It seems to work well so far. I don't know if this is the best approach, but I couldn't find much else out there.

Here are the details:

MyMasterPage.master is the site-wide master page, in it you will need to add an id to the <head /> element so you can get to it in the code behind:

<head id="MyMasterPageHead" runat="server">

 

 

 


In the code behind of MyMasterPage you will need to add the following method:

Partial Class MyMasterPage

  Inherits System.Web.UI.MasterPage

 

  Public Sub AddStyleSheetLink(ByVal fileName As String)

    Dim stylesheetLink As New HtmlLink

    With stylesheetLink

      .Attributes("href") = fileName

      .Attributes("type") = "text/css"

      .Attributes("rel") = "stylesheet"

    End With

 

    MyMasterPageHead.Controls.Add(stylesheetLink)

 

  End Sub

End Class

 

 

 


Then on your content page, just add the following to the Page.Load event:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

  CType(Master, MyMasterPage).AddStyleSheetLink("ContentStyle.css")

End Sub

 


Notice that you need to cast the Page's master to the MyMasterPage type so you can call the AddStyleSheetLink() method.

That's it!