initializeCulture of pages fires before the selectedIndexChange of dropdownlist in masterPage

99封情书 提交于 2019-12-02 11:42:17

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

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 == "")
        {
        ...
Rubens Ramos

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.

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.

protected override void InitializeCulture(){
   Page.UICulture = Request.Form["ddlLanguage"];
} 
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!