Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / ASP.NET / General / March 2008

Tip: Looking for answers? Try searching our database.

MasterPages and applying CSS to content pages

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Mort Strom - 01 Mar 2008 04:19 GMT
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 <style> tag 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 <header> tag?

Thanks for any direction,
Mort
Mark Rae [MVP] - 01 Mar 2008 11:56 GMT
> 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 <header> tag?

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);

Signature

Mark Rae
ASP.NET MVP
http://www.markrae.net

Mort Strom - 01 Mar 2008 14:27 GMT
Well done!  This is exactly the kind of solution I was looking for.
I'm new to MasterPages.  Thank you

>> How do I programmatically apply styles in my MasterPage based on the
>> ContentPlaceHolder page that's being shown?
[quoted text clipped - 22 lines]
> objCSS.Attributes.Add("type", "text/css");
> Header.Controls.Add(objCSS);
Mort Strom - 01 Mar 2008 14:39 GMT
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)?

>> How do I programmatically apply styles in my MasterPage based on the
>> ContentPlaceHolder page that's being shown?
[quoted text clipped - 22 lines]
> objCSS.Attributes.Add("type", "text/css");
> Header.Controls.Add(objCSS);
Mark Rae [MVP] - 01 Mar 2008 15:11 GMT
> 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...

Signature

Mark Rae
ASP.NET MVP
http://www.markrae.net

Mort Strom - 02 Mar 2008 04:22 GMT
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

>> If objPage1CSS is added to the Header for Content Page1.aspx, then
>> objPage2CSS added for Content Page2.aspx, will the Header tag retain
[quoted text clipped - 19 lines]
> 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...
BobF - 01 Mar 2008 15:04 GMT
> 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
[quoted text clipped - 10 lines]
> 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.
Mark Rae [MVP] - 01 Mar 2008 15:15 GMT
> 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...

Signature

Mark Rae
ASP.NET MVP
http://www.markrae.net

BobF - 01 Mar 2008 20:31 GMT
>> Mort, I see you've been provided a rather elegant solution, but I'm
>> curious why not use a separate style sheet?
[quoted text clipped - 10 lines]
> 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.

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.