472,950 Members | 1,968 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,950 software developers and data experts.

MasterPages and applying CSS to content pages

Right now the header of my master page contains all of the CSS styles for
all of the pages that might be loaded in my ContentPlaceHolder. The problem
is that my <styletag is getting too large to manage. I have 300 lines of
styles in my masterpage and I don't need all of this for every page --
somehow this can't be a smart way of managing styles.

How do I programmatically apply styles in my MasterPage based on the
ContentPlaceHolder page that's being shown?

Or another way to ask it - how do I apply styles to ContentPlaceHolder pages
since these pages cannot contain a <headertag?

Thanks for any direction,
Mort

Mar 1 '08 #1
8 11704
"Mort Strom" <ma**@rocksdream.comwrote in message
news:89**********************************@microsof t.com...
How do I programmatically apply styles in my MasterPage based on the
ContentPlaceHolder page that's being shown?

Or another way to ask it - how do I apply styles to ContentPlaceHolder
pages since these pages cannot contain a <headertag?
Firstly, make sure you have runat="server" in the MasterPage's header tag
e.g.

<head runat="server">

</head>

Then, in the PageLoad event of the content page:

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

or

HtmlLink objCSS = new HtmlLink();
objCSS.Attributes.Add("href", "~/css/DifferentStyle.css");
objCSS.Attributes.Add("rel", "stylesheet");
objCSS.Attributes.Add("type", "text/css");
Header.Controls.Add(objCSS);
--
Mark Rae
ASP.NET MVP
http://www.markrae.net

Mar 1 '08 #2
Well done! This is exactly the kind of solution I was looking for.
I'm new to MasterPages. Thank you

"Mark Rae [MVP]" <ma**@markNOSPAMrae.netwrote in message
news:#v**************@TK2MSFTNGP06.phx.gbl...
"Mort Strom" <ma**@rocksdream.comwrote in message
news:89**********************************@microsof t.com...
>How do I programmatically apply styles in my MasterPage based on the
ContentPlaceHolder page that's being shown?

Or another way to ask it - how do I apply styles to ContentPlaceHolder
pages since these pages cannot contain a <headertag?

Firstly, make sure you have runat="server" in the MasterPage's header tag
e.g.

<head runat="server">

</head>

Then, in the PageLoad event of the content page:

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

or

HtmlLink objCSS = new HtmlLink();
objCSS.Attributes.Add("href", "~/css/DifferentStyle.css");
objCSS.Attributes.Add("rel", "stylesheet");
objCSS.Attributes.Add("type", "text/css");
Header.Controls.Add(objCSS);
--
Mark Rae
ASP.NET MVP
http://www.markrae.net
Mar 1 '08 #3
If objPage1CSS is added to the Header for Content Page1.aspx, then
objPage2CSS added for Content Page2.aspx, will the Header tag retain Page1 &
2 CSS for subsequent pages (page3, page4, etc)?

"Mark Rae [MVP]" <ma**@markNOSPAMrae.netwrote in message
news:#v**************@TK2MSFTNGP06.phx.gbl...
"Mort Strom" <ma**@rocksdream.comwrote in message
news:89**********************************@microsof t.com...
>How do I programmatically apply styles in my MasterPage based on the
ContentPlaceHolder page that's being shown?

Or another way to ask it - how do I apply styles to ContentPlaceHolder
pages since these pages cannot contain a <headertag?

Firstly, make sure you have runat="server" in the MasterPage's header tag
e.g.

<head runat="server">

</head>

Then, in the PageLoad event of the content page:

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

or

HtmlLink objCSS = new HtmlLink();
objCSS.Attributes.Add("href", "~/css/DifferentStyle.css");
objCSS.Attributes.Add("rel", "stylesheet");
objCSS.Attributes.Add("type", "text/css");
Header.Controls.Add(objCSS);
--
Mark Rae
ASP.NET MVP
http://www.markrae.net
Mar 1 '08 #4
"Mort Strom" <ma**@rocksdream.comwrote in message
news:89**********************************@microsof t.com...
Right now the header of my master page contains all of the CSS styles for
all of the pages that might be loaded in my ContentPlaceHolder. The
problem is that my <styletag is getting too large to manage. I have 300
lines of styles in my masterpage and I don't need all of this for every
page -- somehow this can't be a smart way of managing styles.

How do I programmatically apply styles in my MasterPage based on the
ContentPlaceHolder page that's being shown?

Or another way to ask it - how do I apply styles to ContentPlaceHolder
pages since these pages cannot contain a <headertag?

Thanks for any direction,
Mort
Mort, I see you've been provided a rather elegant solution, but I'm curious
why not use a separate style sheet?

The site I'm working on now uses master pages and the style sheet linked to
the master is being applied to the content pages without any extra effort.
I'm asking because maybe there is something I can learn here.
Mar 1 '08 #5
"Mort Strom" <ma**@rocksdream.comwrote in message
news:35**********************************@microsof t.com...
If objPage1CSS is added to the Header for Content Page1.aspx, then
objPage2CSS added for Content Page2.aspx, will the Header tag retain Page1
& 2 CSS for subsequent pages (page3, page4, etc)?
The thing to realise about a MasterPage is that it's not a "page" in the way
that an aspx page is a "page" - in fact, a MasterPage is little more than a
UserControl... If the good folks who wrote ASP.NET had called them
LayoutControls instead of MasterPages, then this would have been obvious...
:-)

You say that you are new to MasterPages, so maybe you're confusing them with
framesets...?

They are totally different...

When a content page is requested, ASP.NET uses the MasterPage to construct
the HTML which will eventually be streamed down to the client - it does this
every time... The MasterPage does not remain on screen or in memory when you
request a new content page...

So, every time you request a content page which needs extra styles, they
need to be added every time - that's why the code I gave you needs to go in
the content pages' code-behind, not the MasterPage's code-behind...
--
Mark Rae
ASP.NET MVP
http://www.markrae.net

Mar 1 '08 #6
"BobF" <rN***********@charter.netwrote in message
news:Ot**************@TK2MSFTNGP03.phx.gbl...
Mort, I see you've been provided a rather elegant solution, but I'm
curious why not use a separate style sheet?

The site I'm working on now uses master pages and the style sheet linked
to the master is being applied to the content pages without any extra
effort. I'm asking because maybe there is something I can learn here.
The OP had a single style sheet which was being referenced directly in his
MasterPage... However, this single stylesheet was becoming very large, and
not all of the content pages required all of the styles in the sheet...

Therefore, the solution I suggested allows him to break the stylesheet up
into several more manageable stylesheets which can then be referenced as
necessary by the content pages which need them...
--
Mark Rae
ASP.NET MVP
http://www.markrae.net

Mar 1 '08 #7

"Mark Rae [MVP]" <ma**@markNOSPAMrae.netwrote in message
news:uW**************@TK2MSFTNGP04.phx.gbl...
"BobF" <rN***********@charter.netwrote in message
news:Ot**************@TK2MSFTNGP03.phx.gbl...
>Mort, I see you've been provided a rather elegant solution, but I'm
curious why not use a separate style sheet?

The site I'm working on now uses master pages and the style sheet linked
to the master is being applied to the content pages without any extra
effort. I'm asking because maybe there is something I can learn here.

The OP had a single style sheet which was being referenced directly in his
MasterPage... However, this single stylesheet was becoming very large, and
not all of the content pages required all of the styles in the sheet...

Therefore, the solution I suggested allows him to break the stylesheet up
into several more manageable stylesheets which can then be referenced as
necessary by the content pages which need them...
Oh. I thought he had the style code embedded in the master page ...

Thanks.
Mar 1 '08 #8
Indeed I was confusing MasterPages with framesets and your explanation has
cleared that up. Thanks again for helping bring MasterPages into clearer
focus.

On my way
Mort

"Mark Rae [MVP]" <ma**@markNOSPAMrae.netwrote in message
news:eB**************@TK2MSFTNGP04.phx.gbl...
"Mort Strom" <ma**@rocksdream.comwrote in message
news:35**********************************@microsof t.com...
>If objPage1CSS is added to the Header for Content Page1.aspx, then
objPage2CSS added for Content Page2.aspx, will the Header tag retain
Page1 & 2 CSS for subsequent pages (page3, page4, etc)?

The thing to realise about a MasterPage is that it's not a "page" in the
way that an aspx page is a "page" - in fact, a MasterPage is little more
than a UserControl... If the good folks who wrote ASP.NET had called them
LayoutControls instead of MasterPages, then this would have been
obvious... :-)

You say that you are new to MasterPages, so maybe you're confusing them
with framesets...?

They are totally different...

When a content page is requested, ASP.NET uses the MasterPage to construct
the HTML which will eventually be streamed down to the client - it does
this every time... The MasterPage does not remain on screen or in memory
when you request a new content page...

So, every time you request a content page which needs extra styles, they
need to be added every time - that's why the code I gave you needs to go
in the content pages' code-behind, not the MasterPage's code-behind...
--
Mark Rae
ASP.NET MVP
http://www.markrae.net
Mar 2 '08 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Kenneth Keeley | last post by:
Hi, I have a Masterpage that controls the basic layout of the pages displayed on a web site. The masterpage also uses some variables. I would like to be able to access some of these variables from...
4
by: Jeff Lynch | last post by:
I need to call a class on every web page in a site. In ASP.NET 1.1 I would call this class before the Page_Load event in each web page's code-behind file since it needs to run before the web page...
0
by: sasquatch | last post by:
Hi, I've a a site with nested master pages and content pages. I tried using a theme with a stylesheet in the app_themes directory referencing it in the web.config file from a pages tag theme...
2
by: iturner100 | last post by:
Hi, I've been struggling with this one for a couple of hours without much joy. Basically, I've got a set of nested masterpages (3 as it happens). I'm dynamically generating a new page in code...
0
by: Learner | last post by:
Hello, I have one MasterPage (it has Header and Side lay out on it) and it has 3 component placeholders. Out of which the side one for the links to navigate through the site, the Header place is...
3
by: j-in-uk | last post by:
I have 3 pages Site.master, Album.aspx and ContinentsMenu.ascx. Continents is a usercontrol placed in Albums. In Album.aspx.cs I need to access a property from the Continents usercontrol and is...
9
by: RBM007 | last post by:
Hello, I have created some pages in the (old) Atlas pages and migrated to AJAX version. After the update I noticed that any page containing ASP.NET AJAX won't compile anymore. Even if the same...
6
by: =?Utf-8?B?U3RlcGhlbiBIYXRmaWVsZA==?= | last post by:
I have two masterpages in a web application. One is used for the login and logout pages. The other is used for all other pages in the application. The difference between the two masterpages is...
8
by: Randy Smith | last post by:
Hi, I now need to add MasterPages to a number of existing forms, but when I add the code for MasterPage, the MasterPage does NOT appear when it runs. Any thoughts? TIA, Randy Smith
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.