OdeToCode IC Logo

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.