ASP.NET Web Page Globalization & Localization in Master Page C# 3.0

强颜欢笑 提交于 2019-12-09 13:48:09

问题


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


回答1:


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


来源:https://stackoverflow.com/questions/8702296/asp-net-web-page-globalization-localization-in-master-page-c-sharp-3-0

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!