locked
Image buttons and localization RRS feed

  • Question

  • User-1904680267 posted
    I'm having problems creating an image button, that when pressed will change the forms culture. This appears to work ok for a listbox and ordinary button. And it also works ok if I don't specify an image for the image button.

    This is the code from the whenbutton click:-

    Session["CurrentLanguage"] = "es";

    Debug.WriteLine("*** Change Language to " + DropDownList1.SelectedValue + " @ reload");

    // force postback again (redirect to self) to ensure new language picked up

    Response.Redirect(Request.Url.ToString());



    This is the code from global.asax

    void Application_PreRequestHandlerExecute(Object sender, EventArgs e)

    {

    // Set the thread locale for the UI and system.

    // Session state is available here.

    if (Session["CurrentLanguage"] == null)

    {

    Thread.CurrentThread.CurrentUICulture = new CultureInfo("en");

    Debug.WriteLine("Thread start with default culture " + Thread.CurrentThread.CurrentUICulture.ToString());

    }

    else

    {

    Thread.CurrentThread.CurrentUICulture = new CultureInfo(Session["CurrentLanguage"].ToString());

    Debug.WriteLine("Thread start with SAVED culture " + Thread.CurrentThread.CurrentUICulture.ToString());

    }

    }

    I get an error on loading the page, when it tries to check session state:-

    session state is not available in this context.

    I can't understand why when it works ok when using ordinary button.

    Friday, September 30, 2005 4:00 AM

All replies

  • User1183903743 posted

    Thread.CurrentThread.CurrentUICulture = new CultureInfo("en");


    This would throw an error because CurrentUICulture can only take a specific culture. Somehow this error is getting swallowed in asp.net session related errors. If you used a try-catch and dig for the inner exception in the catch block or parse the stacktrace you might get it.

    Why don't you use the Button_Click event handler instead of re-directing to itself which causes extra work on the server but the bigger sin is that this is not a postback when it loads so viewstate is new and all code that !IsPostBack runs?
    Even worse Response.Redirect posts back and makes the runtime parse the viewstate and other steps of the cycle and then rudely says: go to this new page that is yourself but start from scratch. Ts,ts,ts!

    Also you do not need to waste the Session to keep track of the culture. That is what the thread is for. However you do need to set both when you are changing it, which is what you want your button to do.

    void Button_Click(sender o, EventArgs e) 
    {

       CulturInfo ci=new CultureInfo(InputString)
       Thread.CurrentThread.CurrentCulture = ci;

       //prevent an exception
       if(!ci.IsNeutralCulture)
          Thread.CurrentThread.CurrentUICulture = ci;
    }

    in v2.0 this is actually a page directive - much nicer.
    Wednesday, October 12, 2005 1:21 PM