Blue Theme Orange Theme Green Theme Red Theme
 
Safari Books Online
Home | Forums | Videos | Photos | Blogs | Beginners
 | Consulting  
Submit an Article Submit a Blog 
 Jump to
Skip Navigation Links
TechnologyExpand Technology
WebsiteExpand Website
Nevron Chart
 Resources  
Close
 Our Network  
Close
Search :       Advanced Search »
Home » ASP.NET and Web » Themes in ASP.NET 2.0

Themes in ASP.NET 2.0


In this article we will try to cover one great feature of ASP.NET 2.0, i.e. Themes. Themes are the great way to customize user-experience in the web application. Themes are used to define the look and feel of the web application, similar to the use of Cascading Style Sheets (CSS). Unlike CSS, themes can specify the look of the server-side control like a tree-view control, how they will appear when they are rendered by the browser.

Total page views :  13176
Total downloads : 
   Print Read/Post comments Post a comment  Similar Articles  
   Email to a friend  Bookmark  Author's other articles  
 
Become a Sponsor

In this article we will try to cover one great feature of ASP.NET 2.0, i.e. Themes. Themes are the great way to customize user-experience in the web application. Themes are used to define the look and feel of the web application, similar to the use of Cascading Style Sheets (CSS). Unlike CSS, themes can specify the look of the server-side control like a tree-view control, how they will appear when they are rendered by the browser.

A theme once they are created can be used in one of the following two ways.

  1. Themes can be used as a style sheet, which is similar in nature as the regular CSS, 
  2. Themes can be used as customized themes, which change the order of preference for style rules, and will specify the style to use for each element, overriding any style preference specified in a separate style sheet, or even in the style attribute of an element. 

The process of creating a theme involves creation of .skin file which have the definition of appearance of each element on the page, and ASP.NET puts this skin file within a folder name of which specifies the name of the theme. All the themes created are placed under a folder named App_Themes with in your application folder.

Let us do some hands on to get a feel of the same. But Make sure SQLSERVER 2005 EXPRESS is installed and running.
To Create a theme, right-click solution explorer, or the root folder, and select Add ASP.NET Folder Theme (see Figure 1).

Figure 1 - Creating a Theme

This will create an empty folder within the APP_Theme folder. Let us re-name the folder as Blue Theme. By Right clicking the App_Theme Folder and selecting Add ASP.NET Folder, create another theme folder named Red Theme.

Now Right-Click the Blue theme folder and select Add New Item. In the Dialog box select Skin File and name it as MyBlueSkin (see Figure 2).

Figure 2 - Naming a Theme

<%--
Default skin template. The following skins are provided as examples only.
 
1. Named control skin. The SkinId should be uniquely defined because
   duplicate SkinId's per control type are not allowed in the same theme.
 
<asp:GridView runat="server" SkinId="gridviewSkin" BackColor="White" >
   <AlternatingRowStyle BackColor="Blue" />
</asp:GridView>
 
2. Default skin. The SkinId is not defined. Only one default
   control skin per control type is allowed in the same theme.
 
<asp:Image runat="server" ImageUrl="~/images/image1.jpg" />

In the same way create the MyRedSkin skin file in the Red Theme Folder.

Now let us add one web page and name the same as StyleSheetDemo.aspx. In the page we add one label control, name it as label1 and one textbox control name it as TextBox1.

The page will look like Figure 3.

Figure 3 - The Style Sheet Demo Page for the Theme

Now we will add some code in the MyBlueSkin skin file which we created earlier.

The code will be as follows: 

<asp:Label runat="server" backcolor="blue" forecolor="red" font-size="xx-large" font-names="arial" />
<
asp:TextBox runat="server" backcolor="cyan" forecolor="blue" font-size="xx-large" font-names="arial" />

Now double-click the web.config file and put a section as follows:

<?xml version="1.0"?>
<
configuration>
    <
appSettings/>
    <
connectionStrings/>
    <
system.web>
         <
compilation debug="false" strict="false" explicit="true" />
         <
pages theme ="Blue Theme" />
         <
authentication mode="Windows" />
    </
system.web>
</
configuration>         

Now save the web.config  and put a little code in Page_Load Method of the StyleSheetDemo.aspx 

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
       
If Not Page.IsPostBack Then
            Label1.Text = "Theme demo"
            TextBox1.Text = "My Blue Theme"
        End If
End
Sub 

Now run the page by right clicking it and selecting View in Browser. The page will be like Figure 4:

Figure 4 - The Themed Web Page

Now in the same way add a skin file in the Red Theme folder and name it as MyRedSkin.skin. Then add the following code in the skin file.

<asp:Label runat="server" backcolor="red" forecolor="blue" font-size="small" font-names="arial" />
<
asp:TextBox runat="server" backcolor="blue" forecolor="white" font-size="small" font-names="arial" /> 

Change the web.config file as follows:

<configuration>
    <
appSettings/>
    <
connectionStrings/>
    <
system.web>
        <
compilation debug="false" strict="false" explicit="true" />
        <
pages theme ="Red Theme" />
 
       <authentication mode="Windows" />

Now run the page in the browser and will find the similar result as in Figure 5.

Figure 5 - Results of web.config change

With the profile you can allow the user to select the themes.

We will create another demo web site to see the same.

Open Visual Studio 2005 or Visual Web developer 2005 Express and select Open Web Site. Click the new folder Icon and type in ProfileandThemes (see Figure 6) and then click Open. This will create an empty web site, where we can put anything as we wish.

Figure 6 - Creating a new folder for a Web site.

Now we will put a web.config into the we site by right-click the web folder and then selecting Add New Items. In the dialog box (see Figure 7) select Web Configuration File and click add. 

Figure 7 - The Add New Item Dialog

Delete the content of the  web.config file to match up what is displayed in Figure 8. 

Figure 8 - The web.config file

Now put the following code between <system.web> </system.web>

<anonymousIdentification enabled ="true" />
  <
profile>
     <
properties>
        <
add name="UserName" defaultValue="MyUserName" allowAnonymous ="true"/>
     </
properties>
  </
profile 

Now create a web page and name it as ProfileTheme.aspx

Set the page as shown in Figure 9.

Figure 9 - Creating a page with a label and theme.

Now add another page and name it as Settings.aspx In the source view mode of the ProfileTheme.aspx, type in the following code just after the label control. 

<strong>Welcome</strong>
<
asp:Label ID="Label1" runat="server" Text="Label" Width="208px" Font-Bold="True"></asp:Label>
      <a href="Settings.aspx">Settings.aspx</a>

This create as hyperlink for the settings page in the profilethemes page.

Now put some controls in the settings page. The page should look like Figure 10:

Figure 10 - Settings.aspx page with textbox and Save button.

Now we need to put some code. Double click the Button and in the click event write the following code:

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        Profile.UserName = TextBox1.Text
        Response.Redirect(
"ProfileTheme.aspx")
End
Sub

Mdify the ProfileTheme.aspx page and add a textbox control there. And add a little bit of code in the page load event:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
    
If Not Page.IsPostBack Then
         Label1.Text = Profile.UserName
         TextBox1.Text =
"Themes with Profiles"
     End If
End
Sub 

Now create the two themes in the same way we discussed earlier. This because in this demo I am going to use the same themes. Now in the web.config file add the following property:

<profile>
  <
properties>
     <
add name="UserName" defaultValue="MyUserName" allowAnonymous ="true"/>
     <
add name ="MyThemes" defaultValue ="Blue Theme" allowAnonymous ="true" />
  </
properties>
</
profile> 

And we will make some more changes in the Settings page now, to allow the user to select a theme. Add a dropdowncombo and Enter the theme names in the items box. The page should look like Figure 11: 

Figure 11 - Page now with DropDownCombo control added.

Add the following code in the Page load event of the settings page:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
   
If Not Page.IsPostBack Then
        DropDownList1.Items.FindByValue(Profile.MyThemes).Selected = True
    End If
End
Sub 

And also modify the Click Event of the Save button.

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs)
    Profile.UserName = TextBox1.Text
    Profile.MyThemes = DropDownList1.SelectedValue
    Response.Redirect(
"ProfileTheme.aspx")
End Sub

In the ProfileThemes.aspx page add the following code:

Protected Sub Page_PreInit(ByVal sender As Object, ByVal e As System.EventArgs)
        Page.Theme = Profile.MyThemes

End
Sub

Now run the application. This will create a SQL SERVER database file automatically and will store all the information regarding a User Profile.

So whenever the user log on to the website, he will find his settings intact (see Figure 12).

Figure 12 - Final page

In this demo I have used lot of modifications in the source and code. This could have been avoided. But If you follow all the steps and at any point of time try to test the application you can do that. I have also included some overview about the profiles to make you understand how the user settings area stored.


Login to add your contents and source code to this article
 About the author
 
Ravikumar Raja
Simple guy working in CTS for past a year, almost worked in most Microsoft Technology... thriving for More Knowledge
Looking for C# Consulting?
C# Consulting is founded in 2002 by the founders of C# Corner. Unlike a traditional consulting company, our consultants are well-known experts in .NET and many of them are MVPs, authors, and trainers. We specialize in Microsoft .NET development and utilize Agile Development and Extreme Programming practices to provide fast pace quick turnaround results. Our software development model is a mix of Agile Development, traditional SDLC, and Waterfall models.
Click here to learn more about C# Consulting.
 
Introducing MaxV - one click. infinite control. Hyper-V Hosting from MaximumASP.
Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Dynamic PDF
ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
SQL and .NET performance profiling in one place
Investigate SQL and .NET code side-by-side with ANTS Performance Profiler 6, so you can see which is causing the problem without switching tools.
Go.NET
Build custom interactive diagrams, network, workflow editors, flowcharts, or software design tools. Includes many predefined kinds of nodes, links, and basic shapes. Supports layers, scrolling, zooming, selection, drag-and-drop, clipboard, in-place editing, tooltips, grids, printing, overview window, palette. 100% implemented in C# as a managed .NET Control. Document/View/Tool architecture with many properties&events. Optional automatic layout.
Dundas Software
Dundas Chart for .NET is the most advanced .NET charting package available today.  With an extremely complete feature set, elegant architecture and easy implementation, Dundas Chart can quickly add advanced Charting functionality to enhance and transform ASP.NET and Windows Forms applications.  Whether you are implementing charting into internal projects, or building applications for clients, Dundas Chart offers advanced technology and advanced results to get the most out of data.
60 FREE UI Controls from DevExpress
Register for your FREE copy on over 60 free presentation controls from DevExpress - Absolutely Free-of-Charge without any royalties or distribution costs. Visit Devexpress.com/60 today. Free controls include advanced lists box, dropdown calendar, rich text edit, spin edit, tab control and so much more!

DevExpress engineers feature rich presentation controls and reporting tools for WinForms, ASP.NET, WPF, and Silverlight. Our technologies help you build your best, see complex software with greater clarity and deliver compelling business solutions for Windows and the web in the shortest possible time.
Clickatell's SMS Gateway
Clickatell's Developer Solutions allow you to SMS enable any website or application via a range of API's. Learn More about our API connections.
Free access to .NET Memory Management video
Everything you need to know about Garbage Collection, Temporary Objects, Fragmentation, Finalization and common causes of memory leaks in .NET. Watch the video here.
Microsoft Visual Studio 2010
Visualize your workspace with new multiple monitor support, powerful Web development, new SharePoint support with tons of templates and Web parts, and more accurate targeting of any version of the .NET Framework. Get set to unleash your creativity.
Nevron Chart for .NET 2010.1 Now Available
The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
Developer-Ready ASP.NET 2.0 Web Hosting with 3 MONTHS FREE
Now supporting .NET 3.0 Framework with Windows Workflow Foundation, Windows Communication Foundation (WCF), Windows Presentation Foundation (WPF), windows CardSpace (WCS)! Providing more flexibility for Developers with Web Services Support and a User/Permission Manger. Also supporting MS SQL 2005/2000 with Real-Time Backups, FREE Automated Attach .MDF Tool, FREE SQL Restore and Shrink SQL DB Tools, and SQL
Read the Top 10 Books for Microsoft Developers, 15 Days FREE
Read the Top 10 Books for Microsoft Developers, 15 Days FREE
Try Safari Books Online - 15 Days FREE + 15% Off for 1 Year
Try Safari Books Online - 15 Days FREE + 15% Off for 1 Year
 
 Post a Feedback, Comment, or Question about this article
Subject:
Comment:
Become a Sponsor
 Comments

 Hosted by MaximumASP  |  Found a broken link?  |  Contact Us  |  Terms & conditions  |  Privacy Policy  |  Site Map  |  Suggest an Idea  |  Media Kit
Current Version: 5.2010.8.14
 © 2010  contents copyright of their authors. Rest everything copyright Mindcracker. All rights reserved.