简体   繁体   English

initializeCulture of pages在masterPage中的下拉列表的selectedIndexChange之前触发

[英]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. 它有多个使用母版页的子页面,但是在子页面中(我创建了一个basePage类,然后让页面继承)我覆盖了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: 在下拉列表的SelectedIndexChanged事件中,我在会话中设置新语言,如下所示:

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

but the initializeCulture has then already been fired. 但是initializeCulture已经被解雇了。

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. 我不能再次调用initializeCulture,因为我在一个母版页上,我无法访问子页面的basePage类。

anyone got an idea how to tackle this? 有人知道如何解决这个问题吗?

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

You can't use the event handler for the dropdownlist because that happens after InitializeCulture() . 您不能将事件处理程序用于下拉列表,因为这发生在InitializeCulture() InitializeCulture() happens before the request values are loaded into the form controls. InitializeCulture()在将请求值加载到表单控件之前发生。

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. 因此,从下拉列表中获取值的正确方法是不使用事件处理程序,并使用InitializeCulture() Request.Form["yourddlid"]来获取所选值。

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

在这种情况下,我的解决方案是在更改语言后将页面重定向到自身。

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. 与“重定向到自身”的答案相同,您可以使用Server.Transfer()而不是Redirect,避免往返客户端。 Something like this (consider it's in the Default.aspx page): 像这样的东西(考虑它在Default.aspx页面中):

    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. 我已将文化的LCID存储在组合框值中,但这并不重要。 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. 该技术的核心是用户Server.Transer(pagename),以便重新启动页面工作流,并且Page.InitializeCulture有机会从Session获取“当前”值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 masterpage initializeculture没有找到合适的方法来覆盖错误? - masterpage initializeculture no suitable method found to override error? 所有下拉列表均在一次selectedIndexchange时重置 - All dropdownlist resets at single selectedIndexchange System.Web.UI.Page覆盖InitializeCulture并访问MasterPage - System.Web.UI.Page override InitializeCulture and access MasterPage 回发前如何触发SelectedIndexChange? - How to trigger SelectedIndexChange before postback? 如何在DropDownList的selectedIndexChange操作上显示CheckBoxList? - How to display a CheckBoxList on selectedIndexChange action of a DropDownList? 如何防止在下拉列表的selectedindexchange上进行全页回发 - How to prevent full page postback on selectedindexchange for dropdownlist 如何在OnSelectedIndexChanged触发事件之前获取DropDownList上的上一项 - How to get the previous item on DropDownList before OnSelectedIndexChanged fires the event 如何更改另一个下拉列表的SelectedIndexChange上的下拉列表? - How I can change a dropdownlist on SelectedIndexChange of another dropdownlist? 在SelectedIndexChange事件触发后,列表框弹出回到第一项 - List box pops back to the first item after SelectedIndexChange event fires 对gridview中的所有行触发Dropdown_SelectedIndexchange事件 - Dropdown_SelectedIndexchange event fires for all rows in gridview
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM