Accidental Technologist

Musings about Entrepreneurship, Technology and Software Development

  • Home
  • About
  • Still River Software
  • Privacy Policy

Powered by Genesis

You are here: Home / Programming / Easy Dynamic Style Sheets in HMTL Pages using ASP.NET

Easy Dynamic Style Sheets in HMTL Pages using ASP.NET

October 21, 2008 by Rob Bazinet

Tweet

One of my current projects consists of a lot of existing static HTML documents that I need to work with and integrate with ASP.NET.  The idea is to change as few of the static HTML files as possible as they are fine the way they are but allow new ASP.NET applications to add some dynamic dimensions to somewhat older systems.

The Problem

A particular task I faced was to be able to take a regular link statement from HTML like this:

<link rel="stylesheet" type="text/css" href="/shared/styles/style.css" />

And be able to dynamically replace the style.css file with some other custom style sheet.  The requirement is the document could not be converted to an ASPX page and needed to remain a .html file.

This is actually a very small problem in the grand scheme of things but is one example of dealing with legacy systems and trying to find the least disruptive and most cost-effective solution to a problem.

The Solution

There is probably a clever way to solve this problem using JavaScript but our solution needed to be solved using ASP.NET.  My normal approach to solving a problem of this nature is first assume it has been solved before and do a few Google searches trying to find the answers.   This time my searches returned no viable solutions using the tool (ASP.NET) that I am using on this project, so I needed to solve this the best way I could find.

The solution I built in ASP.NET allows for many different style sheets to be used, one per client, to help “skin” our web pages.  I have some logic which figures out which CSS file to use based on who the client is and load up that style sheet.  The solution to this problem leverages this by creating a “faceless” ASPX page to help out.

The page is very simple and is called Customstyle.aspx.  This page does something very simple in the Page_load:

protected void Page_Load(object sender, EventArgs e)
{
Response.Redirect("/shared/styles/" + CurrentStyleSheet);
}

This page is then called from our original static page, making a very small change to the HTML file by changing the original LINK to this:

<link rel="stylesheet" type="text/css" href="Customstyle.aspx" />

When our HTML page is loaded in the browser the Customstyle.aspx page will return and inject the proper CSS file into our HTML, giving us the desired solution.

There may be several ways to solve this same problem but after many tries this seemed to be the simplest approach.  In all of my years of ASP.NET development, this is the first time I needed to use it this way.

Technorati Tags: ASP.NET,Legacy Systems,HTML,CSS

Share this:

  • LinkedIn
  • Twitter
  • Facebook
  • Email
  • More
  • Pinterest
  • Tumblr
  • Pocket
  • Reddit

Related

Filed Under: Programming

Care about your privacy? I do and use Fathom Analytics on this site.

Fathom Analytics

Comments

  1. Gemunu says

    January 8, 2009 at 1:59 am

    Wow…I had the same reqirement and implemented your invention to realize it beautifully. Simple yet smart and amazing finding by you. Keep up the good work.
    Thanks again.

  2. Gemunu says

    January 8, 2009 at 1:59 am

    Wow…I had the same reqirement and implemented your invention to realize it beautifully. Simple yet smart and amazing finding by you. Keep up the good work.

    Thanks again.

  3. Sulumits Retsambew says

    August 7, 2009 at 9:00 am

    Thanks for the info.

  4. Sulumits Retsambew says

    August 7, 2009 at 9:00 am

    Thanks for the info.

Recent Posts

  • Overcoming the Mimemagic Fiasco
  • Redis::CommandError – MISCONF Redis is configured to save RDB snapshots
  • frozen_string_literal: the not so magical comment
  • Status Bar in iTerm2
  • Supporting Multiple SSH Keys on macOS

Categories

Services I Love

HatchBox - Easy Rails Deploys Fathom Analytics
Follow @rbazinet

Rob Bazinet
@rbazinet

  • Apple’s trade-in program is a joke. I’ve heard multiple people saying their value lowered for made up reasons - https://t.co/bZK4rqvUpW
    about 2 days ago
  • I didn’t realize so many Pragmatic Programers titles were available to read online - https://t.co/OrLr01QCip
    about 4 days ago
  • Starts Monday. https://t.co/BkmcjnMRZS
    about 6 days ago
  • This years @railsconf line up of talks looks so good this year - https://t.co/eOwq9qlxs4. I wish it was in-person t… https://t.co/nt7nxV35OH
    about 1 week ago
  • RSS - Posts
  • RSS - Comments
loading Cancel
Post was not sent - check your email addresses!
Email check failed, please try again
Sorry, your blog cannot share posts by email.