How to: Retrieve Resource Values Programmatically

You can use declarative syntax to set the values of ASP.NET server control properties to a resource value. Alternatively, you can retrieve resource values programmatically. You might do this if the resource value is not known at design time or if you want to set the resource value based on a run-time condition.

You can get resource values from both local and global resource files that use methods that return an object that you can cast to the appropriate type. Because ASP.NET compiles global resources with strong typing, alternatively you can get global resources using strongly typed members.

To retrieve resource values programmatically

Call the GetLocalResourceObject or GetGlobalResourceObject method to read specific resources from a global or local resource file, respectively. These overloaded methods are available in the HttpContext and TemplateControl classes.

The GetGlobalResourceObject method takes the name of a resource class and the resource ID. The class name is based on the .resx file name. For example, the file WebResources.resx, and all associated localized files, are referenced by the class name WebResources.

The GetLocalResourceObject method takes a resource name representing a ResourceKey property.

The following code example shows how to get the value of a resource from a local and global resource file. The methods return an object; therefore, you must cast the resource to the appropriate type.

"C#" %>

To retrieve global resources using strong typing

Get the resource using the following syntax:

Resources.Class.Resource

Resources are compiled into the namespace Resources, and each default resource becomes a member of the Resources class. For example, if you have created the default resource file WebResources.resx and the file contains a resource named WelcomeText, you can reference the resource in code as shown in the following code example:

String welcome = Resources.WebResources.WelcomeText;

[@more@]

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/13651903/viewspace-1038448/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/13651903/viewspace-1038448/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值