Can I combine ASP with external CSS?

R

Richard Speiss

I am trying to display a random background image for a webpage. I found
this code to do it

<%
'Defines the number of background images you have
Const NUMBER_OF_IMAGES = 2

'Initiates the randomize function
Randomize

'Sets a variable: this will be used to hold the
'random generated value
Dim intImageNumber

'This is where we create the random number
intImageNumber = Int((NUMBER_OF_IMAGES * Rnd) + 1)
%>


Modify the body tag to accept the random number. In this case we are going
to place it within the image so as to facilitate the selection of a random
image.

<BODY BACKGROUND="bg_<%= intImageNumber %>.gif">

This did work.

In my case, I have an external style sheet with this defined:

body {
color:#333;
background-color: gray;
background-image: url(../images/bg_<%=intImageNumber%>.JPG);
background-attachment: fixed;
margin:20px;
padding:0px;
font:11px verdana, arial, helvetica, sans-serif;
}

and it is included in my main page with

<head>
<style type="text/css" media="screen">@import "css/cvs.asp";</style>
</head>

which comes after the above randomizing script

I removed <BODY BACKGROUND="bg_<%= intImageNumber %>.gif"> from the main
page and replaced it with just <body> thinking that since it is defined in
the external CSS file I wouldn't need it on each page.

I just got a gray background. Since I am really new to ASP and CSS I am not
sure the variables are global throughout included files or not. I wanted to
have the background image defined in the style file since it is used
everywhere so rather than modiying <body> for each page I could have it in
one place.

Can this be done this way? Is there a better way to handle this situation?

Many thanks

Richard Speiss
 
E

Evertjan.

Richard Speiss wrote on 08 jun 2004 in
microsoft.public.inetserver.asp.general:
<head>
<style type="text/css" media="screen">@import "css/cvs.asp";</style>
</head>

<link type="text/css" href="css/cvs.asp" rel="stylesheet">
 
R

Roland Hall

in message
: I am trying to display a random background image for a webpage. I found
: this code to do it
:
: <%
: 'Defines the number of background images you have
: Const NUMBER_OF_IMAGES = 2
:
: 'Initiates the randomize function
: Randomize
:
: 'Sets a variable: this will be used to hold the
: 'random generated value
: Dim intImageNumber
:
: 'This is where we create the random number
: intImageNumber = Int((NUMBER_OF_IMAGES * Rnd) + 1)
: %>
:
:
: Modify the body tag to accept the random number. In this case we are going
: to place it within the image so as to facilitate the selection of a random
: image.
:
: <BODY BACKGROUND="bg_<%= intImageNumber %>.gif">
:
: This did work.
:
: In my case, I have an external style sheet with this defined:
:
: body {
: color:#333;
: background-color: gray;
: background-image: url(../images/bg_<%=intImageNumber%>.JPG);
: background-attachment: fixed;
: margin:20px;
: padding:0px;
: font:11px verdana, arial, helvetica, sans-serif;
: }
:
: and it is included in my main page with
:
: <head>
: <style type="text/css" media="screen">@import "css/cvs.asp";</style>
: </head>
:
: which comes after the above randomizing script
:
: I removed <BODY BACKGROUND="bg_<%= intImageNumber %>.gif"> from the main
: page and replaced it with just <body> thinking that since it is defined in
: the external CSS file I wouldn't need it on each page.
:
: I just got a gray background. Since I am really new to ASP and CSS I am
not
: sure the variables are global throughout included files or not. I wanted
to
: have the background image defined in the style file since it is used
: everywhere so rather than modiying <body> for each page I could have it in
: one place.
:
: Can this be done this way? Is there a better way to handle this
situation?

Remove the background-image line from your external CSS and just use it in
an onload.

<body onload="bg_<%= intImageNumber %>.gif">

Perhaps if your CSS was linked in prior to the script running and it was
..css instead of .asp, then perhaps the ASP processor would process that part
before rendering the page to the client. As it is now, and you can test by
loading the cvs.asp by itself and looking at the source, you'll see what is
actually being returned.

--
Roland Hall
/* This information is distributed in the hope that it will be useful, but
without any warranty; without even the implied warranty of merchantability
or fitness for a particular purpose. */
Technet Script Center - http://www.microsoft.com/technet/scriptcenter/
WSH 5.6 Documentation - http://msdn.microsoft.com/downloads/list/webdev.asp
MSDN Library - http://msdn.microsoft.com/library/default.asp
 
R

Richard Speiss

But if I use <body onload ... , then I need to have that piece of code on
each .asp page which is what I was trying to avoid.

If I load cvs.asp by itself then it wouldn't have intImageNumber filled in
since the number is generated by the file that includes it. I guess that's
my question; whether a value generated in a page can be used by something
that is included. Your point is good though. Which is done first? Does
the ASP VB code get executed first or does the style inclusion in the <head>
section.

By doing a little test it looks like intImageNumber doesn't make it from
index.asp to cvs.asp. Or I am still missing something

Thanks

Richard
 
M

Mark Schupp

index.asp and cvs.asp are separate asp scripts so the value of
intImageNumber will not pass from index to cvs. You will need to include the
random background code in the cvs.asp file.

The sequence of execution is:

index.asp is requested by the browser

index.asp is executed on the server and the generated html returned to the
browser. This html will include your style or link tag. Note that at this
time the cvs.asp script has not been called.

browser finds the link tag or style import directive in the html and sends a
request for cvs.asp

cvs.asp is executed on the server and the resulting text is returned (You
will probably have to set response.contenttype to whatever is valid for a
stylesheet in order for the browser to use the styles).

--
Mark Schupp
Head of Development
Integrity eLearning
www.ielearning.com
 
L

Lance Wynn

You can map css pages to be processed through the same engine as the ASP
pages. Then you can just embed your ASP code in the CSS file.



I am trying to display a random background image for a webpage. I found
this code to do it

<%
'Defines the number of background images you have
Const NUMBER_OF_IMAGES = 2

'Initiates the randomize function
Randomize

'Sets a variable: this will be used to hold the
'random generated value
Dim intImageNumber

'This is where we create the random number
intImageNumber = Int((NUMBER_OF_IMAGES * Rnd) + 1)
%>


Modify the body tag to accept the random number. In this case we are going
to place it within the image so as to facilitate the selection of a random
image.

<BODY BACKGROUND="bg_<%= intImageNumber %>.gif">

This did work.

In my case, I have an external style sheet with this defined:

body {
color:#333;
background-color: gray;
background-image: url(../images/bg_<%=intImageNumber%>.JPG);
background-attachment: fixed;
margin:20px;
padding:0px;
font:11px verdana, arial, helvetica, sans-serif;
}

and it is included in my main page with

<head>
<style type="text/css" media="screen">@import "css/cvs.asp";</style>
</head>

which comes after the above randomizing script

I removed <BODY BACKGROUND="bg_<%= intImageNumber %>.gif"> from the main
page and replaced it with just <body> thinking that since it is defined in
the external CSS file I wouldn't need it on each page.

I just got a gray background. Since I am really new to ASP and CSS I am not
sure the variables are global throughout included files or not. I wanted to
have the background image defined in the style file since it is used
everywhere so rather than modiying <body> for each page I could have it in
one place.

Can this be done this way? Is there a better way to handle this situation?

Many thanks

Richard Speiss
 
R

Richard Speiss

Oh that was way too easy. I wasn't thinking that I could actually execute
the VB script inside a style file since it was just declarations but when I
think about it, it does make sense.

Thanks a lot. It's working great now

RIchard
 
A

Aaron [SQL Server MVP]

No, you can't do that, because the CSS file is not included in the
server-side script, it is only sent to the client-side browser for parsing.

You can use a CSS file as ASP:

body {
....
background-image: url(../images/bg_<%=Request.QueryString("i")%>.jpg);
....
}

Then call it like this:

<link rel=stylesheet href=css.asp?i=<%=intImageNumber%>>

Or, you can just put the style inline instead of a linked file.

You lose the benefit of using an external style sheet by making the content
dynamic, but you can keep the same maintenance benefit by using an external
ASP file.
 
A

Aaron [SQL Server MVP]

Oh that was way too easy.

Yes, and you know what they say when a solution is very easy: it probably
isn't the best solution. If you're using a version of IIS prior to 6.0,
this "easy" solution is going to cost you dearly, because every single CSS
file your site serves will be parsed by the ASP engine (and again, there go
the benefits of re-using a linked CSS file).

A
 
R

Richard Speiss

I also just found out that although this worked great for IE and Opera.
Netscape and Firefox didn't see any of the CSS at all. The only way I got
it to work for them was to change the extension of the css file from ASP
which eliminated having the ASP part of the CSS file. 6 of one and half
dozen of another.

I see you have another post here explaining things in more detail. I guess
I will have to do some more reading

Thanks for your input

Richard
 
A

Aaron [SQL Server MVP]

Yes, that's right... perhaps you might have to avoid using server-side code
for Firefox, since IIRC it will only accept CSS extension as CSS. Anything
else that contains style information will be ignored. So, let firefox users
see a single static image. Or, like my other suggestion, keep those styles
that must be dynamic in an inline <style> block (e.g. in an include).

Another idea might be to have background-image:url(somefile.asp);

Then have somefile.asp stream a randomly-selected image.
 
L

Lance Wynn

This isn't really That big of a problem, because most browsers will cache
the css file, and therefore won't request it each time. Even if the request
is made each time a page is loaded, the cost of parsing a file through the
ASP engine likely isn't going to affect performance all that much anyway,
most IIS servers will be able to outperform your internet connection anyway
(Unless you are doing some heavy Database access, or something), however if
it's just an if then else, or case statement you are processing; I doubt
that you'll notice a performance hit at all.

I think "cost you dearly" may be a bit strong. After all, the ASP engine is
there to create dynamic pages, whether they have an extension of .ASP, .HTM,
..CSS, whatever; I say go ahead and use it.

Also, you can map .CSS files to run through the ASP engine within the IIS
Management console. just click the configuration button next to the
application. That way you won't have to call the .CSS file a .ASP file.

Lance



Aaron said:
Oh that was way too easy.

Yes, and you know what they say when a solution is very easy: it probably
isn't the best solution. If you're using a version of IIS prior to 6.0,
this "easy" solution is going to cost you dearly, because every single CSS
file your site serves will be parsed by the ASP engine (and again, there go
the benefits of re-using a linked CSS file).

A
 
A

Aaron [SQL Server MVP]

This isn't really That big of a problem, because most browsers will cache
the css file, and therefore won't request it each time.

If the CSS file is dynamic like ASP, then there will be no difference
between requesting an ASP file and requesting a CSS file. Even if what you
said were true, then doesn't that defeat the purpose of injecting a random
background image? So how does that suggestion help achieve the goal?
I think "cost you dearly" may be a bit strong. After all, the ASP engine is
there to create dynamic pages, whether they have an extension of .ASP, ..HTM,
.CSS, whatever; I say go ahead and use it.

Uh, the ASP engine is not used to process HTM and CSS files unless you tell
it to. And if you tell it to process all CSS files as ASP files, then the
ASP engine will be invoked for all CSS files. Regardless of whether or not
they contain any dynamic code; and regardless of whether or not they were
called from an ASP file. So if you're calling a CSS file from an ASP file,
the "extra" cost might not be that much. But if you have a lot of static
HTML pages that use CSS files, those HTML pages suddenly take significant
resources to render.

Go ahead and map all your CSS files to asp.dll if you like. But I don't
think it's very good advice for people who are trying to get decent
performance out of their servers.

A
 
L

Lance Wynn

This is true, by default, HTML files, and CSS files aren't processed by the
ASP engine, and in most cases they don't need to be, but there are, in some
cases, definite advantages to allowing the ASP engine to process the CSS
files. I'm not, nor did I ever say this is true in all cases, but it is
certainly worth looking into as a viable option.

In fact, you raise a very interesting point. There may be certain cases
where there are static HTML pages, that you want to receive Dynamic Styles
depending on a customer, or a specific situation. In this case, I think
processing the CSS through the ASP engine would be a correct way to go. The
HTML file won't go through the ASP engine, but the CSS will.

Another thing you need to look at is, how many css style sheets do you have
in your site, and of those how many do you want to be able to dynamically
modify? If you have a limited number of style sheets, or if you decide you
want all the style sheets to be able to return dynamic content, then why not
map them to the ASP engine? If you only have 1 out of 50 CSS files that
need dynamic content, I agree with Aaron running all of them through the ASP
engine is probably wasteful.

In the end, it's up to you how you do it. The ASP designers put the
functionally in there to use, and if it fits the bill, I say use it (if it
fits the requirement).



Aaron said:
This isn't really That big of a problem, because most browsers will cache
the css file, and therefore won't request it each time.

If the CSS file is dynamic like ASP, then there will be no difference
between requesting an ASP file and requesting a CSS file. Even if what you
said were true, then doesn't that defeat the purpose of injecting a random
background image? So how does that suggestion help achieve the goal?
I think "cost you dearly" may be a bit strong. After all, the ASP engine is
there to create dynamic pages, whether they have an extension of .ASP, ..HTM,
.CSS, whatever; I say go ahead and use it.

Uh, the ASP engine is not used to process HTM and CSS files unless you tell
it to. And if you tell it to process all CSS files as ASP files, then the
ASP engine will be invoked for all CSS files. Regardless of whether or not
they contain any dynamic code; and regardless of whether or not they were
called from an ASP file. So if you're calling a CSS file from an ASP file,
the "extra" cost might not be that much. But if you have a lot of static
HTML pages that use CSS files, those HTML pages suddenly take significant
resources to render.

Go ahead and map all your CSS files to asp.dll if you like. But I don't
think it's very good advice for people who are trying to get decent
performance out of their servers.

A
 

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

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top