Can You Use A Global Resx File In ASP.Net For Both The CodeBehind And To Bind To Web Form Labels?

Viewed 88

I am interested in using only Global resource files in the "App_GlobalResources" folder and not having separate files in the "App_LocalResources" for web form labels.

I like the idea of centralizing all the resource strings in one place.

My resx files in App_GlobalResources have the following properties:

Build Action: Embedded Resource
Copy to Output Directory: Do not copy
Custom Tool: PublicResXFileCodeGenerator
Custom Tool Namespace: Resources

This works fine when calling a variable in markup such as:

<p><%=MyStrings.This_is_a_test_string%></p>

It also works fine in the code behind:

Dim strTest As String = MyStrings.This_is_a_test_string

The problem occurs when I attempt to bind a global resource string to a web form control such as:

<asp:Label ID="lblFullName" runat="server" Text="<%$ Resources:MyStrings, This_is_a_test_string %>" AssociatedControlID="txtFullName"></asp:Label>

The web server displays this error:

The resource object with key 'This_is_a_test_string' was not found.

I did notice if I change "Build Action" to "Content" that the resx will correctly bind to the Label control.

However, the problem is the strings will no longer work in the markup or the codebehind any longer.

So, is there a way to use ONE global resource file for the markup, codebehind, and for web form Label controls?

Or, am I forced to use two separate resource files?

At this point I'll admit that I'm thoroughly confused.

1 Answers

I wasn't able to figure out how to use a global .resx file that I could both use explicitly in the code behind such as:

Dim strTest As String = MyStrings.TestString

and also bind to a webform control such as:

<asp:Label ID="lblFullName" runat="server" Text="<%$ Resources:MyStrings, TestString %>" AssociatedControlID="txtFullName"></asp:Label>

Therefore, I decided the simplest solution was to use the global resx files I had and instead of binding them to web form controls I simply set the web form controls explicitly using their ID in the codebehind like so:

lblFullName.Text = MyStrings.FullName

What I did was put a placeholder in the actual control text using { } so I could see if I forgot to set any of the text such as:

<asp:Label ID="lblFullName" runat="server" Text="{THIS IS A TEST STRING}" AssociatedControlID="txtFullName"></asp:Label>

Related