购物车中使用的 ASP.NET 的文化问题
我正在开发一个购物车,部分功能是选择您的货币。
我有一个下拉列表,当选定的索引更改时,我编写了一些代码来查找区域性代码(例如 en-GB 或 en-US),然后更改当前会话的区域性。此外,使用汇率会改变价格...
我目前将 en-GB 作为默认区域性。当有人从下拉列表中选择 en-US 文化时,一切正常。货币变化(所有货币标签均用ToString("C")设置)并且汇率变化。
当我再次使用下拉列表选择 en-GB 时,汇率发生变化(因此我知道代码正在运行),调试后我可以看到区域性会话已从 en-US 更改为 en-GB,但是货币仍显示为 $ 而不是 £。
我真的不明白为什么会发生这种情况。代码非常简单,我重写每个页面的 page_Load 事件以根据文化显示正确的货币:
protected override void OnLoad(EventArgs e)
{
if (Session["Culture"] != null)
{
string culture = Session["Culture"].ToString();
Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(Session["Culture"].ToString());
}
base.OnLoad(e);
}
当我将会话文化从 en-US 更改为 en- 时,为什么它不将货币更改回 £国标?
I'm developing a shopping cart, and part of the functionality is to select your currency.
I have a drop down list, and when the selected index changes I've written some code to find the culture code (such as en-GB or en-US) and then change the culture of the current session. In addition, using the exchange rates given it changes the price...
I currently have en-GB as the default culture. When someone selects the en-US culture from the drop down everything works fine. The currency changes (all currency labels are set with ToString("C")) and the exchange rate changes.
When I use the drop down list to select en-GB again, the exchange rate changes (so I know the code is working), and after debugging I can see that the culture session has changed from en-US to en-GB, but the currency still displays as $ and not £.
I really can't understand why this is happening. The code is very simple, I'm overriding the page_Load event for each page to display the correct currency based on culture:
protected override void OnLoad(EventArgs e)
{
if (Session["Culture"] != null)
{
string culture = Session["Culture"].ToString();
Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(Session["Culture"].ToString());
}
base.OnLoad(e);
}
Why does it not change the currency back to £ when I'm changing the session culture from en-US to en-GB?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
因为您正在检查 if (Session["Culture"] != null),这限制您第二次设置文化,因为 Session["Culture"] 不会为 null。
Because you are checking if (Session["Culture"] != null), which restricts you to set the culture second time as Session["Culture"] will not be null.
我明白了,因为它来自一个数据库条目,由于某种原因,该条目是 char(10) 数据类型,我忘记修剪文本。这导致框架无法找到该文化,并默认返回 en-US,因为它找不到“en-GB”。
I got it, because it was coming from a database entry that for some reason was a char(10) datatype, I forgot to trim the text. This resulted in the culture not being found by the Framework and defaulting back to en-US as it couldn't find 'en-GB '.