2010-07-14 49 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 2010-07-14 04:04:05

+0

不,它會回到配置文件首選項中屬性Culture的默認值 – ONYX 2010-07-14 10:03:22

回答

2

的幾個問題,但最主要的是,你在Page_Load中做到:

ddlCountries.SelectedValue = (HttpContext.Current.Profile as ProfileCommon 
在事件處理click事件

然後做:

profile.Preferences.Culture = ddlCountries.SelectedValue; 

不幸的是,Page_Load事件在點擊事件前觸發,並且由於Page_Load事件將下拉列表的selectedvalue設置爲存儲在Profile對象中的值已存在,然後保存下拉列表的選定值(其中不再是您在點擊按鈕之前選擇的內容),您基本上只是忽略所選值並繼續使用默認值(或前值)。

此舉選擇在下拉列表來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; 
    } 
}