問題描述
我正在嘗試使用帶有 C# 的 ASP.NET 開發(fā)多語言網(wǎng)站我的問題是:我想讓我的 MasterPage 支持語言之間的切換,但是當(dāng)我將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();
}
}
如何創(chuàng)建基類?
推薦答案
InitializeCulture()
方法只存在于Page 類,而不是 MasterPage 類,這就是你得到這個錯誤的原因.
The method InitializeCulture()
exists only on the Page class, not the MasterPage class, and that's why you get that error.
要解決這個問題,您可以創(chuàng)建一個 BasePage
讓您的所有特定頁面都繼承:
To fix this, you could create a BasePage
that all your specific pages inherit:
- 創(chuàng)建一個新類(不是 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.
}
}
具體的頁面應(yīng)該是這樣的:
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.
}
這篇關(guān)于masterpage initializeculture 找不到合適的方法來覆蓋錯誤?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!