Tuesday, February 19, 2013

Retrieving a resource key in a custom LayoutsPageBase page used in SP2010

1. Create the following property in your aspx.cs file:




protected Type ResourceType
{
     get
     {
          return typeof(MyResources); // replace 'MyResources' with your own resources file
     }
}




2. Create the following method which will return the requested value stored in the .resx file by looking up the resource key



protected string GetResourceString(Type resourceType, string resourceKey)
{
     return SPUtility.GetLocalizedString(string.Format("$Resources:{0}, {1}", resourceType.Name, resourceKey), resourceType.FullName, getLanguage);
}


3. Good to go. Say you would need to populate a label in the Page_Load event with a string stored in the resources file with a key called 'MyLabelText'; you can then achieve this as follows:



protected void Page_Load(object sender, EventArgs e)
{
     lblMyLabel.Text = GetSourceString(ResourceType, "MyLabelText");
}

No comments:

Post a Comment