Nikolaos Kantzelis ASP.Net Blog

This blog will focus on ASP.NET Framework


About Me

Great Blogs

How to create selectable themes in your ASP.Net applications

In this post I am going to show you something that we see in most websites. When we visit a website we are given the choice through a control to select the theme(colors,font size,font family) that we want to be applied to the site.

In almost all web sites we define the look and feel of the site through Themes,skins,Master Pages and Stylesheets.

I assume that you know a little bit about CSS,XHTML. I assume that you have little knowledge of web forms and master pages.

Before you go on it will be useful if you have a look in this other post of mine.

1) Launch Visual Studio 2005/2008/2010. Express editions will work fine.

2) Create an empty web site. Choose an appropriate name. Choose VB.Net as the development language.

3) Add an item to your website, a master page. Name it Master.master.

4) Add an ASP.Net special folder to your site, "App_Themes". Add two Theme folders under it. Name the first Theme folder, "Gray" and the other one "Orange".

5) Add another item in your site, a stylesheet. Name it "Site.css".Place this .css file under the Gray Theme folder. The code for the site.css follows.


* {

body {
  font62.5%/1.6 "Lucida Grande", "Lucida Sans", "Lucida Sans Unicode", 
Verdana, sans-serif;

h1 {

h2 {

pli {

h1h2p {
  margin1em 0;

#header {

#header h1 {

#mainNav {

#mainNav a {   
  padding0 15px;
  border-bottom1px solid #FFF;
#mainNav a:hover { 

#mainNav #con a {

#wrapper {
  margin0 auto
  background#fff url('../../Styles/images/bg-fixed.gif') repeat-y left top;

#content { 

#footer {
  padding1px 20px;


6) Place the site.css file under the Orange Theme folder as well. We will make some changes to the .css file so we give an "orangish" look and feel.Τhe only changes I have made to the original site.css are the following:(in bold are only the changes)

body {
  font62.5%/1.6 "Lucida Grande", "Lucida Sans", "Lucida Sans Unicode",
 Verdana, sans-serif;
#header {

#header h1 {


#mainNav {

7) I have created the markup for the master page. It is simple HTML with the  <@Master> directive on top and the ContectPlaceholder controls.We will have a dropdownlist web server control that the user will choose his desired theme.

<%@ Master Language="VB"  CodeFile="Master.master.vb" Inherits="Master" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" 
<html xmlns="">
<head id="Head1" runat="server">
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
<meta name="author" content="Nikolaos Kantzelis" />
<meta name="keywords" content=",css,jquery,javascript,mvc,LINQ,
Entity Framework" />
<meta name="description" content="ASP.Net is a framework developed By Microsoft 
to create extensible,scalable,eye catching web applications." />
<meta name="robots" content="all" />

<title>ASP.Net Rules</title>

<asp:ContentPlaceHolder id="HeadContent" runat="server">


<form id="form2" runat="server">
<div id="wrapper">
<div id="header">
<h1>ASP.Net RULES!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!</h1>
<h3>Select your theme</h3>
 <asp:DropDownList ID="DropDownList1"  runat="server" AutoPostBack="true">
   <asp:ListItem Value="Gray">Gray</asp:ListItem>
   <asp:ListItem Value="Orange">Orange</asp:ListItem>
<div id="main">
<asp:ContentPlaceHolder ID="MainContent" runat="server"/>
<div id="content"> 

<div id="Div1">
<h1>Lorem ipsum dolor</h1>
<p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy 
nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat.</p>
<h2>Ea commodo consequat</h2>
<p> Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy 
nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat. 
Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper.</p>

<ul id="mainNav"> 
<li class="first"><a href="#">Home</a></li> 
<li><a href="#">Company</a></li> 
<li><a href="#">News</a></li> 
<li><a href="#">Products</a></li> 
<li><a href="#">Services</a></li> 
<li><a href="#">Clients</a></li> 
<li id="con"><a href="#">Contact</a></li> 

<div id="footer">
<p>Copyright © 2011 by NKSolutions. All rights Reserved. 
Site developed by  NKSolutions</p>


8) Add another item to your site, a web form. Make sure you choose this master page.

In my case the markup for the default.aspx looks like this

<%@ Page Title=""Language="VB" MasterPageFile="~/Master.master" 
AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>

<asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" Runat="Server">
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" Runat="Server">
        <center><em>THIS IS THE HOME PAGE</em></center></p>


This is the simplest of pages using a master page.Just make sure that the ContentPlaceHolder properties of the Content controls are set to the right values.

Usually we could have the functionality for selecting  a desired theme from the user,(If we choose to implement the user selection per page-which would be wrong) in the Pre_Init event of the Page class.

But now, very wisely, we have the dropdownlist control on the master page.The problem with the master page is that is not actually a page. It does not inherit from the Page class, it inherits from the UserControl class.

Have a look at the MSDN documentation and you will see something like this.

 Public Class MasterPage
          Inherits System.Web.UI.UserControl

     Member of System.Web.UI

Acts as a template and merging container for pages that are composed only of System.Web.UI.WebControls.Content controls and their respective child controls.


It is clear that we do not have a Pre_Init event in this case.Let me show you how we can solve that.

9) Add another item to your site,a class file. Name it PageClass.vb. We will make this class inherit from the Page class.

The code for this class follows:

Imports Microsoft.VisualBasic

Public Class PageClass
    Inherits Page

 Protected Overrides Sub OnPreInit(e As System.EventArgs)
    If (Not Request.Form Is Nothing And Request.Form.Count > 0) Then
     Me.Theme = Request.Form(Me.Master.FindControl("DropDownList1").UniqueID)
    End If
 End Sub
End Class


I override the Pre_Init event of the base class and write my own custom code inside the overridable method.

I set the Theme property of the page (Me) by getting from the master page the UniqueID of the dropdownlist control.

10) The Default.aspx.vb must inherit fromt the PageClass class and not the Page class. So we must change that.

The whole code for the Default.aspx.vb  follows.

Partial Class _Default
    Inherits PageClass

End Class


11) Run your application.Initially we do not see any style applied to our page. Choose your theme, Gray or Orange and see the markup being styled accordingly.

All of the pages in your site must inherit from the PageClass class.

Drop me an email if you want the source code.

Hope it helps!!!!


Andrew said:

nice code...but there a little bit problem...when m navigating to other page (means "Home.aspx" to "News.aspx" ) the selected theme is changed...or the selected theme is not for the whole session of browse....plzzz help me...

# February 8, 2011 9:09 AM

zazababy said:

Can u email me code for C# on selectable themes pls

# February 12, 2011 2:34 AM

sathish said:

i need source code please send it

# September 16, 2011 2:44 AM