Question

I cant use the following code in Master page for Globalization & Localization. It gives the error as commented in the code part "does not contain a defination for InitializeCulture"

   protected override void InitializeCulture()
    {
        if (Request["Language"] != null)
        {
            //String selectedLanguage = Request["Language"];
           // code wil go here

        }
        base.InitializeCulture();
       //base.InitializeCulture gives error as mentioned in the next line
       //does not contain a defination for InitializeCulture
    }

When i add this code to other pages other than Master Page it works fine. is there any restriction on using this code in Master Page.

If i am able to define this code in Master Page then i dont need to write this code in every file.

Am i doing something wrong, I have include File for threading and Globalization, Still it doesn't work in Master Page

Was it helpful?

Solution

You have to do this (= override InitializeCulture) in your Page class. It doesn't work in the master page (MasterPage is derived from Control, not from page). I would suggest that you implement a base class which is derived from Page and derive every web form from this class, then you also have to write the code only once. It is always handy to have your own base class.

In Visual Studio you add a new class PageBase.cs:

public class FormBase : Page
{
   protected override InitializeCulture()
   {
      if (Request.Form["lbCulture"] != null)
      {
         String selectedLanguage = Request.Form["lbCulture"];
         UICulture = selectedLanguage;
         Culture = selectedLanguage;

         Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(selectedLanguage);
         Thread.CurrentThread.CurrentUICulture = new CultureInfo(selectedLanguage);
      }
      base.InitializeCulture();
   }
}

The current culture is either stored in some dropdown listbox, in the session or passed by query string. I used a listbox in the sample.

And then you derive your WebForm from this page like this:

public class Default : FormBase // instead of deriving from Page
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top