Our Mindfly Blog

Website Design and Development

Random creative design element

Correct the stylesheet link tag for ASP.NET 2.0 Themes

by Rusty Swayne 2.September 2007 14:58

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
    

Comments

Hector

Hector said on July 2, 2008 (03:58)...

Thanks for coding tips....


Hector

Hector said on July 3, 2008 (01:44)...

Hi again, I am trying this script but have some problems with that. So I want to ask you thatIs that complet and accurate?


Rusty Swayne

Rusty Swayne said on July 3, 2008 (08:41)...

Yes, it is complete. I have had no problems with it ... what issues are you having?


Jim

Jim said on July 14, 2008 (02:59)...

It sounds good,,keep it up man.


Rose

Rose said on July 14, 2008 (03:00)...

Fantastic effort..........


Add comment



(Will show your Gravatar icon)  









Live preview

said on August 28, 2008 (05:32)...


 

Powered by BlogEngine.NET 1.2.0.0. Original Design by Heather Alvis.
Sign in

Bellingham, Washington
Copyright © 2007 Mindfly Inc. All Rights Reserved.