第二次点击时语言改变
要更改语言,我单击 imageButton,它会执行如下操作:
SetCulture(Session, "en-GB");
此函数的实现如下:
public static void SetCulture(HttpSessionState session, string locale)
{
Thread.CurrentThread.CurrentUICulture = new CultureInfo(locale);
Thread.CurrentThread.CurrentCulture = new CultureInfo(locale);
session["currentLocale"] = locale;
}
另外,我的 .aspx 页面的类型为 LocalizedPage,它覆盖了 InitializeCulture:
protected override void InitializeCulture()
{
if (Session["currentLocale"] != null)
{
//changes the cultures of the current Thread
CurrentUICulture = new CultureInfo((string)Session["currentLocale"]);
CurrentCulture = new CultureInfo((string)Session["currentLocale"]);
}
base.InitializeCulture();
}
现在,问题是我必须在 imageButton 上单击两次为了改变语言。我可以做什么来更改第一次点击时的语言?
请注意,我对 ASP.NET 相当陌生,所以这可能是一个简单的解决方案
To change the language I click on an imageButton which executes something like:
SetCulture(Session, "en-GB");
This function is implemented as follows:
public static void SetCulture(HttpSessionState session, string locale)
{
Thread.CurrentThread.CurrentUICulture = new CultureInfo(locale);
Thread.CurrentThread.CurrentCulture = new CultureInfo(locale);
session["currentLocale"] = locale;
}
Also, my .aspx pages are of type LocalizedPage which overrides InitializeCulture:
protected override void InitializeCulture()
{
if (Session["currentLocale"] != null)
{
//changes the cultures of the current Thread
CurrentUICulture = new CultureInfo((string)Session["currentLocale"]);
CurrentCulture = new CultureInfo((string)Session["currentLocale"]);
}
base.InitializeCulture();
}
Now, the problem is that I have to click twice on the imageButton in order to make the language change. What can I do to change the language on the first click?
Note that I am rather new to ASP.NET so it might be a simple solution
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
InitializeCulture()
是 加载页面时发生的第一件事:当您尝试使用按钮更改区域性时,在初始化区域性之后,该代码段可以很好地运行到页面生命周期中。
一键更改文化的最简单方法是通过重定向到自身,在
SetCulture()
之后重新加载页面:InitializeCulture()
is one of the first things that happen when a page is loaded:When you try to change the culture using a Button, that bit of code runs well into the page lifecycle, after the culture has been initialized.
The easiest way to get the culture to change in one click it to reload the page after
SetCulture()
via a redirect to itself:我以前也遇到过这个问题。您可能正在使用母版页,并且某些方法在母版页中执行,其他方法在内部页中执行。
问题在于加载内容的顺序。这意味着:某些代码在您的
SetCulture(Session, "en-GB");
之前运行。在这种情况下,请尝试先调试以找出正在运行的内容,然后修复它。
I've had this problem before. It's likely that you are using a master page, and some methods are executed in master page, others in the inner page.
The problem lies in the order that things are being loaded. That means: some code is running before your
SetCulture(Session, "en-GB");
.In that case, try to debug to find out what's running first and then fix it.
您是否尝试过从
SetCulture()
内部调用InitializeCulture()
?Have you tried calling
InitializeCulture()
from insideSetCulture()
?按钮单击的事件处理程序可能会在页面初始化后触发,因此解决此问题的最简单方法(不一定是最优雅的)是更改会话状态中的区域性,然后立即发出重定向到同一页。即
这应该导致浏览器再次请求该页面。
The event handler for your button click is likely to be firing after the page has been initialised, so the easiest way to solve this (not necessarily the most elegant) is to change the culture in the Session state, then immediately issue a redirect to the same page. I.e.
This should cause the browser to request the page over again.
更改按钮单击事件中的区域性后,使用以下代码行将用户重定向到同一页面。
After you change the culture in button click event, redirect the user to the same page using below line of code.
最简单的解决方案是在设置区域性后,在点击处理程序中使用 Response.Redirect(Request.RawUrl);。
Easiest solution is a
Response.Redirect(Request.RawUrl);
in the click handler, after the culture has been set.