views:

70

answers:

2

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.
A: 

Try adding the code that sets the CultureInfo to the Page_Init event instead of 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");
   }
}
ichiban
Thank you! Unfortunately, Visual Studio complains about the "override" part ("There is no suitable method for override"), so I removed it. Also, OnInit() didn't fire, so I changed that to Page_Init() which *did* fire. However the problem still remains, the DropDownList changes from Spanish to English when choosing a new item. Any ideas?
Jelks
A: 

It turns out you need to set the CurrentUICulture in an overridden InitializeCuture():

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

    base.InitializeCulture();
}

Once I put that in, the dropdown stays in the chosen language after AutoPostBacks!

Jelks