1

Why would a DropDownList switch from Spanish into English when one selects a new item in it? And how does one prevent that from happening?

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

Then in Page_Load(), this always runs (i.e., both as IsPostBack and !IsPostBack):

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

When you first come to this page after having chosen Spanish as your language, the dropdown is populated with the ListItems texts displaying -- as expected -- in Spanish. But when you go to select another item from the dropdown, all the items come back in English!

When you examine the dropdown before the AutoPostBack (both server-side and in FireBug), each ListItem is properly set, as in

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

whereas after the PostBack, it looks like

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

Why is this happening, and what can I do to get it to keep the Spanish one sees before any PostBacks?

Notes:

  1. The routine pointed to in OnSelectedIndexChanged is currently commented out, so the problem is not there.
  2. I added EnableViewState="true" to the DropDownList, but that didn't make any difference, so I removed it.
  3. As suggested below by Ichiban, I moved setting the Thread.CurrentThread.CurrentUICulture from Page_Load to Page_Init(), but that too didn't make any difference.
4

2 回答 2

0

事实证明,您需要将CurrentUICulturein 设置为覆盖InitializeCuture()

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

    base.InitializeCulture();
}

一旦我把它放进去,下拉菜单会在 AutoPostBacks 之后保持选择的语言!

于 2010-07-15T19:02:33.463 回答
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");
   }
}
于 2010-07-13T19:49:19.973 回答