gpt4 book ai didi

c# - 在日历控件中自定义选择

转载 作者:太空宇宙 更新时间:2023-11-03 16:28:01 25 4
gpt4 key购买 nike

我在 Asp.Net 中使用日历控件。我要实现的条件是“如果用户从一周中选择一个特定的工作日,他不应该能够从任何不同的周中选择任何其他日期”。比如,所有的选择都应该在一周内完成。但是我无法弄清楚如何禁用选定日期那一周以外的其余几周。

最佳答案

I would go about it like this.. when the selected date is changed, get the start and end of that particular week.然后,在呈现每一天时,确保这一天在该周范围内。

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

关于c# - 在日历控件中自定义选择,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11876632/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com