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".
Step 2
Right-click on the website and choose Add and then Add New Item.
Add a resource file named "MyResources.resx".
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.
Then you can see your resource file named "MyResources.resx" and the following 3 fields:
- Name: Key name of the text to indentify.
- Value: Value of that key.
- Comment: Comments to explain the purpose of this key.
Step 3
The following are some sample keys with values.
Now to access the resource file values with their keys respectively in multiple locations.
- Inside the body tag of an .aspx page as text.
- Inside the body tag of an .aspx page as DropdownList values.
- In the JavaScript.
- In the code file or .cs file.
- Inside the body tag of .aspx page as Label Text
- 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".
- <h2><%=Resources.MyResources.Message %></h2>
- <ul>
- <li>Current Session is <%=Resources.MyResources.Session %></li>
- <li>Last Year is <%=Resources.MyResources.PrevYear %></li>
- <li>Current Year is <%=Resources.MyResources.CurrentYear %></li>
- <li>Next Year is <%=Resources.MyResources.NextYear %></li>
- </ul>
And that we will look like at the following from a successful run.
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".
- Year <asp:DropDownList ID="Myddl" runat="server">
- <asp:ListItem Text="<%$ Resources:MyResources, PrevYear%>" Value="<%$ Resources:MyResources, PrevYear%>"></asp:ListItem>
- <asp:ListItem Text="<%$ Resources:MyResources, CurrentYear%>" Value="<%$ Resources:MyResources, CurrentYear%>"></asp:ListItem>
- <asp:ListItem Text="<%$ Resources:MyResources, NextYear%>" Value="<%$ Resources:MyResources, NextYear%>"></asp:ListItem>
- </asp:DropDownList>
And that will look the following from a successful run.
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".
- <script type="text/javascript">
- var Session = '<%= Resources.MyResources.Session%>';
- alert('My Current Session is ' + Session);
- </script>
And that will look as in the following from a successful 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".
- Response.Write(Resources.MyResources.Message+"<br/>");
- Response.Write(Resources.MyResources.Session + "<br/>");
- Response.Write(Resources.MyResources.PrevYear + "<br/>");
- Response.Write(Resources.MyResources.CurrentYear + "<br/>");
- Response.Write(Resources.MyResources.NextYear + "<br/>");
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’.
- <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’.
- <asp:Label ID="Mylabel" runat="server">
-
- <%= "before Message String- "+ Resources.MyResources.Message +" -After Message String " %>
-
- </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.