Search code examples
c#textdrop-down-menuautopostback

DropDownList switches from Spanish into English?


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.

Solution

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