1

有没有更简单的方法在 asp:Calendar 控件中显示周数?我找到了这篇文章,但它是 2008 年的帖子。认为现在可能有更简单的方法吗?

我正在使用 asp.net c# 4.0 进行开发。

谢谢

4

3 回答 3

1

您可以使用该DayRender事件并将星期数添加到单元格中:

protected void Calendar1_DayRender(object sender, DayRenderEventArgs e)
{
    int weeknum = CultureInfo.CurrentCulture.Calendar.GetWeekOfYear(
            e.Day.Date,
            CultureInfo.CurrentCulture.DateTimeFormat.CalendarWeekRule,
            CultureInfo.CurrentCulture.DateTimeFormat.FirstDayOfWeek
    );
    e.Cell.Controls.Add(new LiteralControl(string.Format("({0})", weeknum)));
}

如果这还不够可爱,因为它总是将星期数添加到单元格中,请使用您的链接

于 2013-02-05T16:32:01.710 回答
0

相同的语法应该仍然有效。请记住,不同文化中的周计算方式不同,如果您看到第 53 周,则没有错误!

using System.Globalization;

CultureInfo cultInfo = CultureInfo.CurrentCulture;
int weekNumNow = cultInfo.Calendar.GetWeekOfYear(DateTime.Now,
                         cultInfo.DateTimeFormat.CalendarWeekRule,
                         cultInfo.DateTimeFormat.FirstDayOfWeek);
于 2013-02-05T16:29:18.100 回答
0

我已经设法使该代码也可以更改周数,这是代码:

后面的代码:

protected void Page_Load(object sender, EventArgs e)
{
    if (!Page.IsPostBack)
    {
        eventsDayView.DataLoad(DateTime.Now);
        calendar.SelectedDate = DateTime.Now;
        calendar.TodaysDate = DateTime.Now;
        calendar.VisibleDate = DateTime.Today;
        addWeekNumberColumn();
    }
}

private void addWeekNumberColumn()
{
    // Get the date shown in the calendar control
    DateTime curMonth = calendar.VisibleDate;

    // Find first day of the current month
    curMonth = Convert.ToDateTime(curMonth.Year.ToString() + "-" + curMonth.Month.ToString() + "-01");

    // Build javascript
    string jscript = "<script type='text/javascript'> addWkColumn('" + calendar.ClientID + "', " + getISOWeek(curMonth).ToString() + ");</script>";

    // Add script to page for execution of addWkColumn javascript function
    Page.ClientScript.RegisterStartupScript(this.GetType(), "AddWeeknumbers", jscript);
}

// Helper function to find ISO week
private int getISOWeek(DateTime day)
{
    return System.Globalization.CultureInfo.CurrentCulture.Calendar.GetWeekOfYear(day, System.Globalization.CalendarWeekRule.FirstFourDayWeek, DayOfWeek.Monday);
}

protected void calendar_OnVisibleMonthChanged(object sender, MonthChangedEventArgs e)
{
    calendar.VisibleDate = e.NewDate;
    addWeekNumberColumn();
}

ASP:

<script type="text/javascript">
        function addWkColumn(tblId, wkStart) {
            var tbl = document.getElementById(tblId);
            var tblBodyObj = tbl.tBodies[0];
            for (var i = 0; i < tblBodyObj.rows.length; i++) {
                // Month Header
                if (i == 0) {
                    // Add extra colspan column
                    tblBodyObj.rows[i].cells[0].colSpan = 8;
                }
                // Week Header
                if (i == 1) {
                    // Add week column headline
                    var newCell = tblBodyObj.rows[i].insertCell(0);
                    newCell.innerHTML = 'wk';
                    newCell.style.fontSize = '8px';
                    newCell.style.fontWeight = 'bold';
                    newCell.style.verticalAlign = 'bottom';
                    newCell.style.backgroundColor = '#ffffee';
                }
                // Normal row
                if (i >= 2) {
                    // Add the weeknumbers 
                    var newCell = tblBodyObj.rows[i].insertCell(0);
                    newCell.innerHTML = wkStart;
                    wkStart += 1;
                    if (wkStart == 53) {
                        wkStart = 1;
                    }
                    newCell.style.fontSize = '8px';
                    newCell.style.backgroundColor = '#ffffee';
                }
            }
        }
    </script>

<asp:Calendar ID="calendar" runat="server" Font-Names="Tahoma" Font-Size="11px" NextMonthText="&raquo;"
                                                    PrevMonthText="&laquo;" SelectMonthText="&raquo;" SelectWeekText="&rsaquo;" CssClass="myCalendar"
                                                    BorderStyle="None" CellPadding="1" OnSelectionChanged="calendar_SelectionChanged" OnDayRender="calendar_dayrender"
                                                    meta:resourcekey="calendarResource1" FirstDayOfWeek="Monday" OnVisibleMonthChanged="calendar_OnVisibleMonthChanged">
                                                    <DayHeaderStyle CssClass="myCalendarDayHeader" />
                                                    <DayStyle CssClass="myCalendarDay" />
                                                    <NextPrevStyle CssClass="myCalendarNextPrev" />
                                                    <OtherMonthDayStyle ForeColor="Gray" />
                                                    <SelectedDayStyle Font-Bold="True" />
                                                    <SelectorStyle CssClass="myCalendarSelector" />
                                                    <TitleStyle CssClass="myCalendarTitle" />
                                                    <TodayDayStyle ForeColor="Red" Font-Bold="True" />
                                                </asp:Calendar>
于 2013-02-11T12:20:12.143 回答