views:

2617

answers:

4

Hi, I have a class in my asp.net proj, I would like to get access GetGlobalResourceObject (that page exposes), from anywhere in the site, possible?

In other words I wanna access the global resources from a class that is not a page I don't care how.

+2  A: 

Answer: Yes, as following pseudo:

Resources.<The name of the resources file name>.<your resource key>;

Example:

lblTitle.Text = Resources.MySettings.WebsiteTitle;

Resources is an Visual-Studio auto generated namespace that exposes all the global resource classes and props in the project.

Shimmy
A: 

If you are in the site you have access to HttpContext and can use:

HttpContext.GetGlobalResourceObject("myResourceKey")
Jimmy
I think Resources is better since it's strongly typed, rather than that object finction.thanks anyway
Shimmy
Also no need to do use hard-coded strings.
Shimmy
How can one avoid hard-coded strings?
MrFox
@MrFox use the answer above: http://stackoverflow.com/questions/1107906/how-do-i-access-getglobalresourceobject-function-from-a-class-that-is-not-a-page/1107915#1107915
Shimmy
+2  A: 

You should use

HttpContext.GetGlobalResourceObject("myResourceKey")

...because that way it will still work when using a custom ResourceProvider. The default type-generator for Resource files explicitely uses the Resx provider and won't work if you implement something like a database provider.

Simon Labrecque
A: 

I kinda took this from the resource designer,

ResourceManager temp = 
  new ResourceManager("Resources.<<resource name>>", 
    System.Reflection.Assembly.Load("App_GlobalResources"));
Jan
The code in the designer is used to initiate the static member, so the resource-assembly is loaded once per instance, then user accesses the tools given by MSFT to access the initiated member. Using your line will retrieve the resource over and over from the assembly, which should be avoided. Anyhow, the line you posted loads the ResourceManager and does not get the desired property which is what the OP wants.
Shimmy