Mindfly Website Design Studio

Mindfly Web Designer Favorite

Blog

Our Posts on Web Design

Correct the stylesheet link tag for ASP.NET 2.0 Themes

by Rusty Swayne September 2, 2007 9:58 AM

Even though this method is documented fairly well (and to be honest I don't remember where I got this code snippet) I often have people argue that they don't use asp.net themes as they break when rewritting urls.  The problem stems from the fact that when the them renders by default the link tag created uses a relative path such as:

    <link rel="stylesheet" 
type="text/css" href="App_Themes/rustyswayne/a_normalize.css" />

and what you really need if for it to read:

    <link rel="stylesheet" 
type="text/css" href="/App_Themes/rustyswayne/a_normalize.css" />

You can get around this problem by Overriding the Render routine in your page. I usually do this in a BasePage class my pages inherit from:

    Protected Overrides Sub Render(ByVal writer As System.Web.UI.HtmlTextWriter)
    Dim ctl As Control
For Each ctl In Page.Header.Controls
    If TypeOf (ctl) Is HtmlLink Then
Dim linkTag As HtmlLink = CType(ctl, HtmlLink)
    If (linkTag IsNot Nothing) And linkTag.Href.StartsWith("~/") Then
    If Request.ApplicationPath = "/" Then
linkTag.Href = linkTag.Href.Substring(1)
Else
linkTag.Href = Request.ApplicationPath & "/" & _ 
linkTag.Href.Substring("~/".Length)
End If
    End If
    End If
    Next
    End Sub

Tags: ,

Categories: Web Development

Permalink | Comments (5) | Post RSSRSS comment feed

Comments