我有一个简单的日历页面,我需要用可用日期列表填充它。
我得到的列表如下:
List<DateTime> availableDates = (from d in sb.GetDaysWithAvailableSlotsByLocation(3)
select d.Date).ToList<DateTime>();
但是,看起来日历在 Page_load 中调用此函数之前呈现,因为我需要将此查询放入我的代码中两次以使其工作:
public partial class BookYourSlot : System.Web.UI.Page
{
public SlotBooker sb = new SlotBooker();
public List<DateTime> availableDates = new List<DateTime>();
protected void Page_Load(object sender, EventArgs e)
{
List<DateTime> availableDates = (from d in sb.GetDaysWithAvailableSlotsByLocation(3)
select d.Date).ToList<DateTime>();
DateTime firstAvailable = availableDates.Min();
calSlotBooker.VisibleDate = firstAvailable;
}
protected void calSlotBooker_DayRender(object sender, DayRenderEventArgs e)
{
List<DateTime> availableDates = (from d in sb.GetDaysWithAvailableSlotsByLocation(3)
select d.Date).ToList<DateTime>();
if (!(availableDates.Contains(e.Day.Date)) || (e.Day.Date.DayOfWeek == DayOfWeek.Saturday) || (e.Day.Date.DayOfWeek == DayOfWeek.Sunday))
{
e.Cell.BackColor = System.Drawing.Color.Silver;
e.Day.IsSelectable = false;
e.Cell.ToolTip = "Unavailable";
}
}
}
这似乎非常低效,因为每次呈现单元格时它都会调用 availableDates 查询。
如何在页面上执行一次,并使其可供日历控件的 DayRender 事件访问?