Masterpage Path problem with Stylesheets

Posted on 2006-06-04
Last Modified: 2008-02-07
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
Question by:pbissegger
    LVL 6

    Accepted Solution

    try put  (if your MyStyles.css is in root)
    <head runat="server">
        <link rel="stylesheet" type="text/css" href="~\MyStyles.css">
    LVL 3

    Assisted Solution

    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 2 themes or Page.ResolveUrl for the tag.


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

    "~/" is the application root

    Author Comment

    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!

    Expert Comment

    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

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Join & Write a Comment

    Suggested Solutions

    User art_snob ( encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
    International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
    Need more eyes on your posted question? Go ahead and follow the quick steps in this video to learn how to Request Attention to your question. *Log into your Experts Exchange account *Find the question you want to Request Attention for *Go to the e…
    how to add IIS SMTP to handle application/Scanner relays into office 365.

    728 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now