Thursday, June 05, 2008

dynamic CSS - its a start

Thanks to Christophe @ ( for making my afternoon a little more productive. His blog post about injecting variables into CSS is simplicity at its best.

I had to add one addition to the sample to get it to work on the CSS page (css.aspx) I changed :

<%@ Page Language="VB" %>


<%@ Page Language="VB" ContentType="text/css" %>

the full source code for my VB version is here:

--> css.aspx
<%@ Page Language="VB" ContentType="text/css" %>
* { padding:0; margin:0;}
body { font-size:62.5%; background-color:rgb(255,255,255); font-family:verdana,arial,sans-serif;}
h1{ color:red;}
H1 { background-color:<%= ColourManager.Color %> ; }

--> default.aspx
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="dynamicCSS_Default" %>

< script runat="server">
Protected Sub Bt1_Click(ByVal sender As Object, ByVal e As EventArgs)
ColourManager.Color = IIf((ColourManager.Color = "red"), "blue", "red")
End Sub

< html xmlns="">
< head runat="server">
< title>Untitled Page
< link href="css.aspx" type="text/css" rel="Stylesheet">
< /head>
< body>
< form id="form1" runat="server">
< div id="header">
< /div>
< h1 class="tit">hello world
< asp:button runat="server" id="Bt1" onclick="Bt1_Click" text="Change Color">
< asp:hyperlink text="See CSS" runat="server" id="ToCSS" navigateurl="css.aspx">
< /form>
< /body>
< /html>

-->colourchanger (in app_code folder)

Imports Microsoft.VisualBasic

Public Class ColourManager
Public Shared Property Color() As String
If HttpContext.Current.Session("H1COLOR") = Nothing Then
Return "red"
End If
Return DirectCast(HttpContext.Current.Session("H1COLOR"), String)
End Get
Set(ByVal value As String)
HttpContext.Current.Session("H1COLOR") = value
End Set
End Property
End Class

No comments: