473,748 Members | 3,107 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 ContentPlaceHol der. 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 programmaticall y apply styles in my MasterPage based on the
ContentPlaceHol der page that's being shown?

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

Thanks for any direction,
Mort

Mar 1 '08 #1
8 11790
"Mort Strom" <ma**@rocksdrea m.comwrote in message
news:89******** *************** ***********@mic rosoft.com...
How do I programmaticall y apply styles in my MasterPage based on the
ContentPlaceHol der page that's being shown?

Or another way to ask it - how do I apply styles to ContentPlaceHol der
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.ForeCo lor = System.Drawing. Color.Yellow;
Header.StyleShe et.CreateStyleR ule(objStyle, null, "td");

or

HtmlLink objCSS = new HtmlLink();
objCSS.Attribut es.Add("href", "~/css/DifferentStyle. css");
objCSS.Attribut es.Add("rel", "stylesheet ");
objCSS.Attribut es.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**@markNOSPA Mrae.netwrote in message
news:#v******** ******@TK2MSFTN GP06.phx.gbl...
"Mort Strom" <ma**@rocksdrea m.comwrote in message
news:89******** *************** ***********@mic rosoft.com...
>How do I programmaticall y apply styles in my MasterPage based on the
ContentPlaceHo lder page that's being shown?

Or another way to ask it - how do I apply styles to ContentPlaceHol der
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.ForeCo lor = System.Drawing. Color.Yellow;
Header.StyleShe et.CreateStyleR ule(objStyle, null, "td");

or

HtmlLink objCSS = new HtmlLink();
objCSS.Attribut es.Add("href", "~/css/DifferentStyle. css");
objCSS.Attribut es.Add("rel", "stylesheet ");
objCSS.Attribut es.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**@markNOSPA Mrae.netwrote in message
news:#v******** ******@TK2MSFTN GP06.phx.gbl...
"Mort Strom" <ma**@rocksdrea m.comwrote in message
news:89******** *************** ***********@mic rosoft.com...
>How do I programmaticall y apply styles in my MasterPage based on the
ContentPlaceHo lder page that's being shown?

Or another way to ask it - how do I apply styles to ContentPlaceHol der
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.ForeCo lor = System.Drawing. Color.Yellow;
Header.StyleShe et.CreateStyleR ule(objStyle, null, "td");

or

HtmlLink objCSS = new HtmlLink();
objCSS.Attribut es.Add("href", "~/css/DifferentStyle. css");
objCSS.Attribut es.Add("rel", "stylesheet ");
objCSS.Attribut es.Add("type", "text/css");
Header.Controls .Add(objCSS);
--
Mark Rae
ASP.NET MVP
http://www.markrae.net
Mar 1 '08 #4
"Mort Strom" <ma**@rocksdrea m.comwrote in message
news:89******** *************** ***********@mic rosoft.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 ContentPlaceHol der. 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 programmaticall y apply styles in my MasterPage based on the
ContentPlaceHol der page that's being shown?

Or another way to ask it - how do I apply styles to ContentPlaceHol der
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**@rocksdrea m.comwrote in message
news:35******** *************** ***********@mic rosoft.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.netwrot e in message
news:Ot******** ******@TK2MSFTN GP03.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**@markNOSPA Mrae.netwrote in message
news:uW******** ******@TK2MSFTN GP04.phx.gbl...
"BobF" <rN***********@ charter.netwrot e in message
news:Ot******** ******@TK2MSFTN GP03.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**@markNOSPA Mrae.netwrote in message
news:eB******** ******@TK2MSFTN GP04.phx.gbl...
"Mort Strom" <ma**@rocksdrea m.comwrote in message
news:35******** *************** ***********@mic rosoft.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
1767
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 the content pages. Can this be done and if so how. I have made the Varibles that I wish to access Public Members. Thanks for any help. Kenneth
4
1527
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 loads. In ASP.NET 2.0 can I do the same thing by calling the class on the MasterPage's code-behind before it's Page_Load and will this run before the content page's Page_Load? -- Jeff Lynch
0
423
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 attribute. This works for the content pages, but it doesn't seem able to apply the styles to the top master page even though I did set the master head tag to runat server. Is this by design? If so, what is the best way to apply styles to the top...
2
2467
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 and want to be able to populate the ContentPlaceholders of the inner-most masterpage at run-time.
0
1009
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 for to display the loginstatus and couple of links and and finally the content pages display in the content area of the MasterPage. Now, I have around 10 component pages and all most of all of the pages have the same content to display in the...
3
4317
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 done using the code below. When I add masterpages to Album.aspx and remove all the html tags I get the error System.NullReferenceException: Object reference not set to an instance of an object. for the highlighted yellow line below. I notice once I...
9
2143
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 tags and properties are used in a page without a masterpage. Does anyone shares this exprerience with me or does anyone knows an answer? For the time being, I copy-pased the headings in my masterpage to the clield pages.
6
1978
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 that on the main one I have a user control in the left side that has the site navigation menu while the login/logout masterpage has a non-breaking space in that table cell. Everything else is exactly the same. The issue is that the login/logout...
8
2093
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
0
8987
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8826
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9366
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9241
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6793
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4597
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4867
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3303
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2777
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.