• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 847
  • Last Modified:

Masterpage Path problem with Stylesheets

My question is related to a previous question which is found at:

Topics > Programming Languages > .NET > ASP.NET > Q_21334823.html

I am trying to use Masterpages on my site, where my Masterpage and my Stylesheet are in the root directory, and the pages calling it (ex: default.aspx) are in subdirectories. Here's my setup:

  - Masterpage.master
  - MyStyles.css
  - Pictures (directory)
       - Upload1.aspx

The previous question asked how to get image-related URLs to point correctly when the calling page was not in the same directory as the Masterpage. However, this solution (ie: none of the solutions posted in this question) work for setting the relative path for the stylesheet.

I have the following code in my Masterpage, and a css file MyStyles in the root directory:

<head runat="server">
    <link rel="stylesheet" type="text/css" href="MyStyles.css">

However, when I run upload1.aspx (which uses the Masterpage), it will not find the Stylesheet.

I have tried "~/Mystyles", "..\Mystyles", "..Mystyles" etc... to no avail.

I have also tried to add:

    Dim Mylink As HtmlLink
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
        Mylink.Href = "MyStyles.css"
        Mylink.Attributes.Add("rel", "stylesheet")
        Mylink.Attributes.Add("type", "text/css")
    End Sub

but still with no success.

Any ideas ?

Thanks, Peter
2 Solutions
try put  (if your MyStyles.css is in root)
<head runat="server">
    <link rel="stylesheet" type="text/css" href="~\MyStyles.css">
The link tag is processed as literal text and as such the href attribute is not processed and changed to be relative. The way around this is to either use asp.net 2 themes or Page.ResolveUrl for the tag.


<link type="text/css" rel="stylesheet" href='<%=Page.ResolveUrl("~/MyStyle.css")%>' />

"~/" is the application root
pbisseggerAuthor Commented:
By the way, I figured out the problem was not due to the path of the CSS file, but to the CSS file itself.

In the CSS file I had defined classes (ex:TD.Titles), whereas in my .ASPX page I was calling TD class="titles". Apparently (now I know) CSS files are CASE SENSITIVE, so that's why it was not applying the formatting.

Therefore, all the original solutions I tried actually do work for the path to the stylesheet ex:

<link rel="stylesheet" type="text/css" href="~/Styles/KryliumStyles.css"> works fine.

However, whether the stylesheet was in the root or not, the solution from nehaya does work, but I could not get the solution  from EtherZa to work in either case,

Thanks Guys!
Just another option for someone, similar to that above, the following worked for me trying to link a stylesheet to a contentpage (of a masterpage).  

This page_load is for the contentpage.

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim Mylink as new HtmlLink
        Mylink.Href = "~/MyStyles.css"
        Mylink.Attributes.Add("rel", "stylesheet")
        Mylink.Attributes.Add("type", "text/css")
End Sub

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now