ASP.NET 日历控件中的各种日历

发布于 2024-08-15 13:07:38 字数 188 浏览 3 评论 0原文

我需要创建一个显示日历控件的 ASP.NET 页面,该控件根据下拉列表中选择的值显示特定的日历。 目前我需要显示希伯来日历和常规(公历)日历,但将来我可能需要其他日历。 当然,我无法使用 Windows 的区域设置或 web.config 中的全球化定义,因为所需的日历是在运行时设置的。 如何在 Calendar 控件中显示各种日历?

谢谢!

I need to create an ASP.NET page that displays a Calendar control which shows a specific calendar based on value selected in a drop down.
Currently I need to display HebrewCalendar and the regualr (gregorian) calendar, but in the future I'll probably need others.
Of course I can't use the Regional Settings of Windows or the globalization definition in web.config since the required calendar is set in runtime.
How can I display various calendars in a Calendar control?

Thanks!

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

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

发布评论

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

评论(2

不喜欢何必死缠烂打 2024-08-22 13:07:38

诀窍是您需要设置当前线程的文化,并且(对于某些语言环境,例如希伯来语)还需要在该文化中设置日历。

下面的独立代码示例说明了如何执行此操作。这种方法当然可能会影响其他控件的本地化文本。如果这是一个问题 — 并且您只想本地化 Calendar 控件并将其余部分保留为英语 — 那么您可以执行以下操作:

  • 从 ASP.NET 的 Calendar 控件派生一个类并重写 Render() 方法。
  • 在 Render() 实现中,保存当前线程的区域性/UICulture,然后使用下面的代码重置当前线程的区域性和日历,然后调用基类的 Render(),然后恢复当前线程使用的区域性/
  • UICulture该类,而不是您的 ASPX 页面中的常规 ASP.NET 日历。

这是代码:

<%@ Page Language="C#" AutoEventWireup="true" %>
<%@ Import Namespace="System.Globalization"%>
<%@ Import Namespace="System.Threading"%>
<%@ Import Namespace="System.Collections.Generic"%>
<html>
<body>
    <form id="form1" runat="server">
    Choose a language and calendar: <asp:DropDownList ID="LocaleChoice" runat="server" AutoPostBack="true">
        <asp:ListItem Value="en-US" Selected="True">English</asp:ListItem>
        <asp:ListItem Value="es-MX">Español</asp:ListItem>
        <asp:ListItem Value="de-DE">Deutsch</asp:ListItem>
        <asp:ListItem Value="he-IL|HebrewCalendar">Hebrew (Hebrew Calendar)</asp:ListItem>
        <asp:ListItem Value="he-IL|GregorianCalendar">Hebrew (Gregorian Calendar)</asp:ListItem>
        <asp:ListItem Value="ar-SA|HijriCalendar">Arabic (Hijri Calendar)</asp:ListItem>
        <asp:ListItem Value="ar-SA|GregorianCalendar">Arabic (Gregorian Calendar)</asp:ListItem>
    </asp:DropDownList><br /><br />
    <asp:Calendar ID="Calendar1" runat="server"></asp:Calendar>
    </form>
</body>
</html>
<script runat="server">

    Dictionary<string, System.Globalization.Calendar> Calendars =
        new Dictionary<string, System.Globalization.Calendar>()
        {
            {"GregorianCalendar", new GregorianCalendar()},
            {"HebrewCalendar", new HebrewCalendar()},
            {"HijriCalendar", new HijriCalendar()},
            {"JapaneseCalendar", new JapaneseCalendar()},
            {"JulianCalendar", new JulianCalendar()},
            {"KoreanCalendar", new KoreanCalendar()},
            {"TaiwanCalendar", new TaiwanCalendar()},
            {"ThaiBuddhistCalendar", new ThaiBuddhistCalendar ()}
        };

    protected override void InitializeCulture()
    {
        if (Request.Form["LocaleChoice"] != null)
        {
            string selected = Request.Form["LocaleChoice"];
            string[] calendarSetting = selected.Split('|');
            string selectedLanguage = calendarSetting[0];

            CultureInfo culture = CultureInfo.CreateSpecificCulture(selectedLanguage);

            if (calendarSetting.Length > 1)
            {
                string selectedCalendar = calendarSetting[1];
                var cal = culture.Calendar;
                if (Calendars.TryGetValue(selectedCalendar, out cal))
                    culture.DateTimeFormat.Calendar = cal;
            }

            Thread.CurrentThread.CurrentCulture = culture;
            Thread.CurrentThread.CurrentUICulture = culture;
        }
        base.InitializeCulture();
    }

    protected void Page_Load(object sender, EventArgs e)
    {
    }
</script>

The trick is you'll need to set the Culture of the current thread, and (for some locales like Hebrew) also need to set the Calendar within that Culture.

The self-contained code sample below illustrates how to do this. This approach of course may affect other controls's localized text. If this is a problem-- and you only want to localize the Calendar control and leave the rest in English-- then you can do the following:

  • derive a class from ASP.NET's Calendar control and override the Render() method.
  • in your Render() implementation, save the current thread's culture/UICulture, then reset the culture and calendar of the current thread using the code below, then call the base class's Render(), then restore the culture/UICulture of the curren thread
  • use that class, instead of the regular ASP.NET calendar, in your ASPX page.

Here's the code:

<%@ Page Language="C#" AutoEventWireup="true" %>
<%@ Import Namespace="System.Globalization"%>
<%@ Import Namespace="System.Threading"%>
<%@ Import Namespace="System.Collections.Generic"%>
<html>
<body>
    <form id="form1" runat="server">
    Choose a language and calendar: <asp:DropDownList ID="LocaleChoice" runat="server" AutoPostBack="true">
        <asp:ListItem Value="en-US" Selected="True">English</asp:ListItem>
        <asp:ListItem Value="es-MX">Español</asp:ListItem>
        <asp:ListItem Value="de-DE">Deutsch</asp:ListItem>
        <asp:ListItem Value="he-IL|HebrewCalendar">Hebrew (Hebrew Calendar)</asp:ListItem>
        <asp:ListItem Value="he-IL|GregorianCalendar">Hebrew (Gregorian Calendar)</asp:ListItem>
        <asp:ListItem Value="ar-SA|HijriCalendar">Arabic (Hijri Calendar)</asp:ListItem>
        <asp:ListItem Value="ar-SA|GregorianCalendar">Arabic (Gregorian Calendar)</asp:ListItem>
    </asp:DropDownList><br /><br />
    <asp:Calendar ID="Calendar1" runat="server"></asp:Calendar>
    </form>
</body>
</html>
<script runat="server">

    Dictionary<string, System.Globalization.Calendar> Calendars =
        new Dictionary<string, System.Globalization.Calendar>()
        {
            {"GregorianCalendar", new GregorianCalendar()},
            {"HebrewCalendar", new HebrewCalendar()},
            {"HijriCalendar", new HijriCalendar()},
            {"JapaneseCalendar", new JapaneseCalendar()},
            {"JulianCalendar", new JulianCalendar()},
            {"KoreanCalendar", new KoreanCalendar()},
            {"TaiwanCalendar", new TaiwanCalendar()},
            {"ThaiBuddhistCalendar", new ThaiBuddhistCalendar ()}
        };

    protected override void InitializeCulture()
    {
        if (Request.Form["LocaleChoice"] != null)
        {
            string selected = Request.Form["LocaleChoice"];
            string[] calendarSetting = selected.Split('|');
            string selectedLanguage = calendarSetting[0];

            CultureInfo culture = CultureInfo.CreateSpecificCulture(selectedLanguage);

            if (calendarSetting.Length > 1)
            {
                string selectedCalendar = calendarSetting[1];
                var cal = culture.Calendar;
                if (Calendars.TryGetValue(selectedCalendar, out cal))
                    culture.DateTimeFormat.Calendar = cal;
            }

            Thread.CurrentThread.CurrentCulture = culture;
            Thread.CurrentThread.CurrentUICulture = culture;
        }
        base.InitializeCulture();
    }

    protected void Page_Load(object sender, EventArgs e)
    {
    }
</script>
寂寞笑我太脆弱 2024-08-22 13:07:38

如果您需要按日历进行文化定制,请查看 BaseCalendar (其中包含一个处理此场景的演示) 。更改当前线程的文化可能会在其他地方产生副作用。 BaseCalendar 允许您指定日历的区域性,而无需更改当前线程的区域性。

If you need per-calendar culture customization have a look at BaseCalendar (there's a demo included that handles this scenario). Changing the current thread's culture could have side-effects in other places. BaseCalendar allows you to specify a culture for a calendar without changing the current thread's culture.

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