繁体   English   中英

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

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM