我正在尝试使用带有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<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();
}
}
Run Code Online (Sandbox Code Playgroud)
如何创建Base类?
Ask*_* B. 12
该方法InitializeCulture()仅存在于Page类,而不是MasterPage类,这就是您得到该错误的原因.
要解决此问题,您可以创建BasePage所有特定页面继承的内容:
BasePage或任何你想要的.BasePage.这是一个例子:
public class BasePage : System.Web.UI.Page
{
protected override void InitializeCulture()
{
//Do the logic you want for all pages that inherit the BasePage.
}
}
Run Code Online (Sandbox Code Playgroud)
具体页面看起来应该是这样的:
public partial class _Default : BasePage //Instead of it System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//Your logic.
}
//Your logic.
}
Run Code Online (Sandbox Code Playgroud)
有一个替代解决方案,不要求您创建一个BasePage.
"文化"的问题在于它在页面生命周期中非常早地设置,因此Page.InitializeCulture事件是页面上的早期事件之一(如果不是唯一的事件),我们可以在其中进行连接以更改Thread.CurrentThread.CurrentUICulture.但是,如果我们在服务器上开始请求时,甚至更早就这样做了.
我在每个请求上调用Application_BeginRequest的Global.asax文件上执行此操作.
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);
}
}
Run Code Online (Sandbox Code Playgroud)
在那里,我检查是否存在一个包含我想要使用的文化的cookie.如果没有cookie,则使用默认文化.
要更改我的应用程序上的语言,我只需要一个控件来更改客户端的cookie值,然后执行简单的回发到服务器.如果此类控件位于内容页面或母版页中并不重要,它甚至不需要服务器端的任何代码,因为所有处理都是在上面的方法上完成的,并且cookie设置为客户端甚至在页面发布之前.
我使用了一个简单的LinkButton(它被设计为墨西哥国旗),但你可以使用任何其他控件在点击/更改时进行回发.
<asp:LinkButton ID="btnSpanish" runat="server" OnClientClick="SetLanguageCookie('es')" CausesValidation="false" CssClass="mxFlag" />
Run Code Online (Sandbox Code Playgroud)
在此按钮回发到服务器之前,它运行客户端点击事件,更新我想要设置的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=/";
};
Run Code Online (Sandbox Code Playgroud)
而已!!将Thread.CurrentThread.CurrentUICulture得到改变,并没有BasePage需要,也没有类重写Page.InitializeCulture方法.由于存储在cookie上,因此在访问后会记住所选语言的副作用.
如果你想使用a DropDownList而不是a LinkButton,只需确保设置AutoPostBack="true"和,因为没有OnClientChanged属性DropDownList,你必须对该onchange属性进行硬编码DropDownList并将所选值传递给相同的javascript函数.
<asp:DropDownList ID="ddlLanguage" runat="server" AutoPostBack="true" onchange="SetLanguageCookie(this.options[this.selectedIndex].value)">
<asp:ListItem Text="English" Value="en" />
<asp:ListItem Text="Español" Value="es" />
<asp:ListItem Text="Français" Value="fr" />
</asp:DropDownList>
Run Code Online (Sandbox Code Playgroud)
该onchange属性不是属性的一部分DropDownList,但是,由于它DropDownList是控件的模拟控件,因此<select>在渲染发生时,属性只是"按原样"放置,并且在回发机制代码之前呈现.这是DropDownList上面呈现的HTML :
<select name="ctl00$cph1$ddlLanguage" onchange="SetLanguageCookie(this.options[this.selectedIndex].value);setTimeout('__doPostBack(\'ctl00$cph1$ddlLanguage\',\'\')', 0)" id="cph1_ddlLanguage">
<option value="en">English</option>
<option value="es">Español</option>
<option value="fr">Français</option>
</select>
Run Code Online (Sandbox Code Playgroud)
希望有人发现这种方法和我一样有用.:)
| 归档时间: |
|
| 查看次数: |
10954 次 |
| 最近记录: |