c# - 母版页 C# 3.0 中的 ASP.NET 网页全局化和本地化

标签 c# asp.net multilingual currentuiculture

我无法在全局化和本地化的母版页中使用以下代码。它给出了代码部分中注释的错误“不包含 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 派生的)。我建议您实现一个从 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();
   }
}

当前区域性要么存储在 session 中的某个下拉列表框中,要么通过查询字符串传递。我在示例中使用了列表框。

然后您可以从此页面派生 WebForm,如下所示:

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

关于c# - 母版页 C# 3.0 中的 ASP.NET 网页全局化和本地化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8702296/

相关文章:

c# - 在 xaml wpf 中使用 app.config 设置

javascript - 尝试将 CSS 中的链接与顶部标题居中但不会移动

c# - 是否可以在网络浏览器或框架中自动执行按钮点击和文本输入。如果有必要,甚至可以使用桌面工具?

wpf - 多语言 WPF 应用程序的方法

c - Windows Hook 保存语言字符

c# - 将 guildUserProperties.Mute 设置为 false 时,Bot 不会取消用户静音

c# - MxParser 和 nuget

c# - 类似于 .NET/C# 的 FindBugs

c# - 授权具有角色的属性不起作用?

php - 如何检测浏览器是否支持某种语言?