By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,813 Members | 1,257 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,813 IT Pros & Developers. It's quick & easy.

Master Page prevents using style sheets?

P: n/a
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.

Sep 5 '06 #1
Share this Question
Share on Google+
8 Replies


P: n/a
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></HEADarea.

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

"JT" <jt@onemain.comwrote in message
news:11**********************@e3g2000cwe.googlegro ups.com...
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.

Sep 5 '06 #2

P: n/a
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></headsection:

<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.

JT
Rob wrote:
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></HEADarea.

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

"JT" <jt@onemain.comwrote in message
news:11**********************@e3g2000cwe.googlegro ups.com...
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.
Sep 5 '06 #3

P: n/a
"Rob" <rmacfadyen_at_gmail.comwrote in message
news:Ob**************@TK2MSFTNGP03.phx.gbl...
By using master pages the individual "sub pages" do not have direct access
to the <HTML><HEAD><BODYtags (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");
Sep 5 '06 #4

P: n/a
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 432'17"N 882'37"W : 432'17"N 882'37"W

"Mark Rae" <ma**@markNOSPAMrae.comwrote in message
news:uZ**************@TK2MSFTNGP04.phx.gbl...
"Rob" <rmacfadyen_at_gmail.comwrote in message
news:Ob**************@TK2MSFTNGP03.phx.gbl...
>By using master pages the individual "sub pages" do not have direct
access to the <HTML><HEAD><BODYtags (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");


Sep 6 '06 #5

P: n/a
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
Sep 6 '06 #6

P: n/a
I've eaten more crow than you'll ever know ;-)

<%= Clinton Gallagher

"Rob MacFadyen" <rmacfadyen_at_gmail.comwrote in message
news:uu**************@TK2MSFTNGP03.phx.gbl...
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

Sep 6 '06 #7

P: n/a
"clintonG" <cs*********@REMOVETHISTEXTmetromilwaukee.comwro te in message
news:e7**************@TK2MSFTNGP04.phx.gbl...
I've eaten more crow than you'll ever know ;-)
Likewise... :-)
Sep 7 '06 #8

P: n/a
JT
I like mine with peppercorn sauce. Mmmm... mmmm!

Thanks again for the help!

Mark Rae wrote:
"clintonG" <cs*********@REMOVETHISTEXTmetromilwaukee.comwro te in message
news:e7**************@TK2MSFTNGP04.phx.gbl...
I've eaten more crow than you'll ever know ;-)

Likewise... :-)
Sep 8 '06 #9

This discussion thread is closed

Replies have been disabled for this discussion.