Dave Burke : Freelance .NET Web Developer specializing in Online Communities

Loading a browser-specific CSS

I wrote the other day about modifying existing CS code to load a specific skin CSS based on the browser type.  I wanted to do the same with my non-CS-aware site default.aspx page, so the process was a bit different.

First, load the browsercaps section for Gecko recognition.  (See earlier post.)  Then add the following static string to any class.  I added it to the same Utilities class where my CheckIs() methods are located (again, see earlier post.)

public static string styleFormat = "<link rel=\"stylesheet\" href=\"{0}\" type=\"text/css\" />";

In the usercontrol (placed in the head html area of the .aspx) which contains a single ASP:Literal control we add the following:

protected System.Web.UI.WebControls.Literal ltStyle;

private void Page_Load(object sender, System.EventArgs e)
{
 if (!IsPostBack)
 {
  if (Utilities.CheckIsIE55())
   ltStyle.Text = string.Format(Utilities.styleFormat, "/dbvt/style.css");
  else
   ltStyle.Text = string.Format(Utilities.styleFormat, "/dbvt/style_gecko.css");
 }
}

Now we've got a home page that renders darn-near exactly on both IE and Firefox.

Comments (0) | Post RSS RSS comment feed

Posted on 3/25/2005 5:08:00 PM by Dave Burke
Categories: Community Server
Tags:

Related posts


Powered by BlogEngine.NET 2.0.0.36
Theme by Dave Burke