If you’re using App_Global resources to store your resource strings then you'll need to go through the Global Resource Object using GetGlobalResourceObject.
Label3.Text = (String)GetGlobalResourceObject( "Strings", "GlobalString");
Here’s what’s going on: ASP.NET 2.0 will dynamically generate types for any resources stored in App_GlobalResources. These type will be provided as references to any pages, controls, etc that ASP.Net 2.0 is compiling. Because Web Site projects in VS05 leverage ASP.NET 2.0 dynamic compilation they have access to the types.
Web Application Projects use the same compilation model as VS03, where the code behind files and other classes are compiled by VS and propped to ~/bin before the ASP.NET 2.0 compilation begins. Because the code behind files are compiled first they don’t have access early bound access to the dynamic types generated by ASP.NET. Instead in the code behind and other classes you’ll have to use late binding through the Global Resource Object.
The same problem happens for the profile object. Check out Appendix 2: Migrating Code that works with the ASP.NET 2.0 Profile Object in this tutorial http://webproject.scottgu.com/CSharp/Migration2/Migration2.aspx
In much the same way you could create a proxy object that provides early bound access to your resources then uses GetGlobalResourceObject to get the actual string.
Before we release the final version we’ll provide a tool that creates this proxy class for you.
As an alterative to using App_GlobalResources you can store resources in .resx files are part of your projects properties. This is the same technique that WinForms uses. Web Application Projects support accessing resources in the same way.
Label3.Text = Resources.MoreStrings.GlobalKey;
The one problem with this technique is that the class generated by ResXFileCodeGenerator in VS generates the class as private. That’s fine for all the code behind files and other support classes because their all in the same assembly. However, because the .aspx pages and .ascx controls are later compiled by ASP.NET they can’t access private classes in the code behind assembly.
There are a few techniques you can use to get around this problem if you want to use this technique to storing resources.
There is a custom tool named ResXFilePublicCodeGenerator that will generate the class as public.
http://altinoren.com/resxfilepubliccodegenerator.htm
Some users have provided work arounds using resgen in a build action:
http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=91371&SiteID=1
The other problem with storing resources in this fashion is that they are not accessible to expression builders in the IDE. This can be solved with a custom expression builder.
The technique you use will depend on your scenario:
-
If you’re migrating a VS03 application I’d continue to use the project Resource properties.
-
If you’re building a new ASP.NET 2.0 application I’d consider using App_GlobalResources with a proxy class to access the resources in the code behind files.
-
If you’re building a component that is redistributed into other web applications I’d consider using project resource properties of VS.
Admittedly, accessing resources and profiles is a bit confusing. We’re working to provide better support within VS but the key to understand the compilation model and when each file within the ASP.NET is compiled.
If you’re using Web Site projects that shipped in VS05 then you’re using dynamic compilation and the entire site is compiled by ASP.NET.
If you’re using Web Application Projects then the code behind files and other support classes are compiled by VS and propped to ~/bin. Then on a per request basis .aspx pages, .ascx controls, etc… along with all the App_* folders are compiled by ASP.NET on the fly.
I apologize for the long winded answer but I wanted to provide some context and alternatives.
Hope this helps,
Brad.