Page won't change language

依然范特西╮ 提交于 2020-01-25 07:36:28

问题


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>

回答1:


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

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

Then in the eventhandler for the click event do:

profile.Preferences.Culture = ddlCountries.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.

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;
    }
}


来源:https://stackoverflow.com/questions/3243309/page-wont-change-language

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!