How to Get Global Resource File Values in Multiple Locations

Introduction

Resource files are very useful when you want to create a website in multiple languages (multilingual). Sometimes it is beneficial when you want to rewrite some text in multiple locations such as validation messages. They should be seen equally in all pages.

You can also use the resource file when a text is based on season or specific time period such as in educational websites you need to change the session, the current year or the previous year every year.

To learn more, here is the procedure.

Step 1

Create a website and name it "Test_Website".

web form
Step 2

Right-click on the website and choose Add and then Add New Item.

add new item

Add a resource file named "MyResources.resx".

resource file

Then it will give you a prompt message to save it in a new folder named "App_GlobalResources", it's a basic standard to save all the global resources in this folder, that ‘s why this message came here.

Just click on "Yes" and continue.

yes

Then you can see your resource file named "MyResources.resx" and the following 3 fields:

  1. Name: Key name of the text to indentify.
  2. Value: Value of that key.
  3. Comment: Comments to explain the purpose of this key.

MyResources

Step 3

The following are some sample keys with values.

Keys

Now to access the resource file values with their keys respectively in multiple locations.

  1. Inside the body tag of an .aspx page as text.
  2. Inside the body tag of an .aspx page as DropdownList values.
  3. In the JavaScript.
  4. In the code file or .cs file.
  5. Inside the body tag of .aspx page as Label Text
  6. Inside the body tag of .aspx page as Label Text concatenation with other string

I will now explain how to access the resource file values in the preceding 4 locations.

Note: All the resources can be accessed only by the namespace "Resources", here the resource file name "MyResources" is the file name and then the key name.

Inside Body tag of .aspx page as text

You need to use the server-side delimiters "<% %>" with the "=" sign to access the resource file values. Write the following code in the page named "Default.aspx".

  1. <h2><%=Resources.MyResources.Message %></h2>  
  2. <ul>   
  3.    <li>Current Session is <%=Resources.MyResources.Session %></li>  
  4.    <li>Last Year is <%=Resources.MyResources.PrevYear %></li>  
  5.    <li>Current Year is <%=Resources.MyResources.CurrentYear %></li>  
  6.    <li>Next Year is <%=Resources.MyResources.NextYear %></li>  
  7. </ul>  
Inside Body tag

And that we will look like at the following from a successful run.

page

Inside Body tag of .aspx page as DropDownList values

You need to use server-side delimiters "<% %>" with "$" sign to access the resource file values in a Dropdown list. Write the following code in the page named "Default.aspx". 
  1. Year <asp:DropDownList ID="Myddl" runat="server">  
  2.          <asp:ListItem Text="<%$ Resources:MyResources, PrevYear%>" Value="<%$ Resources:MyResources, PrevYear%>"></asp:ListItem>  
  3.          <asp:ListItem Text="<%$ Resources:MyResources, CurrentYear%>" Value="<%$ Resources:MyResources, CurrentYear%>"></asp:ListItem>  
  4.          <asp:ListItem Text="<%$ Resources:MyResources, NextYear%>" Value="<%$ Resources:MyResources, NextYear%>"></asp:ListItem>  
  5. </asp:DropDownList>  
script

And that will look the following from a successful run.

year

JavaScript

You need to use server-side the delimiters "<% %>" with "=" sign to access the resource file values in JavaScript code. Write the following code in the "head" tag of the page named "Default.aspx".
  1. <script type="text/javascript">  
  2.    var Session = '<%= Resources.MyResources.Session%>';  
  3.    alert('My Current Session is ' + Session);  
  4. </script>  
session

And that will look as in the following from a successful run.

run

Code file/ .cs file

There is no need for server-side delimiters "<% %>" to access the resource file values in the code file, you can access them directly. Write the following code in the page_Load event of the code file named "Default.aspx.cs". 
  1. Response.Write(Resources.MyResources.Message+"<br/>");  
  2. Response.Write(Resources.MyResources.Session + "<br/>");  
  3. Response.Write(Resources.MyResources.PrevYear + "<br/>");  
  4. Response.Write(Resources.MyResources.CurrentYear + "<br/>");   
  5. Response.Write(Resources.MyResources.NextYear + "<br/>");  
Default

And that will look as in the following from a successful run. 

Inside the body tag of .aspx page as Label Text

You have to use server side Delimiters ’<% %>’ with ‘$‘ sign to access the resource file values in Label ‘Text’ property like Dropdown list. Write the following Code in the page named ‘Default.aspx’.

  1. <asp:Label ID="Mylabel" runat="server" Text=" <%$ Resources:MyResources, Message %>" />  

  

And that we will look like at the following from a successful run.


Inside the body tag of .aspx page as Label Text concatenation with other string

You have to use server side Delimiters ’<% %>’ with ‘=‘ sign to access the resource file values in Label but not in ‘Text’ property. You have to access it in between Label start and ending tag. Write the following Code in the page named ‘Default.aspx’.

  1. <asp:Label ID="Mylabel" runat="server">  
  2.   
  3.    <%= "before Message String- "+ Resources.MyResources.Message +" -After Message String " %>  
  4.   
  5. </asp:Label>  

 
 
  And that we will look like at the following from a successful run.
 
 

Conclusion: I hope now that you can implement the resource file in your project and access its values in multiple locations in a simple way.