[英]How to programmatically select and deselect multiple dates in calendar control in asp.net?
如何在asp.net的日歷控件中以編程方式選擇和取消選擇多個日期?
using System.Collections.Generic;
public static List<DateTime> list = new List<DateTime>();
protected void Calendar1_DayRender(object sender, DayRenderEventArgs e)
{
if (e.Day.IsSelected == true)
{
list.Add(e.Day.Date);
}
Session["SelectedDates"] = list;
}
protected void Calendar1_SelectionChanged(object sender, EventArgs e)
{
if (Session["SelectedDates"] != null)
{
List<DateTime> newList = (List<DateTime>)Session["SelectedDates"];
foreach (DateTime dt in newList)
{
Calendar1.SelectedDates.Add(dt);
}
list.Clear();
}
}
protected void Button1_Click(object sender, EventArgs e)
{
if (Session["SelectedDates"] != null)
{
List<DateTime> newList = (List<DateTime>)Session["SelectedDates"];
foreach (DateTime dt in newList)
{
Response.Write(dt.ToShortDateString() + "<BR/>");
}
}
}
上面的代碼僅選擇多個日期,如何在上面的代碼中一個接一個地取消選擇日期?
刪除當前選擇的日期
Calendar1.SelectedDates.Remove(Calendar1.SelectedDate);
一次刪除一個日期
Calendar1.SelectedDates.Remove(Calendar1.SelectedDates[0]);
或者一次清除所有選定的日期
Calendar1.SelectedDates.Clear();
查看SelectedDates的所有可用方法
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.