购物车中使用的 ASP.NET 的文化问题

发布于 2024-08-20 09:45:36 字数 740 浏览 1 评论 0原文

我正在开发一个购物车,部分功能是选择您的货币。

我有一个下拉列表,当选定的索引更改时,我编写了一些代码来查找区域性代码(例如 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 技术交流群。

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

发布评论

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

评论(2

南街女流氓 2024-08-27 09:45:36

因为您正在检查 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.

掐死时间 2024-08-27 09:45:36

我明白了,因为它来自一个数据库条目,由于某种原因,该条目是 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 '.

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