Last post Oct 05, 2007 02:43 AM by Kevin Yu - MSFT
Oct 02, 2007 03:18 AM|ivanpaloscia|LINK
Hi, I have a question regarding resource files. I am using Visual Studio 2003 and there is a resx file for each aspx file in my projects.
The first question is How can I retrieve resources from these files in order to use them in my aspx page?
The second question is, where do these files go after compilation?
I will wait for you replies.
Thank you in advance.
Oct 02, 2007 11:52 AM|jamlew|LINK
For local resources (under App_LocalResources; typically these are where you have one per .as*x), you can access them a couple ways:
1) Implicit localization - this requires adding a meta:resourcekey attribute to the control tag where the value of the attribute is the entry in the .resx file, e.g. <asp:Button ID="Button1" runat="server" meta:resourcekey="Button1Resource1" /> will load
the Button1Resource1.Text and Button1Resource1.Tooltip values from the .resx file.
2) Explicit localization - this just sets the property directly from the resource, rather than loading all associated ones, e.g. <asp:Button ID="Button2" runat="server" Text='<%$ Resources: ButtonText %>' /> will load the associated value from the .resx
If you use global resources (under App_GlobalResources; these are typically shared across different .as*x files), you use explicit localization, but you need to add the class name (e.g. <asp:Button ID="Button2" runat="server" Text='<%$ Resources: GlobalResourceClass, ButtonText
%>' will load the associated resource.
Resources are compiled into satellite assemblies dynamically, so you don't have to do anything about them. Rather, you can't really, since you don't even know the name of the assembly.
[edit:] I should mention: this is based on ASP.NET 2.0. If you're using 1.1, it's a different monster entirely. Also, MSDN might be useful either way.
Oct 05, 2007 02:43 AM|Kevin Yu - MSFT|LINK
The ASP.NET 1.1 does not support implicit or explicit localization like jamlew mentioned. In ASP.NET 1.1, you have to create a ResourceManager object and get the string from it with code. Here is a quick sample code.
ResourceManager rm = new ResourceManager("myApp.Resource1", a);
String s = rm.GetString("foo");