使用.net 4.6.1,我正在后端进行计算,以确定客户何时可以选择订单日期,具体取决于他们的每周发票日。计算和SelectedDate部分一样正常运行,但是,当SelectedDate恰好在以下月内时,默认情况下,我的日历不会自动前进以显示该月,它始终显示当前月而不管。我在做什么错了?
protected void Calendar1_DayRender(object sender, DayRenderEventArgs e)
{
DayOfWeek invoiceDay = DayOfWeek.Wednesday;
DateTime nextMonday = GetNextWeekday(DateTime.Today.AddDays(1), DayOfWeek.Monday, invoiceDay);
if (e.Day.Date < nextMonday)
{
e.Day.IsSelectable = false;
Calendar1.SelectedDate = nextMonday;
Calendar1.VisibleDate = Calendar1.SelectedDate;
}
}
public static DateTime GetNextWeekday(DateTime start, DayOfWeek day, DayOfWeek _invoiceDay)
{
int daysToAdd = ((int)day - (int)start.DayOfWeek + 7) % 7;
if (_invoiceDay > DateTime.Now.DayOfWeek)
{
return start.AddDays(daysToAdd);
}
else
{
return start.AddDays(daysToAdd + 7);
}
}
答案 0 :(得分:0)
我发现了我的问题。我只需要一个用于初始日历设置的单独方法,并在页面加载时调用它,而不是尝试从DayRender事件中调用所有方法。更新后的代码如下:
private DayOfWeek logDay = DayOfWeek.Wednesday;
private DateTime nextMonday;
protected void Page_Load(object sender, EventArgs e)
{
CalendarSetup();
}
private void CalendarSetup ()
{
if (!IsPostBack)
{
logDay = DayOfWeek.Wednesday;
nextMonday = GetNextWeekday(DateTime.Today.AddDays(1), DayOfWeek.Monday, logDay);
Calendar1.SelectedDate = nextMonday;
Calendar1.VisibleDate = Calendar1.SelectedDate;
}
}
protected void Calendar1_DayRender(object sender, DayRenderEventArgs e)
{
if (e.Day.Date < nextMonday)
{
e.Day.IsSelectable = false;
}
}
public static DateTime GetNextWeekday(DateTime start, DayOfWeek day, DayOfWeek _logDay)
{
int daysToAdd = ((int)day - (int)start.DayOfWeek + 7) % 7;
if (_logDay > DateTime.Now.DayOfWeek)
{
return start.AddDays(daysToAdd);
}
else
{
return start.AddDays(daysToAdd + 7);
}
}