masterpage initializeculture没有找到合适的方法来覆盖错误?
我正在尝试使用带有C#的ASP.NET开发一个MultiLanguage网站我的问题是:我想让我的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 Calendars = new Dictionary() { {"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(); } }
如何创建Base类?
方法InitializeCulture()
仅存在于Page类,而不是MasterPage类,这就是你得到该错误的原因。
要解决此问题,您可以创建一个所有特定页面都inheritance的BasePage
:
- 创建一个新类(而不是Webform),将其称为
BasePage
,或任何您想要的。 - 使其inheritanceSystem.Web.UI.Page 。
- 使所有其他页面inheritance
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. }
有一个替代解决方案,不要求您创建一个BasePage。
“文化”的问题在于它在页面生命周期中非常早地设置,因此Page.InitializeCulture
事件是页面上的早期事件之一(如果不是唯一的事件),我们可以在其中连接以更改Thread.CurrentThread.CurrentUICulture
。 但是,如果我们在服务器上开始请求时,甚至更早就这样做了。
我在Global.asax
文件上的Application_BeginRequest
事件上执行此操作,该事件在每个请求时都会调用。
protected void Application_BeginRequest(Object sender, EventArgs e) { HttpCookie cookie = Request.Cookies["langCookie"]; if (cookie != null && !string.IsNullOrEmpty(cookie.Value)) { Thread.CurrentThread.CurrentUICulture = new CultureInfo(cookie.Value); } }
在那里,我检查是否存在一个包含我想要使用的文化的cookie。 如果没有cookie,则将使用默认文化。
要更改我的应用程序上的语言,我只需要一个控件来更改客户端的cookie值,然后执行简单的回发到服务器。 如果此类控件位于内容页面或母版页中并不重要,它甚至不需要服务器端的任何代码,因为所有处理都是在上面的方法上完成的,并且cookie设置为即使在页面发布之前,客户端也是如此。
我使用了一个简单的LinkButton(它被设计为墨西哥国旗),但你可以使用任何其他控件在点击/更改时进行回发。
在此按钮发回服务器之前,它运行客户端点击事件,更新我想要设置的cookie值,瞧!
我有javascript代码在主页面部分设置cookie:
function SetLanguageCookie(selectedLanguage) { var expDate = new Date(); expDate.setDate(expDate.getDate() + 20); // Expiration 20 days from today document.cookie = "langCookie=" + selectedLanguage + "; expires=" + expDate.toUTCString() + "; path=/"; };
而已!! Thread.CurrentThread.CurrentUICulture
被更改,不需要任何BasePage
类,也不会覆盖Page.InitializeCulture
方法。 由于存储在cookie上,因此在访问后会记住所选语言的副作用。
如果要使用DropDownList
而不是LinkButton
,只需确保设置AutoPostBack="true"
并且由于DropDownList
没有OnClientChanged
属性,因此必须对DropDownList
上的onchange
属性进行硬编码并将所选值传递给相同的JavaScriptfunction。
onchange
属性不是DropDownList
属性的一部分,但是,由于DropDownList
是控件的模拟控件,因此在渲染发生时,属性只是“按原样”放置,并且在回发机制代码之前呈现。 这是上面的
DropDownList
呈现的HTML:
希望有人发现这种方法和我一样有用。 🙂