Master Page prevents using style sheets?

J

JT

Hi,

I have done a fair amount of style editing inline in ASP. I'm now
using VS 2005 with a standard web project (not Web Application
Project). This is my first foray into CSS in a style sheet and also my
first true attempt at using master pages. I tried setting up a style
sheet with a simple setting to float an image to the right and it had
no effect on the image.

Then, I tried putting the style code in my ASPX file as such,

<%@ Page Language="C#" AutoEventWireup="true" MasterPageFile =
"~/MyMaster.master" CodeFile="ProductDetails.aspx.cs"
Inherits="ProductDetails" %>
<style type="text/css">
img
{
float:right;
border:1px dotted black;
margin:0px 0px 15px 20px;
}
</style>

<asp:Content ID="Content1" ContentPlaceHolderID="cphMain"
Runat="Server">
<asp:panel ID="pnlProduct" runat="server" Height="50px"
Style="z-index: 100;
position: relative;" >
<asp:Label ID="lblManufacturer" runat="server" Font-Bold="True"
Font-Underline="True"
Style="z-index: 101; position: relative;"
Text="Manufacturer" Font-Size="X-Large"></asp:Label>
<br />
<asp:Label ID="lblProductName" runat="server" Font-Bold="True"
Font-Underline="True"
Style="z-index: 100; position: relative;" Text="Product
Name"></asp:Label>
<br />
</asp:panel>
<asp:panel ID="pnlDescription" runat="server" Height="50px"
Style="z-index: 102;
position: relative; text-align: left" Width="873px">
<asp:Literal ID="ltrDescription" runat="server"></asp:Literal>
</asp:panel>
</asp:Content>

It complained that "Content is not supported outside 'script' or
'asp:content' regions".

I tried moving the style setting inside the asp:content region and it
complained that "Element 'style' cannot be nested within element 'td'".
This is because my ContentPlaceHolder is inside a table in my master
page. As of yet, I can't get the desired layout without doing that.

I created a new page, ProductDetails2.aspx, as a stand-alone Web Form
page, inserted the style element into the head element and now it
works, but I don't have the menu and other standard page formatting
that was contained in the master page. So my questions are:

1. What do I need to do to make my style settings work with pages that
reference my master page? Do I need to take away all formatting that
would require the style element to be nested?

2. Do I need to reference my stylesheet like you would reference a DLL?

3. Does including a stylesheet in the solution/project automatically
apply it to all pages in the project?

Thanks for your help.
 
R

Rob

JT,

Your styles should appear in the master page. By using master pages the
individual "sub pages" do not have direct access to the <HTML><HEAD><BODY>
tags (by default).

You can add a public property/method to the master page that provides access
to the head element though. More or less in the master's .cs file add:

public void AddStyles(string styles)
{
Literal1.Text = styles;
}

In the master's markup add a literal control in the the <HEAD></HEAD> area.

In the sub page's markup add the following declaration (forces Page.Master
to be strongly typed)
<%@ MasterType VirtualPath="~/masters/SourcePage.master" %>
And in the sub page's onload you can do something like:

protected void Page_Load(object sender, EventArgs e)
{
Page.Master.AddStyles("<style>img {float:right;border:1px;} </style>");
}

Regards,

Rob
 
J

JT

Sweet! I'll have to explore that.

I just found something in an old HTML book of mine concerning css. I
was making a newbie error. I needed to put the following into my
master page's <head></head> section:

<head runat="server">
<style type = "text/css">
<!--
@import url(MyStyleSheet.css);
-->
</style>
</head>

That got my styles going for the site. But I'm thinking I'll have to
use your suggestion to override that in specific pages, correct?

Thanks for the quick response! I'm sure my eyes will be opened once I
realize the power of a css file. No more repeated inline code or
include files. I still prefer WinForms apps.
 
M

Mark Rae

By using master pages the individual "sub pages" do not have direct access
to the <HTML><HEAD><BODY> tags (by default).

Yes they do - just add runat="server" to the tag e.g.

<header runat="server">
....
....
....
</head>

Then, in your codefile, you can do something like:

Style objStyle = new Style();
objStyle.ForeColor = System.Drawing.Color.Yellow;
Header.StyleSheet.CreateStyleRule(objStyle, null, "td");
 
C

clintonG

Furthermore, to help clarify Rob's misunderstanding there are new classes
unique to the head. Start with the following search term and look through
the other related classes listed in the tree that is displayed with MSDN
documentation.

htmlhead class site:msdn2.microsoft.com

<%= Clinton Gallagher
NET csgallagher AT metromilwaukee.com
URL http://clintongallagher.metromilwaukee.com/
MAP 43°2'17"N 88°2'37"W : 43°2'17"N 88°2'37"W
 
R

Rob MacFadyen

Whoopsie :)

I am of course massively incorrect. As pointed out:

this.Header.StyleSheet.CreateStyleRule(...)

is exactly what you're looking for.

I guess I was just to impressed with strongly typed master pages.

Thanx to Mark Rae and Clinton for keeping things straight :)

Regards,

Rob MacFadyen
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,007
Latest member
obedient dusk

Latest Threads

Top