第二次点击时语言改变

发布于 2024-11-07 04:56:46 字数 964 浏览 0 评论 0原文

要更改语言,我单击 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

情绪失控 2024-11-14 04:56:46

InitializeCulture()加载页面时发生的第一件事

调用InitializeCulture方法
在页面生命周期的早期,
在创建控件之前或
为页面设置属性。
因此,要读取的值是
从控件传递到页面,您
必须直接从
使用 Form 集合请求。

当您尝试使用按钮更改区域性时,在初始化区域性之后,该代码段可以很好地运行到页面生命周期中。

一键更改文化的最简单方法是通过重定向到自身,在 SetCulture() 之后重新加载页面:

Response.Redirect(Request.RawUrl);

InitializeCulture() is one of the first things that happen when a page is loaded:

The InitializeCulture method is called
very early in the page life cycle,
before controls are created or
properties are set for the page.
Therefore, to read values that are
passed to the page from controls, you
must get them directly from the
request using the Form collection.

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:

Response.Redirect(Request.RawUrl);
小耗子 2024-11-14 04:56:46

我以前也遇到过这个问题。您可能正在使用母版页,并且某些方法在母版页中执行,其他方法在内部页中执行。

问题在于加载内容的顺序。这意味着:某些代码在您的 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.

动次打次papapa 2024-11-14 04:56:46

您是否尝试过从 SetCulture() 内部调用 InitializeCulture()

Have you tried calling InitializeCulture() from inside SetCulture()?

还给你自由 2024-11-14 04:56:46

按钮单击的事件处理程序可能会在页面初始化后触发,因此解决此问题的最简单方法(不一定是最优雅的)是更改会话状态中的区域性,然后立即发出重定向到同一页。即

SetCulture(Session, "en-GB");
Response.Redirect(Request.RawUrl);

这应该导致浏览器再次请求该页面。

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.

SetCulture(Session, "en-GB");
Response.Redirect(Request.RawUrl);

This should cause the browser to request the page over again.

别忘他 2024-11-14 04:56:46

更改按钮单击事件中的区域性后,使用以下代码行将用户重定向到同一页面。

Responce.Redirect(Request.UrlReferrer.AbsolutePath);  

After you change the culture in button click event, redirect the user to the same page using below line of code.

Responce.Redirect(Request.UrlReferrer.AbsolutePath);  
冷…雨湿花 2024-11-14 04:56:46

最简单的解决方案是在设置区域性后,在点击处理程序中使用 Response.Redirect(Request.RawUrl);。

Easiest solution is a Response.Redirect(Request.RawUrl); in the click handler, after the culture has been set.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文