Multiple CSS files - proper way to do this when you have a master page?

H

Homer J. Simpson

Hi all,

I have enough experience with HTML/classic ASP to get by, and I'm trying to
learn ASP.NET.

Traditionally, I've taken the habit of breaking out extra-long CSS files
into multiple, smaller ones, and referring to them in my HTML/ASP files on
an as-needed basis. Essentially, I've organized things as:

/default.asp
/somepage.asp
/common.css
/area1 (folder)
/area1/default.asp
/area1/somepage.asp
/area1/someotherpage.asp
/area1/area1.css
/area2 (folder)
/area2/default.asp
/area2/somepage.asp
/area2/someotherpage.asp
/area2/area2.css

common.css contains styles that should apply to all pages on my site.
area1.css contains styles that only apply to the files in the area1 folder,
area2.css applies to the files in the area2 folder, etc. Then the
individual files all include /common.css, and the asp files in the
subfolders additionally include the respective area[x].css (only as needed),
using multiple <link href="whatever.css" [...]> tags in the <head> section.
This scheme has served me well for years.

Now...my problem with doing this "the ASP.NET way"...my master page includes
the tag for common.css. This is great as all my content pages automatically
include it. However, I need to include the other area[x].css files on a
page-by-page (content page by content page) basis. The master's already
defined and closed the <head> tag.

I've found out (by accident) that you can put other <link href=...> tags in
the content pages and they'll render in the browser fine, but that confuses
VS2005 (you can't switch to Design view)--a sure sign that this isn't the
way you "should" be doing things.

I'm currently looking at skins/themes, but they seem to bring along their
own set of problems that I'm not ready to sort out just yet--I'd really love
to just be able to have my content pages include their own .css files (not
to override the common.css files, but rather to complement it). Does it
make sense to try to do something in the Page_Load() event? What's the
proper way to do this?

Any guidance appreciated...
 
H

Homer J. Simpson

I hate to reply to my own messages, but I've gotta follow up...

Ok...I've figured out that you can create a theme folder and put multiple
..css files in it, put a reference to the theme in the content page, and then
the styles are automatically available in that page...

Only, I haven't made any reference to any specific .css file--meaning,
apparently, that any aspx file that simply references a theme will use any
and all .css files that exist under that theme's folder. Is that correct?
Can I control that? I'd like to keep organizing things in separate .css
files, but don't necessarily want them *all* automatically included in aspx
pages that don't need them.

Surely the solution isn't to create multiple themes, one for each .css file,
and only use that theme in the page(s) that need it. Obviously I'm working
against how the system is intended to be used...

From what I've been seeing, there's an awful lot of books that introduce
various ASP.NET topics, but very few present things in a "this is how you
now do things if you're from a classic HTML/ASP background" fashion.
Finding the exact keywords on Google to find relevant results for this sort
of thing also seem to escape me...
 
A

Alan Silver

Homer J. Simpson said:
Only, I haven't made any reference to any specific .css file--meaning,
apparently, that any aspx file that simply references a theme will use
any and all .css files that exist under that theme's folder. Is that
correct?

"Correct" meaning that this is the way MS designed it, then yes.

"Correct" meaning that this is sensible, then no.
Can I control that? I'd like to keep organizing things in separate
.css files, but don't necessarily want them *all* automatically
included in aspx pages that don't need them.

One of my long-time gripes with themes. I have tried to find a solution
to this and failed.
Surely the solution isn't to create multiple themes, one for each .css
file, and only use that theme in the page(s) that need it. Obviously
I'm working against how the system is intended to be used...

It wouldn't work anyway. AFAIK, you can only apply one theme to an
application, you can't apply one theme to one folder and another to a
different folder.
From what I've been seeing, there's an awful lot of books that
introduce various ASP.NET topics, but very few present things in a
"this is how you now do things if you're from a classic HTML/ASP
background" fashion.

There are a few, but given that the two are *so* different, you're
actually better off forgetting (almost) everything you learnt about
classic ASP and reading Asp.NET books as though it was something
entirely new.

Read these forums for a bit and see how many people struggle with the
changeover. Most of them suffer from trying to make ASP.NET work in the
classic ASP way. It's far better to make a clean break and treat it as a
new subject entirely.
Finding the exact keywords on Google to find relevant results for this
sort of thing also seem to escape me...

Yeah, that can be a problem too. That's where these groups are so good!

HTH
 
H

Homer J. Simpson

Only, I haven't made any reference to any specific .css file--meaning,
"Correct" meaning that this is the way MS designed it, then yes.

"Correct" meaning that this is sensible, then no.

While I'm learning to appreciate some .NET things, I'm quickly finding that
there's a lot of things I wouldn't have designed the way they are. Well,
that's just MS being MS, I guess...been there, done that.
One of my long-time gripes with themes. I have tried to find a solution to
this and failed.

Hearing this sort of thing isn't particularly encouraging, especially when
this is one of the very first things ever I've tried doing in .NET beyond a
Hello, World page...I've been holding off on learning .NET for a very long
time (I've skipped 1.0 and 1.1 entirely), and now that I'm knee-deep into
it, the reality is such a huge contrast to the endless praise I've been
hearing for years...

Anyway...I don't know if this can be useful to you, coming from a noob such
as myself...I ended up implementing some suggestion I've stumbled upon in
some other forums. For now, I'm willing to forget all about themes.
Essentially, I put a reference to my "base" CSS file in my master page's
<head> as one normally would:

<link href="site.css" rel="stylesheet" type="text/css" />

....followed by an 'empty' one:

<link href="" id="idExtraStyleSheet" rel="stylesheet" type="text/css"
runat="server" />

....then for any page requiring an additional CSS file, I use the following
in its Page_Load() event:

MasterPage m = (MasterPage)Page.Master;
m.ExtraStyleSheetURL = "~/SomePath/UrlToMyAdditionalCssFile.css";

Of course it means I have a bogus "goes-nowhere" <link href=""> tag if I
have a page that *doesn't* need an additional CSS, but that's not causing
any problem.

I'll try to figure out the equivalent "MS-way" later. I'm not holding my
breath though since there doesn't appear to be a short answer to this one.
There are a few, but given that the two are *so* different, you're
actually better off forgetting (almost) everything you learnt about
classic ASP and reading Asp.NET books as though it was something entirely
new.

While I don't necessarily disagree, the intent behind that observation was
just to suggest that I'm convinced there's a market for it. How many
long-time classic ASP coders have moved on to ASP.NET and would buy a book
to assist with the transition, as opposed to the endless supply of help-file
type of books?
Read these forums for a bit and see how many people struggle with the
changeover. Most of them suffer from trying to make ASP.NET work in the
classic ASP way. It's far better to make a clean break and treat it as a
new subject entirely.

It's really a matter of using the right tool for the right job... On one
hand, if something is doable in ASP.NET but is completely awkward, then
that's an indication that I (as a .NET newbie) don't fully understand the
how and the why. On the other, I'd rather stick with the tried-and-true
methods where it makes sense, and not do absolutely everything "the .NET
way" just because this is what MS suggests.

For one--and it's still early in my learning process--I find that ASP.NET
generates *way* more inline styles (as tag attributes) than I would ever
hand-code, especially when "the old method" can be rewritten in a much
cleaner CSS file in a line or two. I find having some server control
attributes in an aspx file, along with separate CSS files, really confuses
matters. And then, of course, not all attributes you can put in a CSS file
can be specified as server control attributes either, so I really have no
choice but to have some styles specified in one location, and others
specified elsewhere...I feel I'm stepping backwards.
Yeah, that can be a problem too. That's where these groups are so good!

Yeah, but my gripe is that it's often quicker to find a relevant answer and
get right back to work than posting a new message and waiting for a
response. No offense to anyone here--I'm a long-time Usenet user...it's
just the nature of things...
 
A

Alan Silver

One of my long-time gripes with themes. I have tried to find a solution to
this and failed.

Hearing this sort of thing isn't particularly encouraging, especially when
this is one of the very first things ever I've tried doing in .NET beyond a
Hello, World page...I've been holding off on learning .NET for a very long
time (I've skipped 1.0 and 1.1 entirely), and now that I'm knee-deep into
it, the reality is such a huge contrast to the endless praise I've been
hearing for years...[/QUOTE]

Maybe I should qualify the comment by saying that having done ASP.NET
for a couple of years now, it is *way* better than classic ASP. Once you
get into the swing of it, you wonder how you ever did classic at all. I
look back at old sites and cringe at how badly they were implemented.
That's partly my fault and partly MS's. ASP.NET does encourage you to
make a complete separation between code and output (ie the (X)HTML), and
that's a very powerful and positive step forwards.

I could list grips I had with classic ASP was well. You just happen to
have hit one I have with ASP.NET. Themes are still a great idea, they
just need some improvement. Even as they are, they are a load better
than doing the same idea yourself.
Anyway...I don't know if this can be useful to you, coming from a noob such
as myself...I ended up implementing some suggestion I've stumbled upon in
some other forums. For now, I'm willing to forget all about themes.
Essentially, I put a reference to my "base" CSS file in my master page's
<head> as one normally would:
<snip>

Thanks for the idea. It's worth considering. You would need some central
mechanism for controlling which page got which CSS file though,
otherwise you would end up with a real mess.
While I don't necessarily disagree, the intent behind that observation was
just to suggest that I'm convinced there's a market for it. How many
long-time classic ASP coders have moved on to ASP.NET and would buy a book
to assist with the transition, as opposed to the endless supply of help-file
type of books?

Dunno. I tried adapting my classic ASP way of thinking, then realised
that it was confusing rather than helping. I bought ASP.NET Unleashed by
Steven Walther and treated myself as a beginner. It was one of the best
books of it's type I ever read and got me going fast. I still use it
sometimes.
It's really a matter of using the right tool for the right job... On one
hand, if something is doable in ASP.NET but is completely awkward, then
that's an indication that I (as a .NET newbie) don't fully understand the
how and the why. On the other, I'd rather stick with the tried-and-true
methods where it makes sense, and not do absolutely everything "the .NET
way" just because this is what MS suggests.

Trouble is, the "tried and true" method for classic ASP is often
completely the wrong method for ASP.NET and vice-versa. That's why I
suggested approaching ASP.NET as a newbie, it avoids the problem of
thinking you know the best way to do something, when really the way you
have in mind might be completely inappropriate for ASP.NET. It really is
a new (but highly (superior) way of thinking.
For one--and it's still early in my learning process--I find that ASP.NET
generates *way* more inline styles (as tag attributes) than I would ever
hand-code, especially when "the old method" can be rewritten in a much
cleaner CSS file in a line or two. I find having some server control
attributes in an aspx file, along with separate CSS files, really confuses
matters. And then, of course, not all attributes you can put in a CSS file
can be specified as server control attributes either, so I really have no
choice but to have some styles specified in one location, and others
specified elsewhere...I feel I'm stepping backwards.

Hmm, sounds more like a structure issue than a problem with the way
ASP.NET handles it. I'd need to see a more concrete example before I
could say though.

I do agree about some of the markup produced by the framework. I quickly
learned never to use the Label control as it creates extra markup you
just don't need. My code tends to have things like...

<h2><asp:Label ID="fred" runat="server" /></h2>

....so I can just set the value of the literal, and know that the markup
will be clean.

I do all of my presentation with CSS, so the .aspx file tends to be very
vanilla (X)HTML and nothing more. That keeps the separation between
content and presentation very well, making better structured pages and a
much easier implementation of themes.
Yeah, but my gripe is that it's often quicker to find a relevant answer and
get right back to work than posting a new message and waiting for a
response. No offense to anyone here--I'm a long-time Usenet user...it's
just the nature of things...

Yup, you need to do both.

Ta ra
 
H

Homer J. Simpson

Oh, don't get me wrong--it took me no time at all to realize there's no
going back to classic ASP. Everything's such a kludge, I don't miss it at
all.
Thanks for the idea. It's worth considering. You would need some central
mechanism for controlling which page got which CSS file though, otherwise
you would end up with a real mess.

As far as my current needs go, this is more than 'good enough'. A lot of my
CSS files are mapped to a single .aspx (eg, to override styles inherited
elsewhere for that page only, or to style a control that is unique to that
page), so I don't think this is gonna be much of an issue. Of course, I was
never involved with sites with more than a dozen pages. Learning any of
this is more of a side-line for me.
Dunno. I tried adapting my classic ASP way of thinking, then realised that
it was confusing rather than helping. I bought ASP.NET Unleashed by Steven
Walther and treated myself as a beginner. It was one of the best books of
it's type I ever read and got me going fast. I still use it sometimes.

I did approach this as a beginning myself, but I still can't help thinking
"I know how to do X, now how should it be done in ASP.NET"...this is what
I'd like to see in some book or tutorial.
Trouble is, the "tried and true" method for classic ASP is often
completely the wrong method for ASP.NET and vice-versa.

"Tried and true" might have been a poor choice of words on my part--there's
an awful lot of ugly hacks that we all came up with out of necessity, and
they should be dropped at the first chance...
That's why I suggested approaching ASP.NET as a newbie, it avoids the
problem of thinking you know the best way to do something, when really the
way you have in mind might be completely inappropriate for ASP.NET. It
really is a new (but highly (superior) way of thinking.

I don't disagree. But let me ask you this: Do you get the sense that MS is
trying to get ASP.NET developers to drop style sheets and go 100%
themes/skins instead? And if so, is sticking to style sheets "working
against the framework"? I'm starting to think I just need more examples.
Most books barely scratch the surface of this topic, or are little more than
references.
Hmm, sounds more like a structure issue than a problem with the way
ASP.NET handles it. I'd need to see a more concrete example before I could
say though.

Meh. It's not such a big issue, all things considered-- I'd rather
concentrate on learning the more important features for now and get back to
this later.
I do all of my presentation with CSS, so the .aspx file tends to be very
vanilla (X)HTML and nothing more. That keeps the separation between
content and presentation very well, making better structured pages and a
much easier implementation of themes.

Great--that's what I'd try to do also. I guess it sorta answers my question
above as well...
 
A

Alan Silver

Homer J. Simpson said:
I don't disagree. But let me ask you this: Do you get the sense that
MS is trying to get ASP.NET developers to drop style sheets and go 100%
themes/skins instead? And if so, is sticking to style sheets "working
against the framework"?

No, I don't get that impression. I have only used skins once. They are
OK, but I prefer to handle it all in style sheets. AFAICS, MS are quite
happy for people to use stylesheets only, I don't think they are pushing
us into skins.
I'm starting to think I just need more examples. Most books barely
scratch the surface of this topic, or are little more than references.

That's certainly true. I guess with al the other stuff in ASP.NET, this
gets pushed to the side a little.

One of the problems is that most ASP.NET developers are programmers, not
designers. CSS, themes and skins are really aimed at designers, so they
don't get as much attention as the coding.

Ta ra
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,817
Latest member
DicWeils

Latest Threads

Top