显示全年的 ASP.NET 可用性日历

发布于 2024-10-12 17:31:52 字数 347 浏览 1 评论 0原文

我正在寻找一个 ASP.NET 控件,它将以网格格式显示全年,每个月作为一行,该月中的每一天作为列。我发现以下经典 ASP 脚本可以生成正确的格式: http:// /www.livio.net/code/calendar_36/CalendarTestYearly36.asp?infomode=HELP(请参阅年历)

是否有类似的内容,或者有没有一种方法可以在没有特定控件的情况下简单地执行此操作?我是 ASP.NET 新手。

谢谢

I am looking for a asp.net control that will display a full year in a grid format, with each month as a row and each day in the month as the columns. I have found the following classic ASP script which produces the correct format: http://www.livio.net/code/calendar_36/CalendarTestYearly36.asp?infomode=HELP (see Yearly Calendar)

Is anything like this available or is there a way to do this simply without a specific control? I am new to asp.net.

Thanks

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

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

发布评论

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

评论(2

↙厌世 2024-10-19 17:31:52

为你准备了这个:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Yearly Calendar</title>
    <style type="text/css">
        table td { padding:0; margin:0; border:1px solid #dadada; }
        table th { width:25px; text-align:center; }
        table td { text-align:center; }
        table td span { color:#dadada; }
        table td a { color:#000000; text-decoration:none; }
        table td a:hover { text-decoration:underline; }
        table td a.hasEvents { color:#ff0000; }
        table td a.selected { color:#0000ff; font-weight:bold; }
        table td.month { background-color:#999999; }
        table td.weekend { background-color:#D0D0D0; }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <table cellpadding="0" cellspacing="0">
            <thead>
                <tr>
                    <th>
                        <asp:Literal ID="litSelectedYear" runat="server" />
                    </th>
                    <% for (int iWeek = 1; iWeek <= 6; iWeek++) {
                        for (int iDay = 1; iDay <= 7; iDay++) { if (iWeek == 6 && iDay > 2) break; %>
                        <th>
                            <%= GetDayName(iDay) %>
                        </th>
                    <% } } %>
                </tr>
            </thead>
            <asp:Repeater ID="repMonths" runat="server" OnInit="repMonths_OnInit" OnItemDataBound="repMonths_OnItemDataBound">
                <ItemTemplate>                  
                    <tr>
                        <td class="month">
                            <asp:HyperLink ID="hylMonth" runat="server" />
                        </td>
                        <asp:Repeater ID="repDays" runat="server" OnItemDataBound="repDays_OnItemDataBound">
                            <ItemTemplate>
                                <td id="tdDay" runat="server">
                                    <asp:Literal ID="litDay" runat="server" />
                                </td>
                            </ItemTemplate>
                        </asp:Repeater>
                    </tr>
                </ItemTemplate>
            </asp:Repeater>
        </table>
    </div>
    </form>
</body>
</html>

using System;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
    private DateTime _dtMonth;
    private DateTime _selectedDate;
    private bool _specialDaySelected = true;
    private int _currentBindingMonth;

    protected void repMonths_OnInit(object sender, EventArgs e)
    {
        if (!DateTime.TryParse(Request.QueryString["CalDate"], out _selectedDate))
        {
            _specialDaySelected = false;
            int selectedMonth, selectedYear;
            int.TryParse(Request.QueryString["CalYear"], out selectedYear);
            int.TryParse(Request.QueryString["CalMonth"], out selectedMonth);

            if (selectedYear <= 0) selectedYear = DateTime.Now.Year;
            if (selectedMonth <= 1) selectedMonth = 1;
            else if (selectedMonth > 12) selectedMonth = 12;

            _selectedDate = new DateTime(selectedYear, selectedMonth, 1);
        }

        litSelectedYear.Text = _selectedDate.Year.ToString();

        repMonths.DataSource = new int[]{1,2,3,4,5,6,7,8,9,10,11,12};
        repMonths.DataBind();
    }

    protected void repMonths_OnItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            Repeater repDays = (Repeater)e.Item.FindControl("repDays");
            HyperLink hylMonth = (HyperLink)e.Item.FindControl("hylMonth");
            _currentBindingMonth = (int)e.Item.DataItem;
            _dtMonth = new DateTime(_selectedDate.Year, _currentBindingMonth, 1);

            hylMonth.Text = _dtMonth.ToString("MMM");
            hylMonth.NavigateUrl = string.Format("{0}?CalMonth={1}&CalYear={2}", Request.Path, _currentBindingMonth, _selectedDate.Year);

            if (_currentBindingMonth == _selectedDate.Month) hylMonth.Attributes.Add("class", "selected");

            if (_dtMonth.DayOfWeek != DayOfWeek.Monday)
            {
                int daysToSubtract = -(int)_dtMonth.DayOfWeek;

                if (_dtMonth.DayOfWeek == DayOfWeek.Sunday) daysToSubtract = -7; // Special case. US weeks start with sunday, thus the enum DayOfWeek.Sunday = 0.

                _dtMonth = _dtMonth.AddDays(daysToSubtract + 1);
            }

            DateTime[] dates = new DateTime[37];
            for (int i = 0; i < 37; i++)
            {
                dates[i] = _dtMonth;
                _dtMonth = _dtMonth.AddDays(1);
            }

            repDays.DataSource = dates;
            repDays.DataBind();
        }
    }

    protected void repDays_OnItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            DateTime date = (DateTime)e.Item.DataItem;
            Literal litDay = (Literal)e.Item.FindControl("litDay");
            HtmlTableCell tdDay = (HtmlTableCell)e.Item.FindControl("tdDay");

            if (date.DayOfWeek == DayOfWeek.Saturday || date.DayOfWeek == DayOfWeek.Sunday)
                tdDay.Attributes.Add("class", "weekend");

            if (_currentBindingMonth == date.Month)
                litDay.Text = string.Format("<a href=\"{0}?CalDate={3}-{2}-{1}\"{4}>{3}</a>", Request.Path, date.Year, date.Month, date.Day.ToString("D2"), (_specialDaySelected && date.Equals(_selectedDate)) ? " class=\"selected\"" : string.Empty);
            else
                litDay.Text = string.Format("<span>{0}</span>", date.Day.ToString("D2"));

            // Clear ID's
            tdDay.ID = string.Empty;
        }
    }

    public static string GetDayName(int dayInWeek)
    {
        switch (dayInWeek)
        {
            case 1: return "mo";
            case 2: return "tu";
            case 3: return "we";
            case 4: return "th";
            case 5: return "fr";
            case 6: return "sa";
            case 7: return "su";
        }

        return "God only made seven days in a week.";
    }
}

Whipped up this, just for you:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Yearly Calendar</title>
    <style type="text/css">
        table td { padding:0; margin:0; border:1px solid #dadada; }
        table th { width:25px; text-align:center; }
        table td { text-align:center; }
        table td span { color:#dadada; }
        table td a { color:#000000; text-decoration:none; }
        table td a:hover { text-decoration:underline; }
        table td a.hasEvents { color:#ff0000; }
        table td a.selected { color:#0000ff; font-weight:bold; }
        table td.month { background-color:#999999; }
        table td.weekend { background-color:#D0D0D0; }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <table cellpadding="0" cellspacing="0">
            <thead>
                <tr>
                    <th>
                        <asp:Literal ID="litSelectedYear" runat="server" />
                    </th>
                    <% for (int iWeek = 1; iWeek <= 6; iWeek++) {
                        for (int iDay = 1; iDay <= 7; iDay++) { if (iWeek == 6 && iDay > 2) break; %>
                        <th>
                            <%= GetDayName(iDay) %>
                        </th>
                    <% } } %>
                </tr>
            </thead>
            <asp:Repeater ID="repMonths" runat="server" OnInit="repMonths_OnInit" OnItemDataBound="repMonths_OnItemDataBound">
                <ItemTemplate>                  
                    <tr>
                        <td class="month">
                            <asp:HyperLink ID="hylMonth" runat="server" />
                        </td>
                        <asp:Repeater ID="repDays" runat="server" OnItemDataBound="repDays_OnItemDataBound">
                            <ItemTemplate>
                                <td id="tdDay" runat="server">
                                    <asp:Literal ID="litDay" runat="server" />
                                </td>
                            </ItemTemplate>
                        </asp:Repeater>
                    </tr>
                </ItemTemplate>
            </asp:Repeater>
        </table>
    </div>
    </form>
</body>
</html>

using System;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
    private DateTime _dtMonth;
    private DateTime _selectedDate;
    private bool _specialDaySelected = true;
    private int _currentBindingMonth;

    protected void repMonths_OnInit(object sender, EventArgs e)
    {
        if (!DateTime.TryParse(Request.QueryString["CalDate"], out _selectedDate))
        {
            _specialDaySelected = false;
            int selectedMonth, selectedYear;
            int.TryParse(Request.QueryString["CalYear"], out selectedYear);
            int.TryParse(Request.QueryString["CalMonth"], out selectedMonth);

            if (selectedYear <= 0) selectedYear = DateTime.Now.Year;
            if (selectedMonth <= 1) selectedMonth = 1;
            else if (selectedMonth > 12) selectedMonth = 12;

            _selectedDate = new DateTime(selectedYear, selectedMonth, 1);
        }

        litSelectedYear.Text = _selectedDate.Year.ToString();

        repMonths.DataSource = new int[]{1,2,3,4,5,6,7,8,9,10,11,12};
        repMonths.DataBind();
    }

    protected void repMonths_OnItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            Repeater repDays = (Repeater)e.Item.FindControl("repDays");
            HyperLink hylMonth = (HyperLink)e.Item.FindControl("hylMonth");
            _currentBindingMonth = (int)e.Item.DataItem;
            _dtMonth = new DateTime(_selectedDate.Year, _currentBindingMonth, 1);

            hylMonth.Text = _dtMonth.ToString("MMM");
            hylMonth.NavigateUrl = string.Format("{0}?CalMonth={1}&CalYear={2}", Request.Path, _currentBindingMonth, _selectedDate.Year);

            if (_currentBindingMonth == _selectedDate.Month) hylMonth.Attributes.Add("class", "selected");

            if (_dtMonth.DayOfWeek != DayOfWeek.Monday)
            {
                int daysToSubtract = -(int)_dtMonth.DayOfWeek;

                if (_dtMonth.DayOfWeek == DayOfWeek.Sunday) daysToSubtract = -7; // Special case. US weeks start with sunday, thus the enum DayOfWeek.Sunday = 0.

                _dtMonth = _dtMonth.AddDays(daysToSubtract + 1);
            }

            DateTime[] dates = new DateTime[37];
            for (int i = 0; i < 37; i++)
            {
                dates[i] = _dtMonth;
                _dtMonth = _dtMonth.AddDays(1);
            }

            repDays.DataSource = dates;
            repDays.DataBind();
        }
    }

    protected void repDays_OnItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            DateTime date = (DateTime)e.Item.DataItem;
            Literal litDay = (Literal)e.Item.FindControl("litDay");
            HtmlTableCell tdDay = (HtmlTableCell)e.Item.FindControl("tdDay");

            if (date.DayOfWeek == DayOfWeek.Saturday || date.DayOfWeek == DayOfWeek.Sunday)
                tdDay.Attributes.Add("class", "weekend");

            if (_currentBindingMonth == date.Month)
                litDay.Text = string.Format("<a href=\"{0}?CalDate={3}-{2}-{1}\"{4}>{3}</a>", Request.Path, date.Year, date.Month, date.Day.ToString("D2"), (_specialDaySelected && date.Equals(_selectedDate)) ? " class=\"selected\"" : string.Empty);
            else
                litDay.Text = string.Format("<span>{0}</span>", date.Day.ToString("D2"));

            // Clear ID's
            tdDay.ID = string.Empty;
        }
    }

    public static string GetDayName(int dayInWeek)
    {
        switch (dayInWeek)
        {
            case 1: return "mo";
            case 2: return "tu";
            case 3: return "we";
            case 4: return "th";
            case 5: return "fr";
            case 6: return "sa";
            case 7: return "su";
        }

        return "God only made seven days in a week.";
    }
}
等风也等你 2024-10-19 17:31:52

您可以使用 ASP.NET 控件的组合来构建它,例如两个中继器,一个用于呈现月份,另一个用于呈现日期。这样做是可能的。包括该月的其他日期和包括一周中的某一天(如此处所示)可能会更困难,但这绝对是可能的。

内部中继器绑定日期,外部中继器绑定月份。

另一种方法可能是动态创建 DataTable 类并将所有逻辑用于在表中显示月份。在数据表中创建天数列,并为每个月创建一个新行。那也可以。

HTH。

You could build that with a combination of ASP.NET controls, such as two repeaters, one to render the months and one to render the days. It's possible to do it that way. It may be more difficult to include the other days of the month and to include the day of the week as depicted there, but its definitely possible.

The inner repeater binds the days, and the outer repeater binds the months.

An alternative may be to dynamically create a DataTable class and put all the logic to display the months within the table. Create as columns in the datatable the days, and create a new row for each month. That would work out too.

HTH.

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