Themes for Custom Controls in ASP.NET

Friday, September 2, 2005

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.


Comments
Ben Thursday, January 12, 2006
You're a dude. Thanks for this one.
Dylan Friday, January 27, 2006
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?
Dylan Friday, January 27, 2006
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!
scott Friday, January 27, 2006
Interesting find, Dylan.
Grant Friday, June 9, 2006
Awesome info, worked like a charm. Thanks for taking the time to post a solution for this.
Deepak Aggarwal Wednesday, October 18, 2006
To the point solution.
Thaks a lot
Ego Friday, December 1, 2006
Dylan, thanks a lot! You saved me hours of headache.
Anantharaman Thursday, January 4, 2007
Thanks buddy.....
Trae Monday, June 28, 2010
Thanks Dylan, I was having the same issue. I dont think I would have discovered that.
Comments are now closed.
by K. Scott Allen K.Scott Allen
My Pluralsight Courses
The Podcast!