Posted by: advapi | February 12, 2009

Extension metod for retrieving Localized Resource or ID if null

Hello to everybody,

it has been a long time since my last post about coding and I excuse for this… I hope you find the piece of code I’m gonna to write quite usefull….. consider this scenario :

  • You’re using localized resource placed inside .resx files
  • You’re under release and so you code a lot and hate (as me) to remember all the items that need to be placed inside the .resx
  • You can’t simply use the GetLocalResourceObject since if it there’s no entry you’ll come across an empty string…

If you’re facing all this points and you’re using .NET Framework 3.5… here’s the solution for you with a simple extension method :

He’re the PageExtension.cs code

public static class PageExtension

{

public static object GetLocalResourceEnhanced(this System.Web.UI.Page page, string resource)

{

object result = HttpContext.GetLocalResourceObject(page.AppRelativeVirtualPath, resource);

return result ?? resource;

}

}

and here’s the .aspx file :

<form id=”form1″ runat=”server”>
<div>
<label><% =this.GetLocalResourceEnhanced(“gino”)%></label>
</div>
</form>

With this if you put you label in uppercase as for example LBLWELLCOMETEXT you’ll see immediatly when some string are not present inside your .resx.

Hope it helped

Bests


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Categories

%d bloggers like this: