[英]Calendar Control - Highlight Dates Programmatically
我正在玩日歷控件,我似乎無法完成簡單的着色日期任務。 如果用戶輸入 7 個日期,我想在日歷上遮蓋這些日期,以便用戶知道它們已被選中。
基本上我想做 Calendar.HighlightDate("5/1/11") => imaginary 大聲笑我知道這一定很簡單,但我正在瀏覽 MSDN 上的屬性並沒有找到任何東西。
設置日歷 object 的ondayrender事件:
<asp:Calendar ID="Calendar1" runat="server" ondayrender="MyDayRenderer">
然后在后面的代碼中,您可以檢查日期並設置顏色:
protected void MyDayRenderer(object sender, DayRenderEventArgs e)
{
if (e.Day.IsToday)
{
e.Cell.BackColor = System.Drawing.Color.Aqua;
}
if (e.Day.Date == new DateTime(2011,5,1))
{
e.Cell.BackColor = System.Drawing.Color.Beige;
}
}
這是我很久以前在一個項目中使用的一些代碼。 現在可能有更好的方法。 但這應該有效。 我能找到的最簡單的方法是參與 DayRender 事件。
我用它來突出顯示已預訂、待定或可出租的某些日期。
protected void Calendar1_DayRender(object sender, DayRenderEventArgs e)
{
for (int x = 0; x < ar.Count; x++)
{
//if the date is in the past, just mark it as booked.
if (e.Day.Date < DateTime.Now)
{
e.Cell.BackColor = System.Drawing.Color.FromArgb(38, 127, 0);
e.Cell.ForeColor = System.Drawing.Color.White;
}
if (e.Day.Date.ToShortDateString() == Convert.ToDateTime(((ListItem)ar[x]).Text).ToShortDateString())
{
switch (((ListItem)ar[x]).Value)
{
case "1":
e.Cell.BackColor = System.Drawing.Color.FromArgb(220,220,220);
break;
case "2":
e.Cell.BackColor = System.Drawing.Color.FromArgb(38,127,0);
e.Cell.ForeColor = System.Drawing.Color.White;
break;
case "3":
if (e.Day.IsWeekend)
{
e.Cell.BackColor = System.Drawing.Color.FromArgb(255,255,204);
}
else
{
e.Cell.BackColor = System.Drawing.Color.White;
}
break;
default:
e.Cell.BackColor = System.Drawing.Color.White;
break;
}
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.