Sumedh Meshram

A Personal Blog

NAVIGATION - SEARCH

Get Value from Embedded Resource File

In ASP.Net it is better to used embedded resource for setting up the text for controls, message, etc for your web application. Add a App_GlobalResources folder and then add a resource file. Right-click on the resource file, go to properties and set the option as shown below:

Now add a class in your class library as :

using System;
using System.Globalization;
using System.Web;

namespace Common.Helper
{
    public static class ResourceHelper
    {
        const string ResourceBase = "WebUI";

        public static string GetResourceName(string resourceFile, string configKey, bool showWarningOnMissingKey)
        {
            return HttpContext.GetGlobalResourceObject(resourceFile, configKey) as string jQuery152025080017000436783_1326121562716
                           (showWarningOnMissingKey
                                ? string.Format("{0} not found in {1}.resx file", configKey, resourceFile)
                                : configKey);
        }

        public static string GetValue(string key)
        {
            CultureInfo culture = CultureInfo.CurrentCulture;
            //First try to load resource value from App_GlobalResources in case user is localizing control
            object val = HttpContext.GetGlobalResourceObject(ResourceBase, key, culture);
            if (val != null)
            {
                return val.ToString();
            }
            //If no value is found then load it from the embedded resource file (Localization/Resource.resx)
            Type type = HttpContext.Current.ApplicationInstance.GetType();
            if (type.BaseType != null)
            {
                System.Reflection.Assembly assembly = System.Reflection.Assembly.GetAssembly(type.BaseType);
                //string name = assembly.GetName().ToString();
                var myManager = new
                    System.Resources.ResourceManager("Web.UI.App_GlobalResources.WebUI", assembly);
                try
                {
                    return myManager.GetString(key, culture);
                }
                catch (Exception)
                {
                    return string.Format("{0} not found in {1}.resx file", key, "WebUI");
                }
            }
            return string.Format("{0} not found in {1}.resx file", key, "WebUI");
        }
    }
}

If you have a custom controls where you want to read from a resource file: 

txt.Text = ResourceHelper.GetValue(ConfigKey);

Also in you Web pages you can directly use as:

using System;
using System.Collections.Generic;
using System.Web.UI.WebControls;
using Resources;


namespace Web.UI.Reports
{
    public partial class ProspectCallback : System.Web.UI.Page
    {
        #region Page Events
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Page.Master != null)
            {
                var lblPageTitle = (Label)Page.Master.FindControl("lblPageTitle");
                if (lblPageTitle != null)
                {
                    lblPageTitle.Text = WebUI.PageTitle; //WeUI is namespace of resource file.
                }
            }            
        }
       
    }
}

Hope this helps.

Sumedh. 

Add comment

  Country flag

biuquote
  • Comment
  • Preview
Loading