views:

183

answers:

4

Hi all,

I noticed that I cannot add stylesheets on any page. They must be added to the master page.

I already have about 15 stylesheets in the master view, which seems like overkill, since only some of the pages use a certain stylesheet.

I imagine I could reference the file via javascript (although, I can't think of how off the top of my head), but it would be really nice to not have to use any.

+5  A: 

You should be able to add them to the head placeholder on the content pages...

MasterPage:

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

Content Page:

<asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" runat="server">
    <%-- link styles here...--%>
</asp:Content>
Alexander
Pretty much same example I had. +1
Atømix
The sad thing is, at least in VS2k8, when you create a MasterPage, it adds one of these for you, with the `id="head"`
R. Bemrose
Ahhh I'll have to try that out, thanks. I didn't even know about the purpose of the asp contentPlaceHolder.
Darcy
+1  A: 

No, they don't have to go in the master page.

You can create a content placeholder for them in the master page, and then add page-specific styles using a content element on the page.

E.g., in the master:

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

Then in the page:

<asp:Content ID="styleContent" ContentPlaceHolderID="Styles" runat="server">
    <link rel="stylesheet" type="text/css" href="<%= Url.Content("~/Content/style/MyStyles.css") %>" />
</asp:Content>
Craig Stuntz
+1  A: 

Can you show some code?

It's actually possible to have a CSS stylesheet in the view. But the question is if it's a good practice. The best idea is to create a placeholder in the masterpage in the Head section and use this placeholder in the view to use the correct CSS files.

Like this:

In your masterpage:

<html xmlns="http://www.w3.org/1999/xhtml"&gt;
<head runat="server">
    <title>Example</title>
    <asp:ContentPlaceHolder ID="HeadContent" runat="server" />
</head>

<body>

Inside your view:

<asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" runat="server">
    <link href="<%= Url.Content("~/Content/style/MyStyles.css") %>" rel="stylesheet" type="text/css" />
</asp:Content>
Rody van Sambeek
Prefer `Url.Content` over relative paths as the former will not be broken by routing changes.
Craig Stuntz
Your completely true. Just copied a quick link tag in there. Changed it for the convenience for a later reader.
Rody van Sambeek
A: 

You can store stylesheets, along with images and control skins, as part of a Theme and specify on each content page which theme should be used. Note: You can not specify a theme in the MasterPage itself. It only works with Page directives.

Note that this requires the head element to be runat="server" to have ASP.NET automagically add the appropriate stylesheet references to the page.

I'm not sure if this applies to ASP.NET MVC or not, but you are using a MasterPage...

R. Bemrose