The Beauty of ASP.NET 2.0 Themes in Visual Studio 2005

This article introduces you to “ASP.NET 2.0 Themes” with the Visual Studio 2005 Integrated Development Environment. The sample downloadable application was developed using Visual Studio 2005 Professional Edition on Windows Server 2003 Standard Edition.

Contributed by
Rating: 4 stars4 stars4 stars4 stars4 stars / 61
April 10, 2006
Rate this Article:
MEH MEH++


SEARCH ASP FREE
TOOLS YOU CAN USE

advertisement

A downloadable file for this article is available here.

Understanding “Themes” in ASP.NET 2.0

Any commercial website needs to spruce up its appearance now and then. Even though the appearance of a website may get modified periodically, the consistency of the entire website should always be maintained.

The most important issues for consistency are the coloring methodology and other look and feel strategies. In general, to maintain consistency throughout a website, style sheets (Cascading Style Sheets) have been a good choice. Style sheets have been introduced with DHTML, even before any server side technology was invented.

And later style sheets have been directly integrated into (almost) every server side technology available (along with ASP.NET). Applying CSS to HTML seems to be simple and easy, but applying the same to ASP.NET Server controls can be confusing, because it is up to the developer to know which HTML tags are rendered by the control and how they are rendered.  Keeping such scenarios in mind, Microsoft introduced a new feature in ASP.NET 2.0 called “themes.”

Before understanding “themes,” we need to understand the concept of “skin.” A skin generally contains visual properties (for look and feel strategies) for one or more kinds of ASP.NET controls (server controls).  Every skin is made with a set of skin tags, which are typically based on XML and XSLT specifications. At this movement (at the time of this writing), Visual Studio 2005 doesn’t have any intellisense support to work with skin tags. It is the developer’s responsibility to figure out the syntax of every skin manually.

Coming to the concept of “themes,” a theme is simply a collection of several skins and style sheets together with images. A theme really makes a web developer’s life very easy as they can define the appearance for a set of controls at design time and make them look coherent to the overall application. Web developers can apply theme to the application at control, page or application levels.  This article mainly concentrates on the following issues:

  • designing skins
  • creating themes based on the skins
  • applying themes to web pages
  • applying two different appearances for the same kind of controls
  • creating and applying Cascading Style Sheets to the theme
  • applying themes at runtime.

A simple demonstration of ASP.NET 2.0 skins and themes

Before proceeding to the demonstration, we need to know where the skins and themes actually reside in an ASP.NET 2.0 web application.  Skins are located inside the selected themes folders, whereas themes are located in a special folder inside the framework. Themes can be declared in the source as well as class files.

However, custom made themes can also be saved inside the predefined "App_Themes" folder inside ASP.NET applications, making them easier to manage and interpolate according to your needs. This new feature improves the maintenance of the website and avoids unnecessary duplication of code for shared styles.

Let us start with a simple example:

1.       Go to Start ->Programs -> Microsoft Visual Studio 2005.

2.       Open File -> New -> Web Site -> ASP.NET Web Site and provide the name  “SampleTheme.”

3.       Go to Solution Explorer -> right click on the root folder(C:...SampleTheme) -> Add an ASP.NET Folder -> click on the “Theme” option and provide the name  “FirstTheme” as shown in fig1. The new folder “App_Themes” gets added to the solution explorer along with a subfolder called “FirstTheme.”

We just finished creating the folder structure for implementing themes. Now, it's time to create and place skin files within the same theme folder.   Proceed with the following steps:

1.       Right-click on Theme Subfolder(“FirstTheme”) -> Add New Theme -> select Skin File and provide the name “Button.skin” as shown in fig2.

2.       Finally click on the “Add” button (which should create the skin file within the theme folder “FirstTheme”).

After adding the skin file to the theme folder, you will be taken to the “source view,” where you need to specify the skin configurations.  A skin configuration for a particular control is simply the style (visual properties) of a particular control.  Within the “source view,” type the following sample skin configuration for a “button” control:

<asp:Button
 runat="server"
 BackColor="Red"
 BorderColor="Black"
 BorderStyle="Solid"
 BorderWidth="1px"
 />
 

Once you complete all of the above steps, we need to proceed with applying the theme (along with the skin).  Proceed with the following steps.

1.       Go to the design pane of “default.aspx” page.

2.       Drag an ASP.NET button on to the “default.aspx” (its visual properties will be automatically applied at run-time based on the skin definition above).

3.       Select “Document” in the  top “dropdownlist” of Properties Window.

4.       Select the “Theme” Property and choose the “FirstTheme” item from dropdown list.

5.       Run the application.

You must get the following output (fig3).

You can observe that I didn’t set any properties for the button.  But at runtime it is communicating with the theme (along with the skin) and applying new properties by itself!

How to create and apply different skin elements to the same Web Server Control

Let us try to tell controls on the web page to use a specific skin element by adding a SkinID attribute. Place two ASP.NET list box controls on the web page and create a skin file (as detailed in previous section) with name “ListBox.skin” and type the following in the “source view”:

<asp:ListBox
 runat="server"
 BackColor="Yellow"
 BorderColor="Red"
 BorderStyle="Solid"
 BorderWidth="1px"
 />
 
<asp:ListBox
 runat="server"
 SkinId="lstSkinBlue"
 BackColor="Blue"
 BorderColor="white"
 BorderStyle="Solid"
 BorderWidth="3px
/>

From the above code, we have two different specifications for two separate instances of the same ASP.Net Server control (listbox). You must observe that the first specification does not have the attribute “SkinId,” whereas the other one does.

Let us apply the above specifications to the controls and observe the difference.

1.       Go to the design pane of default.aspx page.

2.       Add two more list boxes (named ListBox1 and ListBox2).

3.       Open the properties for “ListBox2.”

4.       Specify “lstSkinBlue” for the property “SkinId.”

5.       Run the application.

You should be able to get the following output (fig4).

If a control is not specified with any “SkinID” property, then the default skin would be considered for display.

How to create and apply Cascading Style Sheets to an ASP.NET 2.0 theme

Theme folders will support Cascading Style Sheets as well. Let us look at a simple example with CSS. Proceed with the following steps (continued from the previous section):

1.       Right-click on the Theme folder(“FirstTheme”) -> Add New Theme -> select the “Style Sheet” template and provide the name “StyleSheet1.css” as shown in fig5.

2.       Finally click on the “Add” button (which should create a style sheet file within the theme folder “FirstTheme”).

Double click on this style sheet file and type the following code:

body
{
      background-color:Orange;
      height:auto;
      width:auto;
}

In the above code, we are simply setting the properties for background color (as orange), height (as auto) and width (as auto). Once you execute the solution, you should see the output as follows, in fig6.

How to apply ASP.NET 2.0 themes at runtime: creating theme and skin

We can also set the theme of a page during run time. This concept is useful for allowing users to customize the page according to their preference or interest.

Let us go through an example of setting a theme during runtime (we shall use the same design and controls discussed in previous sections)

1.       Modify the properties of the button as follows:

a.       Text=”First Theme”

b.       ID=”btnFirstTheme”

2.       Place another button (ASP.NET server control) and modify the following properties:

a.       Text=”Second Theme”

b.       ID=”btnSecondTheme”

3.       Create one more Theme (named “SecondTheme”) by just following the steps discussed in the previous sections. By this time, your solution explorer should look something like the following image, fig7.

4.       Add a new skin (“SkinFile.skin”) to the newly created theme (“SecondTheme”) by just following the steps discussed in the previous sections.

5.       Open “SkinFile.skin” file and type the following code:

<asp:Button
 runat="server"
 BackColor="AliceBlue"
 BorderColor="Black"
 BorderStyle="Solid"
 BorderWidth="1px"
 />
<asp:Button
runat="Server"
SkinId="btnSecondTheme"
BackColor="#FFE0C0"
BorderColor="#C04000"
BorderStyle="Solid"
BorderWidth="1px"
/>
 
<asp:ListBox
 runat="server"
 BackColor="green"
 BorderColor="white"
 BorderStyle="Solid"
 BorderWidth="1px"
 />
 
<asp:ListBox
 runat="server"
 SkinId="lstSkinBlue"
 BackColor="steelblue"
 BorderColor="black"
 BorderStyle="Solid"
 BorderWidth="1px"
/>

How to apply ASP.NET 2.0 themes at runtime: working with code

Once you complete all the steps in the previous section, you need to perform the following steps to achieve the desired goal.

Open your “web.config” and modify as shown below:

<system.web>
<profile>
                  <properties>
                        <add name="Theme" type="System.String" />
                  </properties>
            </profile>
</system.web>

We basically store the user-selected theme in the “profile” as specified above.  The “profile” is a bit new in ASP.NET 2.0.

Within the button click event of first button, copy the following:

Protected Sub btnFirstTheme_Click(ByVal sender As Object,
ByVal e As System.EventArgs) Handles btnFirstTheme.Click
            Profile.Theme = "FirstTheme"
            Response.Redirect("default.aspx")
    End Sub

Similarly, within the button click event of second button, copy the following:

 

Protected Sub btnSecondTheme_Click(ByVal sender As Object,
              ByVal e As System.EventArgs) Handles btnSecondTheme.Click
               Profile.Theme = "SecondTheme"
               Response.Redirect("default.aspx")
    End Sub

And finally, within the Page_PreInit (NOT IN PAGE_LOAD) event, copy the following:

Protected Sub Page_PreInit(ByVal sender As Object,
ByVal e As System.EventArgs) Handles Me.PreInit
                 
 If Profile.Theme <> "" Then
                  Page.Theme = Profile.Theme
             End If
    End Sub

Once you execute the application, try clicking on the buttons (one by one), you should find the difference for yourself!

Any comments, suggestions, bugs, errors, feedback etc. are highly appreciated at jag_chat@yahoo.com.

blog comments powered by Disqus
ASP.NET ARTICLES

- Implementing ASP.NET 4.0 Page.MetaDescriptio...
- ASP.Net Development Tips
- Intro to Sessions in ASP.Net
- Google Maps API Introduction in ASP.NET usin...
- Creating an ASP.NET 3.5 Gridview Image Galle...
- Encrypt QueryString in ASP.NET 3.5 using VB....
- ASP.NET 3.5 Drop Down List Controls
- Connect to Access Database with ASP.Net
- Secure Audio Streaming with ASP.Net and Flash
- Dynamic Sitemap and Navigation in ASP.Net
- Implement Gzip and Deflate Compression in AS...
- Run ASP.Net in Ubuntu with Apache
- ASP.Net Mono Website Contact Forms
- ASP.Net URL Rewriting Methods
- Murach`s ASP.NET 4 Web Programming with C# 2...

ASP Web Hosting ASP.Net Web Hosting Windows Web Hosting
 
 
 

ASP Free Forums 
 RSS  Tutorials RSS
 RSS  Forums RSS
 RSS  All Feeds
Site Map 
Request Media Kit
Write For Us Get Paid 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Privacy Policy 
Support 


© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 5 - Follow our Sitemap
Most Popular Topics
All ASP.Net Tutorials