Click to Rate and Give Feedback
MSDN
MSDN Library
Visual Studio 2008
Visual Studio
 How to: Retrieve Resource Values Pr...
Collapse All/Expand All Collapse All
This page is specific to
Microsoft Visual Studio 2008/.NET Framework 3.5

Other versions are also available for the following:
ASP.NET
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.

    A local default resource file stored in the special App_LocalResources folder is named according to the ASP.NET page. For example, if the following code is used in a Default.aspx page, the resource file must be named Default.aspx.resx. For this example, add a string resource to this file named Button1.Text with the value "Found Resources".

    Also for this example, a global default resource file that is stored in the special App_GlobalResources folder is named WebResourcesGlobal.resx. Add a string resource named LogoUrl with the value http://go.microsoft.com/fwlink/?LinkId=49295 or the URL of another image.

    Visual Basic
    <%@ Page Language="VB" %>
    
    <script runat="server">
        Protected Sub Button1_Click( _
            ByVal sender As Object, ByVal e As System.EventArgs)
            Button1.Text = _
                GetLocalResourceObject("Button1.Text").ToString()
            Image1.ImageUrl = _
                CType(GetGlobalResourceObject("WebResourcesGlobal", _
               "LogoUrl"), String)
            Image1.Visible = True
        End Sub
    </script>
    
    <html  >
    <head runat="server">
        <title>Untitled Page</title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
            <asp:Button ID="Button1" runat="server" 
                OnClick="Button1_Click" 
                Text="Get Resources" />
            <asp:Image ID="Image1" runat="server" 
                Visible="false" />
        </div>
        </form>
    </body>
    </html>

    C#
    <%@ Page Language="C#" %>
    
    <script runat="server">
        protected void Button1_Click(object sender, EventArgs e)
        {
            Button1.Text = 
                GetLocalResourceObject("Button1.Text").ToString();
            Image1.ImageUrl = 
                (String)GetGlobalResourceObject(
                "WebResourcesGlobal", "LogoUrl");
            Image1.Visible = true;
        }
    </script>
    
    <html  >
    <head id="Head1" runat="server">
        <title>Untitled Page</title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
            <asp:Button ID="Button1" runat="server" 
                OnClick="Button1_Click" 
                Text="Get Resources" />
            <asp:Image ID="Image1" runat="server" 
                Visible="false" />
        </div>
        </form>
    </body>
    </html>

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:

    Visual Basic
    Dim welcome As String
    welcome = Resources.WebResources.WelcomeText

    C#
    String welcome;
    welcome = Resources.WebResources.WelcomeText;
Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
To retrieve global resources using strong typing      Dave Sexton   |   Edit   |   Show History

Note that you may need to qualify references with the global alias, as in the following example, because the namespace that is used by the code generator is just Resources, not [My.Default.Namespace.]Resources.

string welcome;
welcome = global::Resources.WebResources.WelcomeText;

Tags What's this?: Add a tag
Flag as ContentBug
Processing
© 2009 Microsoft Corporation. All rights reserved. Terms of Use | Trademarks | Privacy Statement | Site Feedback
Page view tracker