jbwharris
jbwharris

Reputation: 720

Dynamic CSS page using Classic ASP

I've been trying to figure out how to pull in a dynamic CSS stylesheet using classic asp. I've read quite a few tutorials on the subject and I can't seem to get it to pull in properly. Many of them seem to imply that just changing to styles.asp or .aspx and referencing it using the standard stylesheet link will work, but I'm not getting that result.

https://web.archive.org/web/20211020131223/https://www.4guysfromrolla.com/webtech/tips/t071201-1.shtml

What I'm trying to achieve is being able to pull in server side variables from our CMS at work into my stylesheet. I realize that SASS and LESS exist and might be able to be adapted, but I'm just trying to find a simple way to use asp variables and pull them into my stylesheet. I'm not super well versed in ASP, so any help you could provide would be helpful.

Edit: I've updated the code below to reflect the working code.

HTML

<link rel="stylesheet" href="<% = TemplatePath %>css/styles.asp" type="text/css" />

ASP CSS page

<%
   dark_color = "navy" 
%>

<% Response.ContentType = "text/css" %>
<style type="text/css">
   h2 { color: <%= dark_color %> }
</style>

Upvotes: 2

Views: 5599

Answers (2)

Umesh Chaurasiya
Umesh Chaurasiya

Reputation: 173

    you could use global variables like Application and session which are accessible across overall application.

    here are the codes for your reference -

    my asp page named asp_1.asp

    <!DOCTYPE html>
    <html>
    <head>

         <link rel="stylesheet" type="text/css" href="dynastyle.asp">
    </head>
    <body>
    <%
    Set MyBrow=Server.CreateObject("MSWC.BrowserType")

    Application("myfontcolor") = "#ff0000"

    %>

    <table border="0" width="100%">
    <tr>
    <th>Client OS</th><th><%=MyBrow.platform%></th>
    </tr><tr>
    <td >Web Browser</td><td ><%=MyBrow.browser%></td>
    </tr><tr>
    <td>Browser version</td><td><%=MyBrow.version%></td>
    </tr><tr>
    <td>Frame support?</td><td><%=MyBrow.frames%></td>
    </tr><tr>
    <td>Table support?</td><td><%=MyBrow.tables%></td>
    </tr><tr>
    <td>Sound support?</td><td><%=MyBrow.backgroundsounds%></td>
    </tr><tr>
    <td>Cookies support?</td><td><%=MyBrow.cookies%></td>
    </tr><tr>
    <td>VBScript support?</td><td><%=MyBrow.vbscript%></td>
    </tr><tr>
    <td>JavaScript support?</td><td><%=MyBrow.javascript%></td>
    </tr>
    </table>

    </body>
    </html> 

    here i displayed simple HTML table to display browsers capability. you could use any element as per your requirement.

another steps towards the solution is to create asp page with dynamic css named dynastyle.asp. here is the code for same -

<% Response.ContentType = "text/css" %>
<%
DIM fontColor
**fontColor =Application("myfontcolor")**
%>

    table

{
    background-color: <%= fontColor %>;
} 

Upvotes: 0

Chris Nielsen
Chris Nielsen

Reputation: 14758

The missing ingredient here was the content type. Classic ASP pages are served as text/HTML by default, which confuses browsers that expect stylesheets to be text/css.

Changing the content type is done like so:

Response.ContentType = "text/css"

MSDN documentation

Upvotes: 2

Related Questions