Blue Theme Orange Theme Green Theme Red Theme
 
6 Months Free & No Setup Fees ASP.NET Hosting!
Home | Forums | Videos | Advertise | Certifications | Downloads | Blogs | Interviews | Jobs | Beginners | Training
 | Consulting  
Submit an Article Submit a Blog 
 Jump to
Skip Navigation Links
TechnologyExpand Technology
WebsiteExpand Website
Discover the top 5 tips for understanding .NET Interop
Search :       Advanced Search »
Home » ASP.NET & Web Forms » Using an ASP.Net Master Page with theme and CSS

Using an ASP.Net Master Page with theme and CSS

Using an ASP.Net master page with a theme and CSS can be tricky. In this article, we’ll cover the basics. In this article, we’ll cover the basics. Then we’ll add stylesheets for printing and for Internet Explorer only.

Page Views : 12902
Downloads : 0
Rating :
 Rate it
Level : Intermediate
   Print Read/Post comments Post a comment  Similar Articles  
   Email to a friend  Bookmark  Author's other articles  
 
DevExpress Free UI Controls
Become a Sponsor
 Tag Cloud
 Latest Jobs
More ... 
 Latest Interview Questions
More ... 


Using an ASP.Net master page with a theme and CSS can be tricky.  Themes and master pages came out in 2005.  There were hundreds of articles written about them at the time.  Since then, however, the way we do web development has changed a great deal.  It's time to readdress the subject.  In this article, we'll cover the basics.  Then we'll add stylesheets for printing and for Internet Explorer only.

The Problem

Suppose I want to make a website without .Net, one that's nothing but HTML and CSS.   My HTML head might look something like this:

image002.jpg

Notice I'm linking in three CSS stylesheets, style.css, print.css and ie.css. 

Style.css, is the main CSS file for my website.   The "media" attribute indicates that this CSS applies when my page is displayed on a computer screen or projector.

The media attribute for my second CSS file, print.css, indicates that this file contains special CSS which only applies when someone is printing my page.  I can add CSS here that will hide the header, footer, advertisements, etc., when the page is being printed.  I can define different fonts and font colors for printing.

The last CSS file, ie.css, contains CSS which only applies when my page is being viewed in Internet Explorer.   The link to the CSS file is contained within a comment block.   Internet Explorer will include this extra CSS file.  Other browsers will ignore it.  The file will contain special CSS hacks necessary to make my page display correctly in Internet Explorer.

So my issue is this:  I want a Master Page that will apply style.css as the primary CSS.  I also want to include ie.css when I'm using Internet Explorer.  I want to use print.css when I'm printing.  Going further, when I'm in Visual Studio looking at a page in designer view, I want the Studio to apply style.css, so I can get some idea how my page is going to look when it's done.

The Basics

I'm going to go through the basics very quickly.  There are hundreds of articles out there about how to create a master page and theme.   I'm only going to make a few points here.

First I created a website.  I added a folder called App_Themes.  Within this, I added a second folder called BlueTheme.  That's my theme for this site.  If I wanted to add other themes, I could add other folders, GreenTheme, ModernTheme, etc.  I have also added a Master Page called Basic.Master.

1.gif

In my Web.config file, I have specified that I want to use BlueTheme as my theme for this website.  The pages node was already in the Web.config file.  All I did was add the attribute, styleSheetTheme=BlueTheme.

2.gif

Note that I could have said theme=BlueTheme instead of styleSheetTheme =BlueTheme.  The main difference between these is whether the style from the theme applies first and then the styles added to a particular page can override these, or the opposite.  

Adding Style.css

Including style.css is simple.  I just place the file inside the BlueTheme folder.  Now that I've specified in the Web.config file that I want to use BlueTheme, any CSS files I put in the BlueTheme folder will be linked in and applied automatically.  I don't need to do anything to the Master Page. 


3.gif

Now, when I look at the Master Page in Visual Studio in designer view, it shows me (approximately) what my page looks like after applying style.css.

4.gif

I could include multiple CSS files this way just by adding them to the BlueTheme folder.  All the CSS files in the BlueTheme folder will be linked in and applied automatically.

CSS For Printing

I can't just put my print.css file in the BlueTheme folder.  All the CSS files in the BlueTheme folder will be linked in and applied, even if I'm not printing.  I only want the print.css file to be applied when I'm printing.  I need to  put my CSS file somewhere else.

I have added a new folder to my project, Css.  Inside this folder, I've added my print CSS file.  I renamed my file from print.css to BlueTheme_print.css so that I won't get confused if I add additional themes later.


5.gif

Now in my Master Page, I need to include a link to this CSS file: 

6.gif

Notice I didn't hard code the address for the CSS file.  Instead, I used <%# Page.ResolveUrl(  ) %> to link in the file at run time.   To make this work, the head tag must include runat="server."  Finally, in the code behind for the Master Page, I add one line of code, Page.Header.DataBind(); .


7.gif

Note that you must use <%# Page.ResolveUrl(  ) %>, not <%= Page.ResolveUrl(  ) %> .  That is, use #, not =.    This is an easy mistake to make, one which has caused hundreds of programmers to post frustrated questions to .Net forums looking for help.  If you put the wrong thing, you will get this error:


8.gif

CSS For Internet Explorer

As with the print CSS, I can't just include ie.css in the BlueTheme folder.  All the CSS files in the BlueTheme folder will be linked in and applied, regardless of what browser I'm using.  I only want the ie.css file to be applied in Internet Explorer.   I need to put my CSS file somewhere else.  I will put it with the print CSS file:


9.gif

Then I add a link to the file in my Master Page.  I include the same <%# Page.ResolveUrl( ) %> code as described above.  

10.gif

Testing

You can test your new website to make sure it's working.  Try adding the following to your print.css file:

p { color: Red; }

and this to your ie.css file:

p { color: Green; }

Now, when you pull up your page in a brower (not Internet Explorer), you should see your normal font.  If you switch to Internet Explorer, your font should be green.  If you do a print preview in any browser, your font should be red.  

Finally

The techniques I've described are not the only way to do things.  There are other ways to get the job done, each with its own advantages and disadvantages. The techniques I've described will get you started.  You can add your own tweaks and modifications to suit your own preferences.

Comment Request!
Thank you for reading this post. Please post your feedback, question, or comments about this post Here.
Login to add your contents and source code to this article
 Article Extensions
Contents added by Mau Mau on May 22, 2011
 bb
Contents added by maniche s on Mar 11, 2011
Using themes with multiple master page files.
 [Top] Rate this article
 
 About the author
 
Andrew Fenster
Andrew Fenster is an IT consultant in Cleveland, Ohio.  He has worked as a developer, technical lead or architect on many projects, large and small.  He has consulted for Progressive Insurance, Charles Schwab, OE Connection and National City Bank, among others. 
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.
Discover the Top 5 .NET Memory Management Fundamentals
To write the best .NET code, you need to know exactly how the .NET framework really manages memory. Ricky Leeks presents the Top 5 fundamental facts of .NET memory management. Learn more.
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.
ASP.NET 4 Hosting
Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites – Click Here!
 
 Post a Feedback, Comment, or Question about this article
Subject:
Comment:
Discover the top 5 tips for understanding .NET Interop
Become a Sponsor
 Comments
Team Foundation Server Hosting
 © 2012  contents copyright of their authors. Rest everything copyright Mindcracker. All rights reserved.