2

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>
6
  • I'd need to see code in order to offer advice. Feb 22, 2013 at 16:21
  • do they need to be in a stylesheet? can they be inline style or within style tags in the header?
    – Pete
    Feb 22, 2013 at 16:31
  • @Pete Ideally I wanted them in the stylesheet. I was hoping to integrate it heavily into my existing stylesheet, so that I could pull dynamic colours from the CMS and alter and manipulate them in the CSS. I'm just trying not to overcomplicate my question with additional details for the sake of clarity.
    – jbwharris
    Feb 22, 2013 at 16:34
  • Try setting Response.ContentType = "text/css" in the ASP page and see if that helps. Feb 22, 2013 at 16:36
  • @ChrisNielsen Awesome, that looks to be what my issue was. Would you like to add that as an answer below and I can give you credit for the solution?
    – jbwharris
    Feb 22, 2013 at 16:40

2 Answers 2

2

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

0
    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 %>;
} 

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.