HOW TO:以程式設計方式擷取資源值

更新:2007 年 11 月

您可以使用宣告式語法將 ASP.NET 伺服器控制項屬性的值設為資源值。此外,您也可以程式設計方式擷取資源值。使用的時機是當資源值在設計階段還未知時,或您想要根據執行階段的狀況設定資源值時。

您可以從本機和全域資源檔取得資源值,這些資源檔使用的方法會傳回可以轉換為適當型別的物件。由於 ASP.NET 使用強型別編譯全域資源,因此您也可以使用強型別 (Strongly Typed) 成員取得全域資源。

若要以程式設計方式擷取資源值

  • 呼叫 GetLocalResourceObject 或 GetGlobalResourceObject 方法,以分別從全域或本機資源檔讀取特定資源。這些多載方法可以在 HttpContextTemplateControl 類別 (Class) 中使用。

    GetGlobalResourceObject 方法會使用資源類別的名稱和資源 ID。類別名稱是根據 .resx 檔名而定。例如,類別名稱 WebResources 會參考檔案 WebResources.resx 和所有關聯的當地語系化檔案。

    GetLocalResourceObject 方法會使用表示 ResourceKey 屬性的資源名稱。

    在下列程式碼中,示範了如何從本機和全域資源檔取得資源值。該方法會傳回物件,因此,您必須將資源轉換為適當的型別。

    儲存在特殊 [App_LocalResources] 資料夾中的本機預設資源檔是根據 ASP.NET Web 網頁命名的。例如,如果在 Default.aspx 網頁中使用下列程式碼,則資源檔必須命名為 Default.aspx.resx。在這個範例中,應使用值 "Found Resources" 將名為 Button1.Text 的字串資源加入這個檔案。

    同樣在這個範例中,儲存在特殊 [App_GlobalResources] 資料夾中的全域預設資源檔會命名為 WebResourcesGlobal.resx。所以應使用值 https://go.microsoft.com/fwlink/?LinkId=49295 或其他影像的 URL 加入名為 LogoUrl 的字串資源。

    <%@ Page Language="VB" %>
    
    <script >
        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 xmlns="http://www.w3.org/1999/xhtml" >
    <head >
        <title>Untitled Page</title>
    </head>
    <body>
        <form id="form1" >
        <div>
            <asp:Button ID="Button1"  
                OnClick="Button1_Click" 
                Text="Get Resources" />
            <asp:Image ID="Image1"  
                Visible="false" />
        </div>
        </form>
    </body>
    </html>
    
    <%@ Page Language="C#" %>
    
    <script >
        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 xmlns="http://www.w3.org/1999/xhtml" >
    <head id="Head1" >
        <title>Untitled Page</title>
    </head>
    <body>
        <form id="form1" >
        <div>
            <asp:Button ID="Button1"  
                OnClick="Button1_Click" 
                Text="Get Resources" />
            <asp:Image ID="Image1"  
                Visible="false" />
        </div>
        </form>
    </body>
    </html>
    

若要使用強型別擷取全域資源

  • 使用下列語法取得資源:

    Resources.Class.Resource
    

    資源會編譯至命名空間 Resources,並且每個預設資源都會成為 Resources 類別的成員。例如,如果您已建立預設資源檔 WebResources.resx,且該檔案包含名為 WelcomeText 的資源,則可以在程式碼中參考該資源,如下列程式碼範例所示:

    Dim welcome As String
    welcome = Resources.WebResources.WelcomeText
    
    String welcome;
    welcome = Resources.WebResources.WelcomeText;
    

請參閱

概念

ASP.NET Web 網頁資源概觀