2010-07-13 83 views
1

爲什麼當一個DropDownList選擇一個新的項目時,它會從西班牙語切換到英語?如何防止這種情況發生?DropDownList從西班牙語切換到英語?

<asp:DropDownList ID="ddl_r1pc" runat="server" AutoPostBack="True" 
     OnSelectedIndexChanged="ddlRelationship_SelectedIndexChanged"> 
     <asp:ListItem></asp:ListItem> 
     <asp:ListItem Value="Spouse" Text="<%$Resources:messages, RelSpouse %>"></asp:ListItem> 
     <asp:ListItem Value="Parent(s)" Text="<%$Resources:messages, RelParents %>"></asp:ListItem> 
     <asp:ListItem Value="Other" Text="<%$Resources:messages, Other %>"></asp:ListItem> 
    </asp:DropDownList> 

然後在Page_Load(),這始終運行(即,無論是作爲IsPostBack!IsPostBack):

try { 
     culture = (string) Session["culture"]; 
     Thread.CurrentThread.CurrentUICulture = new CultureInfo(culture); 
    } 
    catch { 
     Server.Transfer("~/sessiontimeout.aspx"); 
    } 

當你第一次來到這個網頁,既然選擇西班牙語作爲語言後,下拉填充ListItems文本顯示 - 按預期 - 以西班牙文顯示。但是當你從下拉菜單中選擇另一個項目時,所有的項目都會以英文返回!

當您檢查的AutoPostBack前的下拉菜單(包括服務器端和螢火蟲),各列表項設置正確,如

Value="Some English" Text="Some Español" 

後回傳,它看起來像

Value="Some English" Text="The same English" 

爲什麼會發生這種情況,我該怎麼做才能讓西班牙人在任何回傳之前看到?

注意

  1. 例行指出,在OnSelectedIndexChanged目前註釋掉,所以這個問題是不存在的。
  2. 我將EnableViewState="true"添加到DropDownList,但沒有任何區別,所以我刪除了它。
  3. 正如Ichiban所建議的,我將Thread.CurrentThread.CurrentUICulturePage_Load設置爲Page_Init(),但是這也沒有什麼區別。

回答

0

代碼原來你需要設置CurrentUICulture在覆蓋InitializeCuture()

protected override void InitializeCulture() { 
    try { 
     culture = (string) Session["culture"]; 
     Thread.CurrentThread.CurrentUICulture = new CultureInfo(culture); 
    } 
    catch { 
     Server.Transfer("~/sessiontimeout.aspx"); 
    } 

    base.InitializeCulture(); 
} 

一旦我把它放進來,下拉菜單在AutoPostBacks後保持選定的語言!

0

嘗試添加設置CultureInfoPage_Init事件,而不是Page_Load

protected override void OnInit(object source, EventArgs e) { 
    try { 
     culture = (string) Session["culture"]; 
     Thread.CurrentThread.CurrentUICulture = new CultureInfo(culture); 
    } 
    catch { 
     Server.Transfer("~/sessiontimeout.aspx"); 
    } 
} 
+0

謝謝!不幸的是,Visual Studio抱怨「覆蓋」部分(「沒有合適的覆蓋方法」),所以我刪除了它。另外,OnInit()沒有觸發,所以我將它改成了Page_Init(),它已經觸發了。但是問題仍然存在,當選擇一個新項目時,DropDownList從西班牙語變爲英語。有任何想法嗎? – Jelks 2010-07-14 13:54:55

相關問題