Home > DeveloperSection > Beginner > Resources in .NET

Resources in .NET


.NET C#  ASP.Net 
Ratings:
0 Comment(s)
 1284  View(s)
Rate this:

Resources in .NET

 

In this article, I’m explaining about resources in .NET

What is Resources file?

A resources file is a XML file that contains the string that we want to:

a.       Translate into different language.

b.      Can be updated dynamically so that user themselves can modify values in resources files one the application is deployed on the server without re-compiling the entire application itself.                       

c.       The resource file contains key/value pairs.

d.      Each pair is an individual resources.

e.      Key name are not case sensitive.

Types of resources:

1.       Local resources

2.       Global resources

 

Local resources:

 

          1.       Local resources:

1.       Local resources is specific to a single web page and used for providing version of a web page in different language.

2.       Local resources must be stored in App_LocalResources sub folder.

3.       Local resources must be named in format <WebPageName>[.language/language and culture etc]

4.       Example: Default.aspx.resx- Base resource file.

 

 

2.       Global resources:

1.       Global resource can be read from any  page or code that is  in the application.

2.       Global resource must be stored in App_GlobalResources at the root of the application.

Example:

1.       Create a Default.aspx file.

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="DetailForm.aspx.cs" Inherits="ResourceSam.DetailForm"  Culture="auto" UICulture="auto"%>

 

<!DOCTYPE html>

 

<html xmlns="http://www.w3.org/1999/xhtml">

<head runat="server">

    <link href="css/bootstrap.css" rel="stylesheet" />

    <title></title>

</head>

<body>

    <form id="form1" runat="server">

       

        <table>

            <tr><td>Select language</td><td> <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True">

            <asp:ListItem Value="en-us">English</asp:ListItem>

            <asp:ListItem Value="hi-in">हिंदी</asp:ListItem>

  <asp:ListItem Value="Ur">اردو</asp:ListItem>

        </asp:DropDownList><br /></td></tr>

            <tr>

                <td> <asp:Label ID="lblinformation" runat="server" Text="Personal informatiom" meta:resourceKey="lblinformation"></asp:Label></td>

                <td>

            

             </td>

            </tr>

            <tr>

                <td> <asp:Label ID="lblFirstName" runat="server" Text="First Name"  meta:resourceKey="lblFirstName"></asp:Label></td>

                <td>

                    <asp:TextBox ID="txtFirstName" runat="server"></asp:TextBox></td>

            </tr>

            <tr>

                <td><asp:Label ID="lblLastName" runat="server" Text="Last Name" meta:resourceKey="lblLastName"></asp:Label></td>

                <td>

                    <asp:TextBox ID="txtLastName" runat="server"></asp:TextBox></td>

            </tr>

            <tr>

                <td><asp:Label ID="lblAge" runat="server" Text="Age" meta:resourceKey="lblAge"></asp:Label></td>

                <td>

                    <asp:TextBox ID="txtAge" runat="server"></asp:TextBox></td>

            </tr>

            <tr>

                <td colspan="2">

                    <asp:Button ID="btnReg" runat="server" Text="Register" meta:resourceKey="btnReg" /></td>

            </tr>

        </table>

    </form>

</body>

</html>

 

 

 

2.      Write in Default.cs

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Reflection;

using System.Resources;

using System.Globalization;

using System.Threading;

namespace ResourceSam

{

    public partial class DetailForm : System.Web.UI.Page

    {

        protected void Page_Load(object sender, EventArgs e)

        {

 

        }

        protected override void InitializeCulture()

        {

            if (Request.Form["DropDownList1"] != null)

            {

                UICulture = Request.Form["DropDownList1"];

                Culture = Request.Form["DropDownList1"];

            }

            base.InitializeCulture();

        }

       

    }

}

 

 

3.       Right click on application>> select Add Option>>select  Add ASP.NET Folder>>select>>App_LocalResources>>Right click on App_localResouces>> Select Resource file>>

1.       DetailForm.aspx.resx for English

2.       DetailForm.aspx.hi-in.resx for Hindi     

Output

 

 


Don't want to miss updates? Please click the below button!

Follow MindStick