Laurent Duveau
Appliquer les Skins à vos Custom Controls
Voici une petite astuce pour montrer comment profiter des Skins dans vos Custom Controls
Par Laurent Duveau publié le 20/02/2006 à 08:57, lu 9944 fois,
Vous avez certainement eu l'occasion de pratiquer les Themes d'ASP.NET 2.0 et vous avez pu constater que les Skins ne s'appliquent pas à vos custom controls, même s'ils dérivent tout simplement d'un WebControl de base.
En effet le système de Theme ne s'applique que aux WebControls standards <asp:...> et ne prend pas en compte l'héritage.

Prenons un exemple, vous avez besoin d'un calendrier personnalisé et vous définissez donc votre custom control :
namespace TechHeadBrothers.Web.UI.WebControls
{
    public class MyCalendar : System.Web.UI.WebControls.Calendar
    {
      // ...
    }
}
Puis vous définissez un style dans votre fichier Skin :
<asp:Calendar runat="server" BackColor="#FFFFCC" BorderColor="#FFCC66"
            BorderWidth="1px" DayNameFormat="Shortest" Font-Names="Verdana" Font-Size="8pt"
            ForeColor="#663399" Height="200px" ShowGridLines="True" Width="220px">
    <SelectedDayStyle BackColor="#CCCCFF" Font-Bold="True" />
    <TodayDayStyle BackColor="#FFCC66" ForeColor="White" />
    <SelectorStyle BackColor="#FFCC66" />
    <OtherMonthDayStyle ForeColor="#CC9966" />
    <NextPrevStyle Font-Size="9pt" ForeColor="#FFFFCC" />
    <DayHeaderStyle BackColor="#FFCC66" Font-Bold="True" Height="1px" />
    <TitleStyle BackColor="#990000" Font-Bold="True" Font-Size="9pt" ForeColor="#FFFFCC" />
</asp:Calendar>
Mais là problème, vous testez l'application telle quelle et aucun résultat n'est visible :



Et si vous essayez de déclarer un skin pour votre custom control comme ceci :
<THB:MyCalendar runat="server" BackColor="#FFFFCC" BorderColor="#FFCC66"
            BorderWidth="1px" DayNameFormat="Shortest" Font-Names="Verdana" Font-Size="8pt"
            ForeColor="#663399" Height="200px" ShowGridLines="True" Width="220px">
    <SelectedDayStyle BackColor="#CCCCFF" Font-Bold="True" />
    <TodayDayStyle BackColor="#FFCC66" ForeColor="White" />
    <SelectorStyle BackColor="#FFCC66" />
    <OtherMonthDayStyle ForeColor="#CC9966" />
    <NextPrevStyle Font-Size="9pt" ForeColor="#FFFFCC" />
    <DayHeaderStyle BackColor="#FFCC66" Font-Bold="True" Height="1px" />
    <TitleStyle BackColor="#990000" Font-Bold="True" Font-Size="9pt" ForeColor="#FFFFCC" />
</THB:MyCalendar>
Vous obtenez l'erreur suivante :
  • "Unknown server tag 'THB:MyCalendar'."
Bien, maintenant pour que ça fonctionne - il fallait juste le savoir car ce n'est pas très documenté - il vous suffit d'ajouter la directive Register qui va bien dans votre fichier Skin, soit :

Pour un contrôle situé dans le répertoire /App_Code :
<%@ Register TagPrefix="THB" Namespace="TechHeadBrothers.Web.UI.WebControls" %>
Pour un contrôle situé dans une assembly TechHeadBrothers.Web.UI.dll du répertoire /bin :
<%@ Register TagPrefix="THB" Namespace="TechHeadBrothers.Web.UI.WebControls" Assembly="TechHeadBrothers.Web.UI" %>
Vous pouvez désormais utiliser la syntaxe précédente dans votre fichier skin :
<THB:MyCalendar runat="server" BackColor="#FFFFCC" BorderColor="#FFCC66"
            BorderWidth="1px" DayNameFormat="Shortest" Font-Names="Verdana" Font-Size="8pt"
            ForeColor="#663399" Height="200px" ShowGridLines="True" Width="220px">
    <SelectedDayStyle BackColor="#CCCCFF" Font-Bold="True" />
    <TodayDayStyle BackColor="#FFCC66" ForeColor="White" />
    <SelectorStyle BackColor="#FFCC66" />
    <OtherMonthDayStyle ForeColor="#CC9966" />
    <NextPrevStyle Font-Size="9pt" ForeColor="#FFFFCC" />
    <DayHeaderStyle BackColor="#FFCC66" Font-Bold="True" Height="1px" />
    <TitleStyle BackColor="#990000" Font-Bold="True" Font-Size="9pt" ForeColor="#FFFFCC" />
</THB:MyCalendar>
Et on obtient bien le résultat recherché sur notre custom control :


 
» Démarrer une discussion