页面不会更改语言 [英] Page won't change language

查看:73
本文介绍了页面不会更改语言的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果有人看了我的代码并告诉我即时消息做错了什么,我的页面就不会更改语言,它总是使用默认语言

My page won't change the language could someone have a look at my code and tell me what im doing wrong it always goes to the default language

public partial class ChangeLanguage : BasePage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        SortedDictionary<string, string> objDic = new SortedDictionary<string, string>();

        foreach (CultureInfo ObjectCultureInfo in CultureInfo.GetCultures(CultureTypes.SpecificCultures))
        {
            RegionInfo objRegionInfo = new RegionInfo(ObjectCultureInfo.Name);
            if (!objDic.ContainsKey(objRegionInfo.EnglishName))
            {
                objDic.Add(objRegionInfo.EnglishName, ObjectCultureInfo.Name);
            }
        }

        foreach (KeyValuePair<string, string> val in objDic)
        {
            ddlCountries.Items.Add(new ListItem(val.Key, val.Value));
        }

        ddlCountries.SelectedValue = (HttpContext.Current.Profile as ProfileCommon).Preferences.Culture;
    }

    protected void btnChangeLanguage_Click(object sender, EventArgs e)
    {
        ProfileCommon profile = HttpContext.Current.Profile as ProfileCommon;
        profile.Preferences.Culture = ddlCountries.SelectedValue;
    }
}
protected override void InitializeCulture()
  {
     string culture = (HttpContext.Current.Profile as ProfileCommon).Preferences.Culture;
     this.Culture = culture;
     this.UICulture = culture;
  }
Profile:
  <properties>
    <add name="FirstName" type="String"/>
    <add name="LastName" type="String"/>
    <add name="Gender" type="String"/>
    <add name="BirthDate" type="DateTime"/>
    <add name="Occupation" type="String"/>
    <add name="WebSite" type="String"/>
    <group name="Preferences">
      <add name="Culture" type="String" defaultValue="en-NZ" allowAnonymous="true"/>
    </group>
  </properties>


推荐答案

这里有几个问题,但主要的问题是您在Page_Load中执行以下操作:

Several problems here, but the main one is that you in Page_Load do:

ddlCountries.SelectedValue = (HttpContext.Current.Profile as ProfileCommon

然后在事件处理程序中为click事件执行:

Then in the eventhandler for the click event do:

profile.Preferences.Culture = ddlCountries.SelectedValue;

不幸的是,触发了Page_Load事件在点击事件之前 ,并且由于Page_Load事件将下拉列表的selectedvalue设置为配置文件对象中存储的已经值,然后保存了下拉列表的selectedvalue( (不再是在单击按钮之前选择的内容)。实际上,您只是忽略所选值,而是继续使用默认值(或以前的值)。

Unfortunately for you, the Page_Load event fires before your click event, and since the Page_Load event sets the selectedvalue of the dropdownlist to the value already stored in the Profile object, and you then save the selectedvalue of the dropdownlist (which is no longer what you selected prior to clicking your button) you essentially just ignore the selected value and continue to use the default (or former) value.

移动代码在下拉列表中选择Page_PreRender的值(将其从Page_Load中删除),然后会很好(示例):

Move the code that selects the value in the dropdownlist to Page_PreRender (remove it from Page_Load), and you will be fine (example):

    protected void Page_PreRender(object sender, EventArgs e)
{
    string culture = (HttpContext.Current.Profile as ProfileCommon).Preferences.Culture;
    if (ddlCountries.Items.FindByValue(culture) != null)
    {
        ddlCountries.SelectedValue = (HttpContext.Current.Profile as ProfileCommon).Preferences.Culture;
    }
}

这篇关于页面不会更改语言的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆