Home   |  Articles   |  Resources   |  Humor   |  Feedback       

  Login   Register 

Ads Via DevMavens


Dynamically assign a Style Sheet to an ASP.NET page

Posted by on Saturday, January 10, 2004

Using a Literal control from the code behind allows you to programatically assign the style sheet (CSS) for an ASP.NET page.
One popular personalization feature is to allow users to select a “skin” for your site (the new MSN home page has some nice looking color schemes to choose from). While ASP.NET server controls allow you to assign a style sheet class easily (the Class property), there is no property or method to set the stylesheet (or stylesheets) assigned to a page.

One easy way to do this is to replace your LINK tag in the ASPX with a Literal server control. The Literal control is a placeholder for rendering text into the HTML. By assigning your LINK tag to the Text property of a Literal, you manage the style sheet of a page from the code behind.

Here is an example ASPX file


<%@ Page language="c#" Codebehind="DynamicStyleSheet.aspx.cs"
 AutoEventWireup="false" Inherits="aspnet.DynamicStyleSheet" %
>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
	<HEAD>
		<title>DynamicStyleSheet</title>
		<asp:Literal id="StyleSheet" runat="Server" />
	</HEAD>
	<body MS_POSITIONING="GridLayout">
		<form id="Form1" method="post" runat="server">
			<h6>hello world</h6>
		</form>
	</body>
</HTML>


The corresponding code behind file


public class DynamicStyleSheet : System.Web.UI.Page
{
   protected System.Web.UI.WebControls.Literal StyleSheet;

   private void Page_Load(object sender, System.EventArgs e)
   {
      if(!IsPostBack)
      {
         const string styleFormat = 
           "<LINK href='\"{0}\"' type='\"text/css\"' rel='\"stylesheet\"'>";

         string linkText;
         linkText = String.Format(styleFormat, StyleSheetPath);
         StyleSheet.Text = linkText;         
      }
   }

   protected string StyleSheetPath
   {
      get
      {
         // pull the stylesheet name from a database or xml file...
         return ApplicationPath + "MyStyle.css";
      }
   }

   private string ApplicationPath
   {
      get
      {
         string result = Request.ApplicationPath;
         if(!result.EndsWith("/"))
         {
            result += "/";
         }
         return result;
      }
   }
}


Comments:

May lead to problem if you want to cache pages
By navneet on 10/3/2004
It is a good way of doing it, however theres a potential problem. If one allows different skins for the same web page, then it can create problems if you want to cache the page, since the page that comes back will have the color scheme of the first person who cached it. So you may come across a variety of pages styles. There must be a solution to this, I had thought of caching by parameter, but the way that works is that the parameter would need to come from the browser i.e in all links etc one has to have that parameter which sounds a major pain. So can't see that as practical. Only other option is not to cache - which isin't brilliant either. Anyhow thought it useful to bear this point in mind.

Copyright 2004 OdeToCode.com 


The Blogs
Subscribe to the OdeToCode blogs for the latest news, downloads, new articles, and quirky commentary.
New Articles
C# 3.0 and LINQ
C# 3.0 introduced a number of new features for LINQ. In this article we'll examine the new features like extension methods, lambda expressions, anonymous types, and more.

Introduction To LINQ
This article is an introduction to LINQ and provides examples of using LINQ to query objects, XML, and relational data.

What ASP.NET Developers Should Know About JavaScript
This article looks at JavaScript from the perspective of a C# or Visual Basic programmer. See how to apply object oriented techniques to your JavaScript code.

Most Popular Articles
ASP.Net 2.0 - Master Pages: Tips, Tricks, and Traps
MasterPages are a great addition to the ASP.NET 2.0 feature set, but are not without their quirks. This article will highlight the common problems developers face with master pages, and provide tips and tricks to use master pages to their fullest potential.

Table Variables In T-SQL
Table variables allow you to store a resultset in SQL Server without the overhead of declaring and cleaning up a temporary table. In this article, we will highlight the features and advantages of the table variable data type.

AppSettings In web.config
In this article we will review a couple of pratices to keep your runtime configuration information flexible.

Contribute Code
Privacy
Consultancy