CSS is simple. As a web developer, we can do the styling by creating a css file. However, some might have faced the problem of styling pages in a dynamic manner. Example, if you want the CSS to display differently when different people logs in. Or you want to programmatically change it depending on which day it is on.

In this article, I will try my best to explain it in 3 simple steps.  :)

Step 1: Create an aspx file, let's name it as "DynamicCss.aspx". Remove the additional script and leave the script as below sample code.

<%@ Page Language="VB" AutoEventWireup="false"
CodeFile="DynamicCss.aspx.vb" Inherits="DynamicCss" %>
<asp:Literal ID="ltlDynamicCSS" runat="server" />

Step 2: Generate the css script from its code behind with the QueryString values. (considering you have done all the validation with the Query String value before used)

Dim sbDynamicCss As New StringBuilder

With sbDynamicCss
            ' General style
           .Append(".msg { padding:3px; color:#" & Request.QueryString("color").ToString & "; } ")
           .Append(".pointer { cursor: pointer; } ")
           .Append(".common { font-size: 8pt; font-family: Verdana; } ")
End With
Me.ltlDynamicCSS.Text = sbDynamicCss.ToString

Step 3: Create another aspx file as "Default.aspx". Normally we includes a htmllink from html header tag. For this case, I will generate htmllink from code behind when page load. This is much more flexible that you may send in the attribute value using query string.

Dim csslink As New HtmlLink
csslink.Attributes.Add("type", "text/css")
csslink.Attributes.Add("rel", "stylesheet")
csslink.Attributes.Add("href", "DynamicCss.aspx?color=#f0f0f0")
Me.Head1.Controls.Add(csslink)

That's all! You have generated a dynamic css. Of course you got to assign a css class to a control from the Default.aspx file. For example:

<div id="div1" class="msg">Hello World</div>




0 del.icio.us



6 comments
  1. parag December 4, 2008 6:49 AM  

    I have tried the same steps but it is not working for me

  2. Lasker December 4, 2008 10:09 AM  

    Hey Parag,

    Which part is not working? Let us know what did you do and what did you see so that we can help you :)

  3. parag December 5, 2008 12:14 AM  

    I have created DynamicCSS.aspx page and added the code as per step 1. For step 2 I have added code in page_init of the code behind. It retrives style information from database and creating styles on similar lines of step 2. I have added step3 code on the page where i want to add CSS dynamically. I have applied few of the classes to elements on the page.

    I can't see CSS getting applied to elements on the page. It looks like I am missing something don't know. When I have put the degub control on page_init of DynamicCSS.aspx.cs it is not getting invoked :(

    Thanks a lot in advance for your time and help

  4. Elise December 6, 2008 2:54 PM  

    Hi Parag, it is best that to generate the Step 3 code at page load event.

    I suspect that during page init event, the data is not loaded properly to the htmllink control.

    You may refer to http://msdn.microsoft.com/en-us/library/ms178472.aspx to understand more about the ASP.NET Page Life-Cycle stages.

    Hope its help :)

  5. parag December 10, 2008 2:08 AM  

    I did that but still it is not working. I got it working by setting AutoEventWireup=true in DynamicCss.aspx. Some how I have try to rerun it again and it is not working. Any Clue?

  6. parag December 11, 2008 2:04 AM  

    I have found out that it doesn't work if "Pages" theme is set to default in web.config. So i am able to make it run by deleting Theme="default" from Pages tag

    Also this approach doesn't work in firefox? Any suggestion for it?