主页C#3.0中的ASP.NET网页全球化和本地化

我无法在Master页面中使用以下代码进行全球化和本地化。 它在代码部分中给出了错误,“不包含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 } 

当我将此代码添加到母版以外的其他页面时,它可以正常工作。 在母版页中使用此代码是否有任何限制。

如果我能够在母版页中定义此代码,那么我不需要在每个文件中编写此代码。

我做错了什么,我已经包含文件用于线程和全球化,但它在母版页中不起作用

您必须在Page类中执行此操作(=覆盖InitializeCulture)。 它在母版页中不起作用(MasterPage是从Control派生的,而不是从页面派生的)。 我建议您实现一个派生自Page的基类,并从该类派生每个Web表单,然后您还必须只编写一次代码。 拥有自己的基类总是很方便。

在Visual Studio中,添加一个新类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(); } } 

当前文化要么存储在某个下拉列表框中,要么存储在会话中,要么通过查询字符串传递。 我在示例中使用了一个列表框。

然后你从这个页面派生你的WebForm,如下所示:

 public class Default : FormBase // instead of deriving from Page