gpt4 book ai didi

c# - masterpage initializeculture 找不到合适的方法来覆盖错误?

转载 作者:可可西里 更新时间:2023-11-01 08:37:48 33 4
gpt4 key购买 nike

我正在尝试使用 ASP.NET 和 C# 开发多语言网站我的问题是:我想让我的 MasterPage 支持在语言之间切换,但是当我将“InitializeCulture()”放入 masterpage.cs 时,我收到了这个错误。

这是我的代码:

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类,这就是您收到该错误的原因。

要解决此问题,您可以创建一个 BasePage,所有特定页面都将继承:

  1. 创建一个新类(不是 Webform),将其命名为 BasePage,或任何您想要的名称。
  2. 让它继承System.Web.UI.Page .
  3. 让所有其他页面继承 BasePage

这是一个例子:

public class BasePage : System.Web.UI.Page
{
protected override void InitializeCulture()
{
//Do the logic you want for all pages that inherit the BasePage.
}
}

具体的页面应该是这样的:

public partial class _Default : BasePage //Instead of it System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//Your logic.
}

//Your logic.
}

关于c# - masterpage initializeculture 找不到合适的方法来覆盖错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11805897/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com