2013-02-05 93 views
1

是否有更簡單的方式顯示asp:Calendar控件中的星期數? 我發現this後,但它是一個2008年的職位。以爲現在可能有一個更簡單的方法?在asp中顯示星期數:日曆

我在asp.net c#4.0中開發。

謝謝

回答

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> 
0

相同的語法應該仍然工作。請記住,那一週的計算在不同的文化中是不一樣的,如果你看到第53周的數據,那麼就沒有錯誤!

using System.Globalization; 

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

您可以使用DayRender事件和weeknumber添加到單元格:

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))); 
} 

如果這還不夠可愛,因爲它總是添加weeknumber到小區使用your link