Skip to main content

Utiliser une ressource dans un thème ASP.NET (Tutoriel)

Les thèmes ASP.NET permettent de regrouper tout ce qui a trait à l'apparence d'un site pour pouvoir en changer rapidement. Pour des exemples, voir ici.

Le problème, c'est que dans une Skin on ne peut pas mettre d'expressions autres que <%# Eval %> et <%# Bind %>. Du coup on ne peut pas utiliser <%$ Resource ... %>. Devrait-on alors renoncer au multilinguisme? Non, et voici comment.

Dans cet exemple, je vais customiser l'apparence d'un GridView, et son en-tête va être traduit. Pour commencer, je déclare la grille dans la page ASPX:

<asp:GridView ID="GridView1" runat="server">
</asp:GridView>

Puis je place dans le fichier aspx.cs le code de remplissage de la grille:

protected void Page_Load(object sender, EventArgs e)
{
 CultureInfo[] list = CultureInfo.GetCultures(CultureTypes.NeutralCultures);
 GridView1.DataSource = list;
 GridView1.DataBind();
}

Pour permettre la traduction, je crée un contrôle ResouceLabel qui prend un nom de ressource et affiche la ressource traduite. ResourceLabel.ascx est le suivant:

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="ResourceLabel.ascx.cs" Inherits="ResourceLabel" %>
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>

Et ResourceLabel.ascx.cs:

public partial class ResourceLabel : System.Web.UI.UserControl
{
 public string Texte
 {
  set { Label1.Text = GetLocalResourceObject(value); }
 }
}

Il ne me reste plus qu'à créer un thème "Def" que je déclare dans le Web.config avec le contrôle fraîchement créé:

<system.web>
 <pages theme="Def">
  <controls>
   <add src="~/ResourceLabel.ascx" tagName="ResourceLabel" tagPrefix="uc1"/>
  </controls>
 </pages>
 ...
</system.web>

Pour finir, j'ajoute un fichier Grille.skin à mon thème, qui a le contenu suivant:

<asp:GridView runat="server" AutoGenerateColumns="False">
 <Columns>
  <asp:TemplateField>
   <HeaderTemplate>
    <uc1:ResourceLabel runat="server" Texte="A traduire" />
   </HeaderTemplate>
   <ItemTemplate>
    <asp:Label runat="server" Text='<%# Bind("EnglishName") %>'></asp:Label>
   </ItemTemplate>
  </asp:TemplateField>
 </Columns>
</asp:GridView>

Et le tour est joué! La chaîne "A traduire" apparaîtra traduite d'après le fichier RESX local de la page.

Comments

Popular posts from this blog

Gift idea : help your loved ones take their IT skills to the next level

You surely know a special person who works in IT. Learn Kubernetes & Docker enables them to quickly ramp up on those two technologies that one doesn't want to miss in 2021. With smoother operations, better scaling and availability, DevOps tooling, containers and containers orchestration smartly solve many problems that developers and system administrators commonly face. Don't let your loved ones face problems in their IT job : offer them the book that takes them from zero to productive in a matter of days. Grab it now! Get the ebook from Leanpub , Amazon or choose from many retailers . Get the print edition from Lulu .  

Learn Meteor book available

I'm pleased to announce the general release of my Learn Meteor book. It is now available as an ebook or print book from various sources: Learn Meteor print (paperback) on Lulu Learn Meteor ebook on LeanPub Learn Meteor ebook on Barnes & Noble Learn Meteor ebook on iBooks Learn Meteor ebook on Kobo Learn Meteor ebook on Scribd Learn Meteor ebook on Inktera Page Foundry Learn Meteor ebook on 24symbols Learn Meteor ebook on Amazon US Learn Meteor ebook on Amazon UK Learn Meteor ebook on Amazon France Learn Meteor ebook on Amazon Deutschland Learn Meteor ebook on Amazon Canada Learn Meteor ebook on Amazon India Learn Meteor ebook on Amazon Brasil Learn Meteor ebook on Amazon Mexico Learn Meteor ebook on Amazon España Learn Meteor ebook on Amazon Italia Learn Meteor ebook on Amazon Netherlands Learn Meteor ebook on Amazon Japan Learn Meteor ebook on Amazon Australia More sources are coming soon for the print version. Learn Meteor has been a fun expe...

Learn ASP.NET Core: read the book as it is being written

     TL;DR: get the discounted book now using the coupon , then get the full version for free. Thanks to the great acceptance of my previous books, ( Learn WPF MVVM ,  Learn ASP.NET MVC and Learn Meteor ), I'm in the process of writing a fourth one. Learn ASP.NET Core - MVC and DI with .NET Core 1.1 using Visual Studio 2017 teaches you how to quickly get coding using that technology. Just as I did previously, I'm publishing it before it is even finished. Would you like to read it? You can get it for half the price: 50% off the book using that coupon (limited to the first 100 readers and up to august 1st 2017): http://leanpub.com/netcore/c/K9mHH0IzfI2F About 50% of the book is already written, yet it is already available for download as an ebook: PDF, EPUB, MOBI: you choose your format. Over the next days I'll be writing the rest of the book, and publishing updates often. The final book will be ready by au...