开发者

language changing on second click

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();

}

No开发者_开发知识库w, 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


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);


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.


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


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.


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);  


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜