INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

Formatting

Generating CSS using ASP by kavius
Posted: 10 Dec 02 (Edited 1 Aug 03)

What this does is create a css script dynamically based on a parameter. This could be used to allow users to define or choose their own color scheme based on personal preference or different companies, using the same interface could be given different color layouts.

The styles can then be placed into a relational database for later use.

A friend of mine (GumB on tek-tips) has also used this concept as an interface between a COBOL front-end and an oracle database. The front-end was built using COBOL and requires a flat file as input, wanting to upgrade the back-end for ease of processing, but having a perfectly good front-end that the users were already familiar with, he built an interface between the front-end and the Oracle database. To be fair, he used JSP since he is a Java nut case, but to do something like this ANY server side scripting tool will do (ASP, JSP, CGI,...).

Step 1: Create Table
This could actually be three or four tables, but for this example:

create table cssClass(
  style as varchar(20),
  class as varchar(20),
  precedence as int
)

insert into css values('DEFAULT','a',1)
insert into css values('DEFAULT','a:active',2)
insert into css values('DEFAULT','a:hover',2)

create table cssProp(
  style as varchar(20),
  class as varchar(20),
  property as varchar(50),
  value as varchar(50)
)

insert into css values('DEFAULT','a'       ,"color"          ,"#660033")
insert into css values('DEFAULT','a'       ,"text-decoration","none")
insert into css values('DEFAULT','a:active',"text-decoration","underline")
insert into css values('DEFAULT','a:hover' ,"text-decoration","underline")

I think its important to point out that this DB structure has other uses as well. This design is a basic "property bag". It could be used to store any number of properties for any number of reasons. I have used this before to contain properties for Applications written in C, VB, Java, and now ASP.

Step 2: CSS Script
All this code does is dump the values to the data stream, formatted as valid CSS. Note the 3rd or 4th line down (response.contentheader = "text/css") this line is important for servers to know what they are seeing. It also assists the browser in identifying what it is looking at.

Changing the content header will also allow you to send other types of data including images or sounds.

<%
option explicit

'change the content header (from "text/html" to "text/css")
Response.ContentType = "text/css"

'declarations
dim curClass
dim style
dim sql
dim rs

'initialize
curClass = ""
style = request.QueryString("style")
set rs = server.createobject("ADODB.Recordset")

'build sql statement
sql = "select class, " & vbnewline & _
      "       property, " & vbnewline & _
      "       value " & vbnewline & _
      "from   cssClass c, " & vbnewline & _
      "       cssProp p " & vbnewline & _
      "where  c.style = p.style and " & vbnewline & _
      "       c.class = p.class and " & vbnewline & _

if(style = "")then
  sql = sql & "       style = 'default' " & vbnewline
else
  sql = sql & "      style = '" & style & "' " & vbnewline
end if

sql = sql & "order by p.precedence, c.class " & vbnewline

'open recordset
rs.cursorlocation = 3    'adUseClient
rs.open sql, Application("cnnStr")
set rs.activeconnection = nothing

'loop through each property
while(not rs.eof)
  'if the class has changed with the new record
  if(currClass <> rs("class").value)then
    'if its not the very first record
    if(currClass <> "")then
      'close the old CSS class
      response.write("}" & vbnewline & vbnewline)
    end if
    'change which class we are working on
    currClass = rs("class").value
    'open the new class
    response.write(currClass & "{" & vbnewline)
  end if
  'note the property and its value
  response.write(vbtab & rs("property").value & " : " & rs("value").value & ";" & vbnewline )
  
  rs.movenext
wend
'close the last class
response.write("}" & vbnewline)

'clean up
rs.close
set rs = nothing

response.end
%>



Back to Microsoft: ASP (Active Server Pages) FAQ Index
Back to Microsoft: ASP (Active Server Pages) Forum

My Archive

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close