Create a Mobile Friendly Website with ASP.NET Themes

by Justin 8. November 2008 10:15

Download the Demo (ASP.NET 2.0)

If you're reading this you realize the value of optimizing websites for mobile devices. I created an ASP.NET website that chooses a different theme depending on whether or not the device accessing the page is a mobile device. If a mobile device is recognized the theme displays a red background, otherwise a different theme is chosen which displays a blue background. Feel free to check out my online demo for custom theming mobile devices, and download a zipped version of the demo to see exactly how it works. Be sure to view the demo in both a mobile and non-mobile browser to get the full effect. If you don't have a mobile device try using the online Opera Mini simulator.

Supported Mobile Devices:

  • Apple iPhone
  • Apple iPod
  • Google Android
  • Opera Mini
  • PlayStation Portable
  • Handspring Blazer
  • Motorola Internet Browser
  • Pocket IE
  • RIM Browser
  • Nokia
  • Compact NetFront

How it Works

I've added some custom browser definitions to the App_Browsers folder. This extends the default ASP.NET browser definitions, which are not very good for recognizing mobile devices. There are two .browser files, courtesy of Owen Bradly's website, that contains the definitions. (I'll note that I added a definition for recognizing the Google Android as it was not in Owen's files.) Each time the Default.aspx page is request I check to see if a supported Mobile device is requested the page. If so then I change the theme to the mobile theme. The code to do this is very simple.

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class _Default : System.Web.UI.Page 
{
    protected void Page_PreInit(object sender, EventArgs e)
    {
        if (Request.Browser.IsMobileDevice)
            Theme = "Mobile";
    }
}

If you're using Master pages you can always set this in the master page's code behind instead of each individual page's code behind.

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags: , ,

asp.net

Powered by BlogEngine.NET 1.4.5.0
Theme by Mads Kristensen

About the author

Justin HoltonHi, my  name is Justin. I cut my teeth learning HTML back when Netscape Navigator was still the most popular web browser. Later that inspired me to major in Computer Science at college. Today I'm a professional web developer with experience in everything from social networking application design to Search Engine Optimization (SEO). I believe the Internet is the most important achievement of man since the printing press, and I'm grateful that I was born in time to see it go from obscurity to a ubiquity.

Page List