开发者

Page won't change language

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(objRegio开发者_运维问答nInfo.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>


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜