Monday, May 7, 2007

IMAGES AND RESX

USE CASE: USE RESX FILE TO CONTROL LOOK AND FEEL OF THE SITE

Goal

To dynamically change display properties of the site.

Description

You want to build a web page that could be configured dynamically to display differently depending on the site logic (type of user, country etc…). To accomplish this you should separate display components with similar logic into user controls and abstract out behavioral properties into resource files.

Pattern

Put variables that control the behavior of the visible elements into resx files.

Use ASP.NET Placeholder construct to “turn off” parts of the web page. Put the control variables into resx files.

Scenarios

SCENARIO: Storing Text using literal VIA RESX FILES

Index.aspx

<title><asp:literal id="lit_Title" runat="server"/></title>

Index.en-ca.resx

<data name="lit_Title|Text">

<value>WeightWatchers: Start Losing Weight Today!</value>

</data>

SCENARIO: Storing Data VIA RESX FILES

ad_placeholder.ascx

<asp:literal id=ltlPageAdInfo runat="server" />

ad_placeholder…resx

<data name="ltlPageAdInfo|text">

<value>/shop/mag/index.aspx~/images/4105/wwmag_ad.gif**/util/news/index.aspx~/images/4105/newsletter_125x125_4.gif**</value>

</data>

ad_placeholder.ascx.vb

if staticAd.visible then

arrAdInfo = split( ltlPageAdInfo.text, "**" )

arrCurrAdInfo = split( thisAdInfo, "~" )

litStaticAd.text = "<a href=""" & adLink & """><img src=""" & adImage & """ border=0 width=125 height=125></a>"

end if

SCENARIO: USE ASP.NET PLACEHOLDER TO RENDER VISIBLE ELEMENTS

sc_findameeting.ascx

<%@ Control Language="vb" AutoEventWireup="false" Inherits="wwShortcuts.sc_findameeting" %>

<asp:placeholder id="phCA" runat="server">

<font class="small"><a href="/util/mtf/canada_meetings.aspx">Find a location near you</a></font><br>

</asp:placeholder>

<asp:placeholder id="phUSUK" runat="server">

<font class="small" >Enter a <%=str_ziptext%> code below</font>

<table id="table1" cellspacing="0" cellpadding="0" border="0" runat="server">

<tr><td colspan="3"></td></tr>

</table>

</asp:placeholder>

sc_findameeting…resx

<data name="phCA|Visible">

<value>blntrue</value>

</data>

<data name="phUSUK|Visible">

<value>blnfalse</value>

</data>

Anti-Patterns

ANTI-PATTERN: STORE HTML IN THE RESX FILE (1)

You should not put any html tags into resx files: