views:

1223

answers:

7

I have an APS.net app (C#) with several pages that use the same MasterPage. In that page, I have included a couple stylesheets like so:

<head runat="server">
<link href="/apps/_lib/ui/styles1.css" type="text/css" rel="stylesheet" />
<link href="/apps/_lib/ui/styles2.css" type="text/css" rel="stylesheet" />
</head>

These styles apply correctly to all content that is in the actual .master file (page), but are not applied to any pages that use the Master page (for instance default.aspx uses that master page and has a Content placeholder in it).

If I add these lines in each individual page:

<link href="/apps/_lib/ui/styles1.css" type="text/css" rel="stylesheet" />
<link href="/apps/_lib/ui/styles2.css" type="text/css" rel="stylesheet" />

Then the styles show up as expected... but it was my hope that I could include them in the master page so that I didn't need to include these references in each subsequent page too.

Because these files are not located physically in the same project (they are shared between several apps), i cannot just include them as an ASP.net theme that would be applied to all pages.

Update

In order to rule out the file locations problem, I used the absolute URL for these stylesheets...

<link href="https://myserver/apps/_lib/ui/styles1.css" type="text/css" rel="stylesheet" />
<link href="https://myserver/apps/_lib/ui/styles2.css" type="text/css" rel="stylesheet" />

That way, no matter where it is read from, the file can be located. This still exhibits the same behavior.

To me, it looks like the masterPage is rendered with the CSS styles (because they're in the same file) and then the child/calling page is rendered without the CSS styles (because they aren't in that file, they're in the masterPage) and then those two files are combined together (as opposed to combining them together first and THEN rendering the style elements for the combined pages). Adding to this belief was my previous example of adding the include for the CSS file in the calling page itself, which will cause it to display the CSS correctly.

You can view the source of this file, and in the head section, you can see the CSS styles linked correctly, but they aren't applied to any elements from the calling page while they are applied to all elements in the masterPage.

+1  A: 

What about this?

<link runat="server" href="~/apps/_lib/ui/styles1.css" type="text/css" rel="stylesheet" />
<link runat="server" href="~/apps/_lib/ui/styles2.css" type="text/css" rel="stylesheet" />
Time Machine
Nope, sorry... doesn't make a difference.
Kris
A: 

It looks like you have done it correctly, so your error seems weird and sounds like maybe you've mady a typo in the master page. Try to look at the generated source (view source in the browser) of the two pages, one with the links in the master pages and one with the links in the web page, and compare the paths.

Jan Aagaard
A: 

Something like this should work. You should be able to include the css in the header of the master. And then, include a ContentPlaceHolder in the header so if you need custom header stuff in your content pages you can do that. Does that not work?

<head>

<link href="/apps/_lib/ui/styles1.css" type="text/css" rel="stylesheet" />
<link href="/apps/_lib/ui/styles2.css" type="text/css" rel="stylesheet" />


<asp:ContentPlaceHolder ID="head" runat="server">
</asp:ContentPlaceHolder>
</head>

Ultimately you don't want a

<head></head>

tag in your content page, it's possible that is overriding your master page header.

brendan
+1  A: 

Is the content page in a different folder that the master? If so, you'll have to get the application relative path to the css files using ResolveUrl in the master page:

<link href='<%= ResolveUrl("~/apps/_lib/ui/styles1.css") %>' type="text/css" rel="stylesheet" />

Update: If this doesn't work, then you might have HTML or CSS errors like Lance suggested. Try using the HTML and CSS validators at w3schools.com. If it still doesn't work after fixing any errors, double check your CSS selectors with the rendered HTML as Steve suggested. ASP.Net's generated IDs have bitten me more than once.

jrummell
I think jrummell might be one to something. The css styles from the link in the masterpage should apply to the contentpage.
Steve
I thought that might be the case too, so I tried moving them to the same directory, but I still get this same problem.
Kris
When I do this... <link href='<%= ResolveUrl("~/apps/_lib/ui/styles.css") %>' type="text/css" rel="stylesheet" />I renders in HTML as this... <link href="CommonUI/%3C%25=%20ResolveUrl(%22~/apps/_lib/ui/styles.css%22)%20%25%3E" type="text/css" rel="stylesheet" />Which doesn't work.
Kris
That's odd, it works for me. Is your link inside head with runat="server"?
jrummell
I agree it's very odd. And I did include runat="server" in the head tags.
Kris
+1  A: 

What jrummell posted is what I use on my sites to ensure that the links work if I were to move my pages around.

As far as rendering. The rendering is done on the client machine's browser. To the browser it has no idea the html is generated from multiple documents.

I would make sure your HTML and CSS are correct.

On a side note i have noticed the last week or so that VS2008 keeps messing my css stylesheets up, doing really random things like moving text around. However, I think this might just be something on my machine. Just something to check.

Here is some sample code I checked this just to make sure and this works.

Head of Master Page

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

Content Place Holder

<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
    <div class="something">
        Lance
    </div>
</asp:Content>

Css in StyleSheet

.something{
    color:Blue;
}

The result is "Lance" in Blue in the child page.

Lance Ingle
Verified this with a new tag and css class and it worked. Looked more closely at the CSS and the found that the wrong css class to display error messages was being applied (an easy to make error given how closely the two classes were named). Seems to work right now that I renamed things.
Kris
Good, I'm glad i could help.
Lance Ingle
+1  A: 

Maybe it might be your css selectors. When you add master pages, asp.net tacks on more prefixes to the ids.

Steve
+1  A: 

I had a similar problem. As others have suggested, you should be able to use ResolveUrl to make sure the path is correct.

Unfortunately, this created a further problem for me. My head tag was runat server. My link tags were not. The resolve url within my link tag would never execute. Instead, the C# code and response.write tags were being encoded and outputted to the page.

Bizarrely, the same technique in a script (non-runat server) tag would work as normal.

To solve the problem, I ended up writing the whole link tag within an ASP.Net Response.Write tag:

<%="<link href='" + ResolveUrl("~/apps/_lib/ui/styles.css") + "' rel='stylesheet' type='text/css' />"%>

That worked. How strange. Somehow, it is all related to the head tag being runat sever.