Resource Files in ASP.NET

A resource file is an XML file that contains the strings that you want to translate into different languages or paths to images.The resource file contains key/value pairs. Each pair is an individual resource. Key names are not case sensitive.

Types of Resources:
There are two types of resources in asp.net ,
  1. Local Resources
  2. Global Resources
    Local Resources 

    • Local resource is specific to a single Web page and used for providing versions of a Web page in different languages.
    • Local resources must be stored in App_LocalResources sub folder. for Example Default.aspx.resx.
      How to create local Resource file 

      for example you want and to create local resource file for Default.aspx page then open the page and select tool menu 

      Tools-->Generate Local Resources 
      this will create App_LocalResources folder with Text and ToolTip values for all existing controls on the page, as well as the page title will be generated.
      the file Default.aspx.resx. look like this .
       How to access the resource file 
      1. Access Resource file on Source file :- If you want to use an value from Default.aspx.resx on the lable use this syntex,                                                       <asp:Label ID="lblMessage" runat="server" Text="<%$ Resources:Default, PageResource1.Title%>"></asp:Label>                                                                   <%$ Resources:Class, ResourceID %>

        Where Class:
        Identifies the resource file to be used
        ResourceID: Identifier of the resource to be read from resource file                         
      2. Access Resource file on the .cs file :-If you want to use the resource file on .cs file use this syntex.  (string)GetLocalResourceObject("ResourceID"); 
      Global Resource File
      • Global resource can be read from any page or code that is in the application.
      • Global resource must be stored in App_GlobalResources at the root of the application.
        How to create Global file 
        Right click on Solution Explorer--> Add New Item-->Resource File
        How to use Global file
      1)On Source file :- Text="<%$ Resources:Default,Global, ResourceID%>">
      2)On .cs file :-String test=Resources.Global.ResourceID;


0 comments: