Wednesday, April 6, 2011

How to Access a Local Resource in a Theme from an ASP.net Page?

I have a resource file placed in the App_LocalResources folder inside the folder for a theme in App_Themes like so: \App_Themes\Theme1\App_LocalResources\Resources1.aspx.resx

How can I access the resources in this file from a web form in my project, assuming the web form is called Resources1.aspx having Theme="Theme1"?

From stackoverflow
  • You can use the "Resources" expression to extract values from the resource file, for instance:

    <h1><%$ Resources: H1 %></h1>
    

    Alternatively, and especially if you're in code-behind, use GetLocalResourceObject:

    h1.InnerText = GetLocalResourceObject ( "H1" ).ToString ( );
    

    EDIT: Sometimes i answer too fast; i don't think themes are localizable in that sense, however there are some workarounds. You could have theme specific items in the resource file and access them depending on current theme.

    urig : Thanks for the quick reply Dennis. I've tried the syntax you're suggesting but that does not seem to work for a resx file inside a Theme folder, even when the page is assigned that theme. Any other suggestions?
    baretta : I don't think you can have an theme-specific App_LocalResources directory. You would need to place App_LocalResources under same dir as aspx/ascx, and then maybe have keys in the resource file matching the theme, so you can that way pick up theme-specific resources, if you really need them.
    urig : Oddly enough, VS allows me to right-click on a Theme, then Add -> Add ASP.Net Folder -> App_LocalResources. And that's the only type of asp.net folder available for a theme. But no clue as to how to access any resx files in this folder...
    baretta : I really didn't know that. But since your'e telling me, i would guess it's possible to refer to resources in this file, from server markup in the .skin files. That's my guess, but probably these are not directly accessible from a aspx/ascx, but only in the actual skin file.
  • You can access globalresources located in \App_GlobalResources\Generic.resx with;

    <%= Resources.Generic.Cancel %>
    

    Local resources reside in an App_LocalResources folder which doesn't necessarily have to be be placed in the root folder. For a localresource it would be;

    <%$ Resources:Cancel%>
    

    or

    string labelCancel = GetLocalResourceObject("Cancel").ToString();
    

    As far as placing resource files in your theme folders go; I'd keep my themes and resources separated and programmatically switch between various resources in a site master/basepage or such by making use of globalresources.

    urig : Thanks for your help. At this rate I'll have no option but to separate the themes from the resources and choose the resource file programmatically. The downside is that I won't be able to use <%$ %> markup. :(

0 comments:

Post a Comment