Themes for Custom Controls in ASP.NET

Q: How do you skin custom controls and user controls in ASP.NET 2.0?

A: You can use an @ Register directive in the .skin file. Once the control is registered, skinning works just as you'd expect. For instance, the following control:

namespace OdeToCode.WebControls

{

    public class MyCalendar :

          System.Web.UI.WebControls.Calendar

    {

      // ...

    }

}

Can be skinned like so:

<%@ Register TagPrefix="OTC" namespace="OdeToCode.WebControls" %>

 

<OTC:MyCalendar runat="server" BackColor="White" BorderColor="#EFE6F7">

  <SelectedDayStyle BackColor="#41519A" ForeColor="White" />

  <SelectorStyle BackColor="#41519A" />

</OTC:MyCalendar>

The above Register directive assumes your control is defined in App_Code, otherwise you'll need to add the Assembly attribute to specify the assembly. It’s also important to note that a skin defined using <asp:Calendar> will not apply to the MyCalendar control, even though they have an inheritance relationship. The theme algorithm does an exact match on the control’s type.

posted on Thursday, September 01, 2005 8:38 PM by scott

Comments

Thursday, January 12, 2006 5:05 AM by Ben

# re: Themes for Custom Controls in ASP.NET

You're a dude. Thanks for this one.
Friday, January 27, 2006 2:06 PM by Dylan

# re: Themes for Custom Controls in ASP.NET

I'm getting a 'Type X is not defined error' when building; there seems to be some clash in the namespaces. Control X is in namespace Foo.Bar.A.B.Whatever and when I move it to Fu.Bar.A.B.Whatever, it works fine... Guaranteed, no other types with that name. Since all assemblies am referencing from my web project start with Foo (and no option of changing it either), I seem to have hit a road block... any thoughts?
Friday, January 27, 2006 2:47 PM by Dylan

# re: Themes for Custom Controls in ASP.NET

Got a solution of sorts: The problem was that the Theme containing the skin I was working on was also named 'Foo' - well, Microsoft never told us that giving a Theme the same name as a namespace would lead to a conflict, did they??? (unless it was in the small print somewhere...) ;-)

The solution? Rename the Theme!
Friday, January 27, 2006 2:55 PM by scott

# re: Themes for Custom Controls in ASP.NET

Interesting find, Dylan.
Thursday, June 08, 2006 8:46 PM by Grant

# re: Themes for Custom Controls in ASP.NET

Awesome info, worked like a charm. Thanks for taking the time to post a solution for this.
Tuesday, October 17, 2006 9:25 PM by Deepak Aggarwal

# re: Themes for Custom Controls in ASP.NET

To the point solution.
Thaks a lot
Friday, December 01, 2006 3:20 AM by Ego

# re: Themes for Custom Controls in ASP.NET

Dylan, thanks a lot! You saved me hours of headache.
Wednesday, January 03, 2007 11:29 PM by Anantharaman

# re: Themes for Custom Controls in ASP.NET

Thanks buddy.....
Friday, February 16, 2007 3:45 PM by Community Blogs

# ASP.NET 2.0 Themes and Custom Controls

Found an interesting one today on this old topic. As you know you can skin your custom controls by adding
Wednesday, February 21, 2007 11:13 PM by Wen Ching's Blog

# ASP.NET 2.0 Custom Control Skinning is Possible