initializeCulture of pages fires before the selectedIndexChange of dropdownlist in masterPage

I have a masterpage with a language selector dropdownlist

it has multiple subpages using the masterpage but, in the subpages (i created a basePage class which i then let the pages inherit from) i override the initializeCulture.
like this:

protected override void InitializeCulture()
        {
            String selectedLanguage = Common.SessionManager.Language;

            if (selectedLanguage == "")
            {
                selectedLanguage = ConfigurationManager.AppSettings.Get("defaultLanguage");
            }

            if (selectedLanguage == "")
            {
                selectedLanguage = "nl-BE";
            }

            UICulture = selectedLanguage;
            Culture = selectedLanguage;
            Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(selectedLanguage);
            Thread.CurrentThread.CurrentUICulture = new CultureInfo(selectedLanguage);

            base.InitializeCulture();
        }

on the SelectedIndexChanged event of the dropdownlist, i set the new language in the session
like this:

    protected void LanguageSelectorSelectedIndexChanged(object sender, EventArgs e)
    {
        string sCulture = LanguageSelector.SelectedValue;
        Common.SessionManager.Language = sCulture;
    }

but the initializeCulture has then already been fired.

so i have sort of a delay effect, page loads with previous language, and on the next postback its translated correctly.

i cannot call the initializeCulture again, because i’m on a masterpage and i have no access to the subpage’s basePage class.

anyone got an idea how to tackle this?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

You could try to get the selected language by form posted values:

    protected override void InitializeCulture()
    {
        String selectedLanguage = Common.SessionManager.Language;

        if (Request.Form.ContainsKey(myLanguageDropDown.ClientID)
            selectedLanguage = Request.Form[myLanguageDropDown.ClientID];

        if (selectedLanguage == "")
        {
        ...

Method 2

You can’t use the event handler for the dropdownlist because that happens after InitializeCulture().
InitializeCulture() happens before the request values are loaded into the form controls.

So the correct way to obtain the value from the dropdownlist is to NOT use the event handler, and use Request.Form["yourddlid"] inside InitializeCulture() to get the selected value.

Method 3

My solution in this case is to redirect the page to itself after changing the language.

Method 4

In the same vein of the “Redirect to itself” answer you could use Server.Transfer() instead of Redirect, avoiding a round-trip to the client. Something like this (consider it’s in the Default.aspx page):

    protected override void InitializeCulture()
    {
        if (Session["LCID"] != null)
        {
            int lcid = (int)Session["LCID"];
            CultureInfo c = new CultureInfo(lcid);
            Thread.CurrentThread.CurrentCulture = c;
        }
        base.InitializeCulture();
    }

    protected void comboCultures_SelectedIndexChanged(object sender, EventArgs e)
    {
        CultureInfo c = new CultureInfo(Thread.CurrentThread.CurrentCulture.LCID);
        if (comboCultures.SelectedItem != null)
            c = CultureInfo.GetCultureInfo(Convert.ToInt32(comboCultures.SelectedItem.Value));
        Session["LCID"] = c.LCID;
        Server.Transfer("Default.aspx");
    }

I’ve stored the LCID of the culture in the combo box values, but this is not important. The heart of the technique is to user Server.Transer(pagename) so that the page workflow is reinitiated and the Page.InitializeCulture has a chance to get the “current” values from the Session.

Method 5

protected override void InitializeCulture(){
   Page.UICulture = Request.Form["ddlLanguage"];
}


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x