2010-07-14 3 views
0

Моя страница не изменит язык кто-то может взглянуть на мой код и скажите мне, что я делаю неправильно, это всегда идет на языке по умолчаниюстраницы не изменится язык

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> 
+0

ли вниз показать обновленный язык, который был выбран падение? – spinon

+0

Нет возврата к значению по умолчанию в свойстве Culture в настройках профиля – ONYX

ответ

2

Несколько проблем здесь, но главным является то, что вы в Page_Load сделать:

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

Тогда в EventHandler для события щелчка сделать:

profile.Preferences.Culture = ddlCountries.SelectedValue; 

К сожалению для вас событие Page_Load вызывает до вашего события click, а так как событие Page_Load устанавливает для выбранного значения выпадающего списка значение уже, сохраненное в объекте Profile, и затем вы сохраняете выбранное значение выпадающего списка (который уже не то, что вы выбрали до нажатия на кнопку), вы по существу просто игнорируете выбранное значение и продолжаете использовать значение по умолчанию (или прежнее).

Переместить код, который выбирает значение в DropDownList для Page_PreRender (удалить его из Page_Load), и вам будет хорошо (пример):

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; 
    } 
} 
Смежные вопросы