Groups | Blog | Home
all groups > asp.net > august 2007 >

asp.net : master pages and css



Sir Psycho
8/28/2007 11:55:09 PM
Hello

I understand how master pages work with containers.

How would i go about having a stylesheet for one of my containers in a
different aspx file considering that the <head> element is in the
master file? Putting it in the master file means it will be included
for all.

once using master pages, aspx files only have this

<asp:Content ID="ContentSiteBody" ContentPlaceHolderID="SiteBody"
Runat="Server">

any help would....help :)
Alexey Smirnov
8/29/2007 12:23:33 AM
[quoted text, click to view]

you can use following technique

Style s = new Style();
s.BackColor = System.Drawing.Color.Red;
Page.Header.StyleSheet.CreateStyleRule(s, this, "BODY");

this would change the background of your content page

http://groups.google.com/group/microsoft.public.dotnet.framework.aspnet/browse_thread/thread/cd61cb0ba720710d
vMike
8/29/2007 9:09:51 AM

[quoted text, click to view]
Another method would be to create a writeable property in your master page
which allows you to set the css for the page being loaded. I have done this
for metatags and titles also.

Mike

gerry
8/29/2007 9:22:53 AM
http://www.pluralsight.com/blogs/ted/archive/2005/08/31/14437.aspx


[quoted text, click to view]

Alan Silver
8/30/2007 12:00:00 AM
In article <#6Gx1#j6HHA.464@TK2MSFTNGP02.phx.gbl>, gerry
<germ@nospam.nospam> writes
[quoted text, click to view]

No disrespect to anyone, but I don't see why people make such heavy work
out of adding stylesheets, meta tags, etc when using master pages.

Sure you can jump through hoops like this, but it's way easier just to
have a contentplaceholder in the <head> section of the (X)HTML in the
master page, then on content pages where you want to something, you just
use a content tag and insert the code.

For example, the top of a master page could look like this...

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head runat="server">
<title>Untitled Page</title>
<asp:ContentPlaceHolder ID="cplHead" runat="server" />
</head>
<body>

.....and the content page would include this...

<asp:Content ContentPlaceHolderID="cplHead" runat="server">
<link href="fred.css" ..../>
<meta name="ferret" content="furry" />
</asp:Content>

This is *much* easier than using the ASP.NET objects to modify the
<head> programmatically.

HTH

--
Alan Silver
Vince
8/30/2007 6:31:23 AM
On Aug 30, 6:43 pm, Alan Silver <alan-sil...@nospam.thanx.invalid>
[quoted text, click to view]

Thats a nice and clean approach. Thank you Alan!
vMike
8/30/2007 11:16:24 AM

[quoted text, click to view]

No disrespect taken. I think it depends a lot on how much flexibly you
need. If it you are trying to build an app that is going to have a lot of
flexibility, I prefer properties.

Mike


vMike
8/30/2007 1:10:57 PM

[quoted text, click to view]

I find it easier to code. I admit that setting the style sheet is not the
best example for using properties. I use it for setting menus, titles and
body attributes. Maybe it is just a preference thing.

Mike

Alan Silver
8/30/2007 4:56:15 PM
In article <_aBBi.3995$Y7.2146@bignews3.bellsouth.net>, vMike
<Michael.George@5nerrawgenospam.com> writes
[quoted text, click to view]

Out of interest, why? Having the <head> stuff inside a content tag still
allows flexibility. I showed a static way to do it, but there's nothing
wrong with putting a Literal control in the <head> content tag and
setting the stylesheet dynamically. Works the same as a property, but
much simpler IMO.

I'd be interested to hear why you prefer properties.

Ta ra

--
Alan Silver
Alan Silver
8/30/2007 6:47:37 PM
In article <nSCBi.24444$7e6.10911@bignews4.bellsouth.net>, vMike
<Michael.George@5nerrawgenospam.com> writes
<snip>
[quoted text, click to view]

OK, for titles and body attributes it could be sensible, although I
often bung a Literal in the <title> tag and do the same thing! Horses
for courses I guess.

Ta ra

--
Alan Silver
Alexey Smirnov
8/31/2007 1:34:20 AM
On Aug 30, 7:47 pm, Alan Silver <alan-sil...@nospam.thanx.invalid>
[quoted text, click to view]

I think this is just a preference thing and a matter of the
architecture. I can say, if MasterPage has to be different for
different sets of content pages, than it is not really a "master". In
this case you can also have MasterPage2 with different CSS on it and
you can use it for that set of content pages where the style should be
different.
Alan Silver
8/31/2007 11:31:59 AM
In article <1188549260.979291.109750@i13g2000prf.googlegroups.com>,
Alexey Smirnov <alexey.smirnov@gmail.com> writes
[quoted text, click to view]

Could be, although I have had cases where one particular page required
extra CSS, and I didn't want to include that in the CSS for every page,
so I just added an extra link in the header for that one page. Certainly
wasn't worth doing a new master page for just that.

Ta ra

--
Alan Silver
AddThis Social Bookmark Button