Welcome to EMC Consulting Blogs Sign in | Join | Help

Simon Evans' Blog

My blog covers the technology areas I focus on here at EMC Consulting, namely Architecture using the .NET Framework, ASP.NET, WCF, WCF Data Services and Windows Azure Follow me on twitter @simonevans

How to programmatically assign a SkinID to a control while using a master page in ASP.net 2.0

Using themes in ASP.net allows you to "skin" the look and feel of a web site, much like CSS, but with more control, because now you can affect the rendering of the HTML in server controls.

The recommended use of a theme is to put as much as possible in the CSS, which can be used in conjuction with the theme folder. In fact, the theme has a nice built in feature of automatically writing the <link> markup to your ASPX page automatically for the CSS file you place within the theme.

A theme can contain many ways to render the same server side control. Each possible way of rendering the control is identified by a SkinID property in the theme, which you then assign to your instance of the control in your page or masterpage.

There are two main ways of assigning both a page theme and its skinID's to the controls within your page: either declaritively in your markup, or programmatically in your code. Assigning skinID's in your markup is great, unless you want a bit more control over the SkinID to assign. For example, you may want to assign a SkinID dynamically based on settings in the users Profile object.

So, how do you do this? Well, the answer is you must assign the theme and skinID of controls early on in the page lifecycle, in the PreInit event. This event fires before initialzation of the controls begins, meaning that the theme is able to effect the render before the render is started. All this makes sense, and all is well in the world. The following code sample demostrates this:

    void Page_PreInit(object sender, EventArgs e)
    {
        Calendar1.SkinID = "MySkin";
    }

Now, what happens if you try this applying a masterpage at the same time? You get an "object reference not set to an instance of an object" exception, and when examine the event, it is clear why this happening - all the controls in the PreRender event are set to null - ie not instantiated yet.

What is going on here? This is currently an undocumented feature of using Masterpages with skins. When you use apply a masterpage to your page, the controls in your page are not available until the control tree of the masterpage has been instantiated. Thus, when you access PreInit on the page, nothing is instantiated yet, because the Masterpages controls are not available.

So what is the answer? Well, the PreInit event must now look like this:

    void Page_PreInit(object sender, EventArgs e)
    {
         System.Web.UI.MasterPage m = Master;
         Calendar1.SkinID = "MySkin";
    }

Published 09 June 2005 00:18 by simon.evans
Filed under: ,

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

 

jitu said:

While using a master page, this was a good information.

April 3, 2007 11:26
 

Righthand blogs said:

Today I've come across an odd problem. I had to set SkinID property of a control dynamically and this

June 1, 2007 14:44
 

ronald said:

would it be possible to call a selected theme in masterpage??

any other option instead of page_preinit??

November 16, 2007 06:10
 

NeedHelp said:

Ufrotunately, this trick doesn't seem to work if the control you are trying to access is also inside an UpdatePanel, any suggestions?

March 23, 2008 01:37
 

Anonymous said:

Hello!

March 27, 2008 12:18
 

skinid said:

August 2, 2008 15:26
 

ronald said:

Suck it!

October 22, 2008 05:53
 

Liz said:

Thank you so much. I have been searching for hours trying to find out why I couldn't dynamically assign a SkinID. I added the line of code and now it works wonderfully. How does "System.Web.UI.MasterPage m = Master" help anyway?

December 9, 2008 22:42
 

Emil said:

Thanks! Saved my day :)

May 7, 2009 16:38
 

JD said:

Great tip! thanks very usefull.

September 1, 2009 15:53
 

Oktay Sezgin said:

Simon, I have exactly the same issue. however your solution here is not working in my case. I have a page within master page and I need to set a controls SkinID in Page_PreInit. But i still set a null reference for the control variable..

any other ideas?

Thanks

January 9, 2010 16:26
 

Suresh Dasari said:

Good! thanks.

April 2, 2010 11:21
 

Tom Lamp said:

I'm having this same problem, but the fix doesn't work.  Main difference -- we use two layers of master pages: a common root master and then a categorical sub-master.

Thanks in advance for any ideas.

April 23, 2010 20:19
 

Sabrina said:

How to make this work with nested master pages?

February 15, 2011 22:34
 

Faiyaz said:

Wow it helped, System.Web.UI.MasterPage m = Master is magic. Searching for long time and answer was here. Thanks for the author.

June 20, 2011 06:00
 

R1 said:

Thanks alot

Your article saves my 2 hours hard work :)

November 29, 2011 13:27
 

m said:

thanks

December 29, 2011 16:24
 

Måns said:

Tack!

January 9, 2012 15:43

Leave a Comment

(required) 
(optional)
(required) 
Submit

About simon.evans

Simon is a Managing Consultant for Conchango in the UK, part of EMC Consulting Services. He is an expert in .NET development, and more specifically in WCF and ASP.NET, having participated in several Microsoft early adoption programs. Simon believes deeply that a broad understanding of key technology concepts is an essential foundation to being a gifted designer and builder of solutions.
Powered by Community Server (Personal Edition), by Telligent Systems