masterpage initializeculture no suitable method found to override error?(masterpage initializeculture 找不到合适的方法来覆盖错误?)
问题描述
我正在尝试使用带有 C# 的 ASP.NET 开发多语言网站我的问题是:我想让我的 MasterPage 支持语言之间的切换,但是当我将InitializeCulture()"放入 masterpage.cs 时,我得到了这个错误.
I'm trying to develop a MultiLanguage web site using ASP.NET with C# My problem is: I want to make my MasterPage support switching among languages, but when i put the "InitializeCulture()" inside the masterpage.cs, I got this error.
这是我的代码:
public partial class BasicMasterPage : System.Web.UI.MasterPage
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Calendar1_DayRender(object sender, DayRenderEventArgs e)
{
if (e.Day.IsToday)
{
e.Cell.Style.Add("background-color", "#3556bf");
e.Cell.Style.Add("font-weight", "bold");
}
}
Dictionary<string, System.Globalization.Calendar> Calendars =
new Dictionary<string, System.Globalization.Calendar>()
{
{"GregorianCalendar", new GregorianCalendar()},
{"HebrewCalendar", new HebrewCalendar()},
{"HijriCalendar", new HijriCalendar()},
{"JapaneseCalendar", new JapaneseCalendar()},
{"JulianCalendar", new JulianCalendar()},
{"KoreanCalendar", new KoreanCalendar()},
{"TaiwanCalendar", new TaiwanCalendar()},
{"ThaiBuddhistCalendar", new ThaiBuddhistCalendar ()}
};
protected override void InitializeCulture()
{
if (Request.Form["LocaleChoice"] != null)
{
string selected = Request.Form["LocaleChoice"];
string[] calendarSetting = selected.Split('|');
string selectedLanguage = calendarSetting[0];
CultureInfo culture = CultureInfo.CreateSpecificCulture(selectedLanguage);
if (calendarSetting.Length > 1)
{
string selectedCalendar = calendarSetting[1];
var cal = culture.Calendar;
if (Calendars.TryGetValue(selectedCalendar, out cal))
culture.DateTimeFormat.Calendar = cal;
}
Thread.CurrentThread.CurrentCulture = culture;
Thread.CurrentThread.CurrentUICulture = culture;
}
base.InitializeCulture();
}
}
如何创建基类?
推荐答案
InitializeCulture()方法只存在于Page 类,而不是 MasterPage 类,这就是你得到这个错误的原因.
The method InitializeCulture() exists only on the Page class, not the MasterPage class, and that's why you get that error.
要解决这个问题,您可以创建一个 BasePage 让您的所有特定页面都继承:
To fix this, you could create a BasePage that all your specific pages inherit:
- 创建一个新类(不是 Webform),将其命名为
BasePage或任何您想要的名称. - 使其继承System.Web.UI.Page.
- 让所有其他页面继承
BasePage.
- Create a new Class (not Webform), call it
BasePage, or whatever you want. - Make it inherit System.Web.UI.Page.
- Make all your other pages inherit the
BasePage.
这是一个例子:
public class BasePage : System.Web.UI.Page
{
protected override void InitializeCulture()
{
//Do the logic you want for all pages that inherit the BasePage.
}
}
具体的页面应该是这样的:
And the specific pages should look something like this:
public partial class _Default : BasePage //Instead of it System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//Your logic.
}
//Your logic.
}
这篇关于masterpage initializeculture 找不到合适的方法来覆盖错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:masterpage initializeculture 找不到合适的方法来覆盖错误?
基础教程推荐
- 更新 Visual Studio 中的 DataSet 结构以匹配新的 SQL 数据库结构 2022-01-01
- 从 C# 控制相机设备 2022-01-01
- SonarQube C# 分析失败“不是指针的有效行偏移" 2022-01-01
- 在 VB6 或经典 ASP 中使用 .NET 2022-01-01
- Mono https webrequest 失败并显示“身份验证或解密失败" 2022-01-01
- 将数据集转换为列表 2022-01-01
- C# 9 新特性——record的相关总结 2023-04-03
- 获取C#保存对话框的文件路径 2022-01-01
- 如果条件可以为空 2022-01-01
- 重新排序 WPF TabControl 中的选项卡 2022-01-01
