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

发布于 2024-09-09 16:21:46 字数 1718 浏览 12 评论 0原文

为什么当人们在 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>

运行(即,同时作为 IsPostBack!IsPostBack):

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

然后在 Page_Load() 中,它总是 选择西班牙语作为您的语言后,下拉列表中将填充 ListItems 文本,按预期显示为西班牙语。但是,当您从下拉列表中选择另一个项目时,所有项目都会以英文显示!

当您检查 AutoPostBack 之前的下拉列表(服务器端和 FireBug 中)时,每个 ListItem 都已正确设置,就像

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

在 PostBack 之后一样,它看起来像

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

为什么会发生这种情况,我能做什么如何让它在任何回发之前保留西班牙语内容?

注释

  1. OnSelectedIndexChanged中指向的例程当前已被注释掉,因此问题不存在。
  2. 我将 EnableViewState="true" 添加到 DropDownList,但这没有任何区别,因此我将其删除。
  3. 正如 Ichiban 下面所建议的,我将 Thread.CurrentThread.CurrentUICulture 设置从 Page_Load 移至 Page_Init(),但这也没有使任何差异。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

浅浅 2024-09-16 16:21:46

尝试将设置 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");
   }
}

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");
   }
}
稳稳的幸福 2024-09-16 16:21:46

事实证明,您需要在重写的 InitializeCuture() 中设置 CurrentUICulture

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

    base.InitializeCulture();
}

一旦我将其放入,下拉菜单在 AutoPostBacks 后将保持所选语言!

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!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文