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.

Print | posted @ Friday, September 02, 2005 12:38 AM

Comments on this entry:

Gravatar # re: Themes for Custom Controls in ASP.NET
by Ben at 1/12/2006 1:05 PM

You're a dude. Thanks for this one.
  
Gravatar # re: Themes for Custom Controls in ASP.NET
by Dylan at 1/27/2006 10:06 PM

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?
  
Gravatar # re: Themes for Custom Controls in ASP.NET
by Dylan at 1/27/2006 10:47 PM

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!
  
Gravatar # re: Themes for Custom Controls in ASP.NET
by scott at 1/27/2006 10:55 PM

Interesting find, Dylan.
  
Gravatar # re: Themes for Custom Controls in ASP.NET
by Grant at 6/9/2006 3:46 AM

Awesome info, worked like a charm. Thanks for taking the time to post a solution for this.
  
Gravatar # re: Themes for Custom Controls in ASP.NET
by Deepak Aggarwal at 10/18/2006 4:25 AM

To the point solution.
Thaks a lot
  
Gravatar # re: Themes for Custom Controls in ASP.NET
by Ego at 12/1/2006 11:20 AM

Dylan, thanks a lot! You saved me hours of headache.
  
Gravatar # re: Themes for Custom Controls in ASP.NET
by Anantharaman at 1/4/2007 7:29 AM

Thanks buddy.....
  

Your comment:

Title:
Name:
Email:
Website:
 
Italic Underline Blockquote Hyperlink
 
 
Please add 2 and 8 and type the answer here: