Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. Page updates only on second click

Page updates only on second click

Scheduled Pinned Locked Moved ASP.NET
tutorialquestion
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    laphijia
    wrote on last edited by
    #1

    I am trying to implement localization in my webpage as described in the Microsoft walkthrough. The Microsoft example reads the CurrentCulture from the client browser preferences but I would like to implement the classic style when on a webpage there are different links to start rendering the pages in a different language. I inserted two buttons one called English, the other Italian. When the user clicks one of the two buttons it sets Session["l"] to either "en" or "it". On Page_Load I have the following code: if (Session["l"] == "en") { Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("en-US"); Thread.CurrentThread.CurrentUICulture = new CultureInfo("en-US"); } else if (Session["l"] == "it") { Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("it"); Thread.CurrentThread.CurrentUICulture = new CultureInfo("it"); } This works fine with only one caveat: the language changes only when I click the button twice. More importantly if the page is rendered in English and I press the Italian button the pages reloads in English. If now I press the *English* button, NOW the pages renders in Italian. It seems that there is a delay. That is when I press the button to change Session["l"] to the appropriate settings this is not read immediately by the Page_Load when rendering the page after the PostBack, but only the next round. Why is it so? I can't figure it out and more importantly how to solve this. Thanks.

    C M 2 Replies Last reply
    0
    • L laphijia

      I am trying to implement localization in my webpage as described in the Microsoft walkthrough. The Microsoft example reads the CurrentCulture from the client browser preferences but I would like to implement the classic style when on a webpage there are different links to start rendering the pages in a different language. I inserted two buttons one called English, the other Italian. When the user clicks one of the two buttons it sets Session["l"] to either "en" or "it". On Page_Load I have the following code: if (Session["l"] == "en") { Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("en-US"); Thread.CurrentThread.CurrentUICulture = new CultureInfo("en-US"); } else if (Session["l"] == "it") { Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("it"); Thread.CurrentThread.CurrentUICulture = new CultureInfo("it"); } This works fine with only one caveat: the language changes only when I click the button twice. More importantly if the page is rendered in English and I press the Italian button the pages reloads in English. If now I press the *English* button, NOW the pages renders in Italian. It seems that there is a delay. That is when I press the button to change Session["l"] to the appropriate settings this is not read immediately by the Page_Load when rendering the page after the PostBack, but only the next round. Why is it so? I can't figure it out and more importantly how to solve this. Thanks.

      C Offline
      C Offline
      Colin Angus Mackay
      wrote on last edited by
      #2

      I think this is because the culture is being set up for the page before it gets to the Page_Load() method. At this point I have to admit to not being fully certain as I haven't worked with multiple cultures in ASP.NET. One thought that springs to mind however is first detecting the current culture, if this is different to the session variable then change the culture and do a Page.Redirect() back to the same page again. This should have the same effect of the double click, but only sending one response back to the client. This should also elimiate the apparent delay of selecting one language then the other and getting the first choice on the second click. I don't know if this work around is the best approach/will work, but it could be quite easy to implement. --Colin Mackay--

      "In the confrontation between the stream and the rock, the stream always wins - not through strength but perseverance." (H. Jackson Brown) Enumerators in .NET: See how to customise foreach loops with C#

      1 Reply Last reply
      0
      • L laphijia

        I am trying to implement localization in my webpage as described in the Microsoft walkthrough. The Microsoft example reads the CurrentCulture from the client browser preferences but I would like to implement the classic style when on a webpage there are different links to start rendering the pages in a different language. I inserted two buttons one called English, the other Italian. When the user clicks one of the two buttons it sets Session["l"] to either "en" or "it". On Page_Load I have the following code: if (Session["l"] == "en") { Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("en-US"); Thread.CurrentThread.CurrentUICulture = new CultureInfo("en-US"); } else if (Session["l"] == "it") { Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("it"); Thread.CurrentThread.CurrentUICulture = new CultureInfo("it"); } This works fine with only one caveat: the language changes only when I click the button twice. More importantly if the page is rendered in English and I press the Italian button the pages reloads in English. If now I press the *English* button, NOW the pages renders in Italian. It seems that there is a delay. That is when I press the button to change Session["l"] to the appropriate settings this is not read immediately by the Page_Load when rendering the page after the PostBack, but only the next round. Why is it so? I can't figure it out and more importantly how to solve this. Thanks.

        M Offline
        M Offline
        Michael Mac
        wrote on last edited by
        #3

        laphijia wrote: Why is it so? I can't figure it out and more importantly how to solve this. It's because you change the culture info after the controls have been created. Instead, override the CreateChildControls of the page class and do it there:

        protected override void CreateChildControls()
        {
        // The code goes here
        Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("en-US");
        Thread.CurrentThread.CurrentUICulture = new CultureInfo("en-US");

        // Don't forget to call base.CreateChildControls()
        base.CreateChildControls ();
        

        }


        43 68 65 65 72 73 2c 4d 69 63 68 61 65 6c

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups