2012-08-09 48 views
2

我在Asp.Net中使用日曆控件。我想要實施的條件是「如果用戶從一週內選擇某個特定的工作日,那麼他應該無法從任何不同的星期選擇任何其他日期。」喜歡,所有的選擇應該只在一週內完成。但我無法弄清楚如何在選定日期的一週內禁用剩下的幾周。在日曆控件中自定義選擇

+0

你能否向我們提供你到目前爲止 – KKP 2012-08-09 04:20:55

回答

0

我會這樣做。當選擇的日期發生變化時,獲取該特定周的開始和結束。然後,在每天呈現時,確保該日落在該周的範圍內。

private DateTime? _firstDayOfWeek; 
private DateTime? _lastDayOfWeek; 

protected void yourCalendar_SelectionChanged(object sender, EventArgs e) 
{ 
    var selectedDate = yourCalendar.SelectedDate; 

    DayOfWeek firstDay = CultureInfo.CurrentCulture.DateTimeFormat.FirstDayOfWeek; 

    _firstDayOfWeek = selectedDate.Date; 
    while (_firstDayOfWeek.Value.DayOfWeek != firstDay) 
     _firstDayOfWeek = _firstDayOfWeek.Value.AddDays(-1); 

    _lastDayOfWeek = selectedDate.Date; 
    while (_lastDayOfWeek.Value.DayOfWeek != _firstDayOfWeek.Value.AddDays(6).DayOfWeek) 
     _lastDayOfWeek = _lastDayOfWeek.Value.AddDays(1); 
} 

protected void yourCalendar_DayRender(object sender, DayRenderEventArgs e) 
{ 
    if (_firstDayOfWeek.HasValue && _lastDayOfWeek.HasValue) 
    { 
     // There are dates to work with now, so check to see if the day falls 
     // outside of the week range... 
     if (e.Day.Date.Ticks < _firstDayOfWeek.Value.Ticks || e.Day.Date.Ticks > _lastDayOfWeek.Value.Ticks) 
      e.Day.IsSelectable = false; 
    } 
} 
+0

非常感謝。它真的幫了我很多,解決了我的問題。謝謝。 – user1586482 2012-08-11 00:04:10

+0

非常歡迎!很高興爲你解決.... – 2012-08-11 00:05:57