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

  • How to Fix Rails Flash Rendering When Using Hotwire
  • Hotwire Fix for CORS Error when using Omniauth
  • Fix Installation of Ruby using rbenv on macOS Big Sur
  • RailsConf 2021 and the Future of Conferences
  • Fixing Out of Diskspace Errors on Amazon EC2

Categories

Services I Love

HatchBox - Easy Rails Deploys Fathom Analytics
Follow @rbazinet

Rob Bazinet
@rbazinet

  • Exactly this…. https://t.co/yWj7fZ01HR
    about 16 hours ago
  • https://t.co/EmagdpLoNv "Introducing GitHub Copilot X · GitHub"
    about 1 day ago
  • RIP Gordon Moore: https://t.co/c5J9LaHrj8
    about 1 day ago
  • Our daughter works as a teaching assistant at a local K-8 public school. It’s become apparent that students today r… https://t.co/p2t912GVyc
    about 2 days ago
  • Working to wrap up my current consulting gig by mid-April. I will be looking for the next thing soon. If anyone has… https://t.co/sg3cSV9yqM
    about 3 days ago
  • RSS - Posts
  • RSS - Comments
Find me on Mastodon