如何在asp.net中以编程方式选择和取消选择日历控件中的多个日期?

时间:2013-12-11 16:25:26

标签: c# 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/>");
            }
        }
}

上面的代码只选择多个日期,我们如何在上面的代码中逐个取消选择日期?

1 个答案:

答案 0 :(得分:3)

删除当前所选日期

Calendar1.SelectedDates.Remove(Calendar1.SelectedDate);

一次删除一个日期

Calendar1.SelectedDates.Remove(Calendar1.SelectedDates[0]);

或者一次清除所有选定的日期

Calendar1.SelectedDates.Clear();

查看 SelectedDates

的所有可用方法