Website development and design blog, tutorials and inspiration

Creating Fully Themable Websites with ASP.Net

Making themeable websites in ASP.Net

By , Written on in C#

Creating Fully Themable Websites with ASP.Net
ASP.Net and the .Net Framework provides a powerful engine that allows themes and skins to change the appearance of asp controls, but what do you do if you want to completely change the appearance of a website, including its layout? This great tip will allow your website to be completely themeable and dynamic.

In a previous tutorial we looked at ASP.Net Skins and Themes, but these can only change the appearance of an ASP.Net control. You cannot change the layout of a website or page, nor can you have multiple content layouts or even add or remove controls on a per-theme basis. This tutorial will show you how to create a fully customisable theme file using master pages, inheritance and very little code.

To start off with, create a blank ASP.Net website and create a folder called "themes" (without quotes) and another folder called default within themes. Note: This is not an ASP.Net folder 'App_Themes' it's just a simple folder called themes. You can call it whatever you like.

Next, we need to create a master page file inside our default theme folder. Visual Studio will not allow you to create a master page inside a subfolder so we have to create it in the root and move it in after. This applies to the master page, style sheet and any other file specific to the theme. When you create the master page call it 'site.master' then move it to the default theme folder.

Now we need to delete the Default.aspx page and create a new one that uses a master page, so add a new item to the project (File->Add New Item or right-click on the project and add new item) and select a new web form. Make sure that the "Select master page" checkbox is ticked before clicking OK. A new window will ask you to select the master page, so browse to you themes/default/site.master.

Now we need to sort out a base page class that all pages we create can use so that they all inherit the theme functionality we are about to include. Right-click on the project in Solution Explorer and add a new item. Select Class and name it BasePage.cs. Visual Studio will display a message stating:

"You are attempting to add a class to an ASP.NET application. For a class to be generally consumable in your site, it should be placed inside the 'App_Code' folder. Would you like to place the class in the 'App_Code' folder?"

Click on "Yes" and Visual Studio will create the folders and a new class file. Our class is going to inherit from a standard ASP.Net page and provide extra functionality in the form of themes. At this stage, all we are going to do is tell our BasePage class to inherit from the standard System.Web.UI.Page class:

  1. using System;
  2. using System.Data;
  3. using System.Configuration;
  4. using System.Web;
  5. using System.Web.Security;
  6. using System.Web.UI;
  7. using System.Web.UI.WebControls;
  8. using System.Web.UI.WebControls.WebParts;
  9. using System.Web.UI.HtmlControls;
  11. /// <summary>
  12. /// Summary description for BasePage
  13. /// </summary>
  14. public class BasePage : System.Web.UI.Page
  15. {
  16. public BasePage()
  17. {
  18. //
  19. // TODO: Add constructor logic here
  20. //
  21. }
  22. }

BasePage.cs: BasePage class inherits from System.Web.UI.Page

In the OnPreInit event for the class, we are going to set the location of the master page to use ('themes/default' will be the default) using the MasterPageFile property.

  1. protected override void OnPreInit(EventArgs e)
  2. {
  3. string theme = "default";
  4. MasterPageFile = "~/themes/" + theme + "/site.master";
  5. base.OnPreInit(e);
  6. }

This code has to go into the OnPreInit event because anywhere else is too late to set the MasterPage because it has already been loaded.

I have hard-coded default in this example, but you can load the theme from the web.config, user profile, query string or any other method - I'll leave that for you.

Go back to the default.aspx page and view the code in default.aspx.cs. Each ASP.Net page has its own class which inherits from the System.Web.UI.Page class, we need to change this so that Default.aspx inherits from our BasePage class (which in turn inherits from System.Web.UI.Page).

  1. using System;
  2. using System.Data;
  3. using System.Configuration;
  4. using System.Web;
  5. using System.Web.Security;
  6. using System.Web.UI;
  7. using System.Web.UI.WebControls;
  8. using System.Web.UI.WebControls.WebParts;
  9. using System.Web.UI.HtmlControls;
  11. public partial class _Default : BasePage
  12. {
  13. protected void Page_Load(object sender, EventArgs e)
  14. {
  16. }
  17. }

Default.aspx.cs: _Default page class inherits from BasePage rather than System.Web.UI.Page

So now we have a BasePage class which our Default.aspx page inherits from. Within the BasePage class, we can programmatically set the master page to use by setting a MasterPageFile property on the Page_Load event.

You can modify the master page to create your default theme, add CSS style sheet files, images and even user controls within the theme. Everything that is unique to the theme should be in the themes folder.

When you want to create a new theme, simply duplicate the folder and start changing it how you want it, the only code changes to make are those that are unique to the theme, and you will need a method to change themes or allow your users to change themes.

When you add more pages to your website, to have them use this new skinning platform simply change the class they inherit from System.Web.UI.Page to our new BasePage class.

You can download the source code for this tutorial below; I have created two themes with the ability to switch themes using two links. You can see that although the content is the same, using different master pages allows the site to look completely different between themes. One theme has a left sidebar, content and right sidebar, whereas the second has two right-hand sidebars with different content.

Download Download sample website source code

Last updated on: Thursday 22nd June 2017


There are no comments for this post. Be the first!

Leave a Reply

Your email address will not be published.