我在 Asp.Net 中使用日历控件。我要实现的条件是“如果用户从一周中选择特定的工作日,他应该不能从任何不同的周中选择任何其他日期”。就像,所有的选择都应该在一周内完成。但我无法弄清楚如何禁用除选定日期的一周之外的其余几周。
问问题
731 次
1 回答
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;
}
}
于 2012-08-09T05:09:03.193 回答