在asp:Calendar中显示周数

时间:2013-02-05 16:25:22

标签: c# asp.net

是否有更简单的方法在asp:Calendar控件中显示周数? 我发现了this帖子,但这是2008年的帖子。以为现在可能会有一种更简单的方法?

我在asp.net c#4.0中开发。

由于

3 个答案:

答案 0 :(得分: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)));
}

如果这不够可爱,因为它总是将周数添加到单元格使用your link

答案 1 :(得分:0)

相同的语法应该仍然有用。请记住,那个星期的计算在不同的文化中有不同的表现,如果你看到第53周,就没有错误!

using System.Globalization;

CultureInfo cultInfo = CultureInfo.CurrentCulture;
int weekNumNow = cultInfo.Calendar.GetWeekOfYear(DateTime.Now,
                         cultInfo.DateTimeFormat.CalendarWeekRule,
                         cultInfo.DateTimeFormat.FirstDayOfWeek);

答案 2 :(得分: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>