.net日历控件 Calendar选择多个日期

时间:2022-06-16 18:10:53

由于使用了Calendar的原因,每选择一个时间页面会被刷新,可以在页面上加个UpdatePanel

 public List<DateTime> SelectDate
{
set
{
ViewState["sd"] = value;
}
get
{
if (ViewState["sd"] == null)
ViewState["sd"] = new List<DateTime>();
return (List<DateTime>)ViewState["sd"];
}
}
protected void Calendar1_DayRender(object sender, DayRenderEventArgs e)
{
List<DateTime> selectDate = SelectDate;
foreach (var item in SelectDate)
{
if (e.Day.IsOtherMonth)
e.Cell.Controls.Clear();
if (e.Day.Date == item)
e.Cell.BackColor = System.Drawing.Color.Red;
}
}
protected void Calendar1_SelectionChanged(object sender, EventArgs e)
{

List<DateTime> lst = SelectDate;
if (lst.Contains(Calendar1.SelectedDate))
{
lst.Remove(Calendar1.SelectedDate);
}
else
{
lst.Add(Calendar1.SelectedDate);
}
}
protected void Button1_Click(object sender, EventArgs e)
{
foreach (var item in SelectDate)
{
Response.Write(item.ToString() + "<BR/>");
}
}