DropDownList从西班牙语切换到英语?

时间:2010-07-13 19:37:21

标签: c# text drop-down-menu autopostback

为什么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(服务器端和FireBug)之前的下拉列表时,每个ListItem都已正确设置,如

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

之后的PostBack,看起来像是

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

为什么会发生这种情况,我该怎么做才能让西班牙人在任何PostBacks之前看到它?

备注

  1. OnSelectedIndexChanged中指出的例程目前已被注释掉,因此问题不存在。
  2. 我在DropDownList中添加了EnableViewState="true",但这没有任何区别,所以我删除了它。
  3. 根据Ichiban的建议,我将Thread.CurrentThread.CurrentUICulturePage_Load设置为Page_Init(),但这也没有任何区别。

2 个答案:

答案 0 :(得分:0)

尝试添加将CultureInfo设置为Page_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");
   }
}

答案 1 :(得分: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后,下拉列表将保留所选语言!

相关问题