Friday, January 29, 2010

Skinning a User Control

It's been a while since my last post, and while I have many ideas for posts, I have opted to spend my hours either working or enjoying my family; however, I came across a case today which will take only a second to blog about.

ASP.Net supports skinning and themes. If you don't know what that is I suggest you go search on it. In a site I am working on we have a User Controls, which are being used as Web Parts. A new requirement came up that depending on the theme we are using we will want to change the Title of the web part. So I fire up Visual Studio, and first thing was to add a new item to my skin file:

Navigating to the page resulted in the YSD (Yellow Screen of Death) with an error indicating that myControl couldn't be themed. The next step was to go and add a Themable attribute on the properties in the control I want to skin. You also need to add Themable to the class as well so you end up with:

[Themable(true)]
public class myControl : System.Web.UI.UserControl
{
     [Themable(true)]
     public string Title {get;set;}
}

At this point you can not skin your user controls. One thing I did skip is to make sure the skin knows about the user control. You can do this by registering it in the web.config by adding a record to the Pages\Controls section as follows:
 

You can also register it on the skin using the following declaration:

<%@ Register TagPrefix="myComp" TagName="myControl" src="~/Controls/MyControl.ascx" %>