DropDownList 从西班牙语切换为英语?
为什么当人们在 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"
为什么会发生这种情况,我能做什么如何让它在任何回发之前保留西班牙语内容?
注释:
OnSelectedIndexChanged
中指向的例程当前已被注释掉,因此问题不存在。- 我将
EnableViewState="true"
添加到 DropDownList,但这没有任何区别,因此我将其删除。 - 正如 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:
- The routine pointed to in
OnSelectedIndexChanged
is currently commented out, so the problem is not there. - I added
EnableViewState="true"
to the DropDownList, but that didn't make any difference, so I removed it. - As suggested below by Ichiban, I moved setting the
Thread.CurrentThread.CurrentUICulture
fromPage_Load
toPage_Init()
, but that too didn't make any difference.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试将设置
CultureInfo
的代码添加到Page_Init
事件,而不是Page_Load
Try adding the code that sets the
CultureInfo
to thePage_Init
event instead ofPage_Load
事实证明,您需要在重写的
InitializeCuture()
中设置CurrentUICulture
:一旦我将其放入,下拉菜单在 AutoPostBacks 后将保持所选语言!
It turns out you need to set the
CurrentUICulture
in an overriddenInitializeCuture()
:Once I put that in, the dropdown stays in the chosen language after AutoPostBacks!