我有一個簡單的日曆頁面,我需要用可用日期列表填充它。我應該在哪裏放置這個查詢來避免多次運行?
我得到的名單如下:
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事件可以訪問它?
你能添加一個if語句來檢查一個標誌''''''''嗎? – Jonesopolis