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

不想你离开。 提交于 2019-12-03 21:09:41

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