【问题标题】:Multiple Date selection using asp calendar使用asp日历选择多个日期
【发布时间】:2016-01-06 06:14:10
【问题描述】:

我有 asp 日历可以选择多个日期。

 <asp:UpdatePanel ID="updpnl" runat="server" UpdateMode="Conditional">
            <ContentTemplate>
                <asp:Calendar ID="Calendar1" runat="server" BackColor="White" BorderColor="White" BorderWidth="1px" Font-Names="Verdana" Font-Size="9pt" ForeColor="Black" NextPrevFormat="FullMonth" OnPreRender="Calendar1_PreRender" OnSelectionChanged="Calendar1_SelectionChanged" OnDayRender="Calendar1_DayRender">

            </asp:Calendar>
            <asp:Button ID="btnClearSelection" runat="server" Text="Clear"
                        OnClick="btnClearSelection_Click" />
            <br />
        </ContentTemplate>
    </asp:UpdatePanel>

代码如下:

protected void Page_Load(object sender, EventArgs e)
        {



        }


        public List<DateTime> SelectedDates
        {
              get
        {
            if (ViewState["Dates"] == null)

                ViewState["Dates"] = new List<DateTime>();
            return (List<DateTime>)ViewState["Dates"];
        }
        set
        {
            ViewState["Dates"] = value;
        }
        }
        protected void Calendar1_PreRender(object sender, EventArgs e)
        {


            // Reset Selected Dates
            Calendar1.SelectedDates.Clear();
            // Select previously Selected Dates
            foreach (DateTime dt in SelectedDates)
                Calendar1.SelectedDates.Add(dt);
        }
        protected void Calendar1_SelectionChanged(object sender, EventArgs e)
        {
            //Check if selected Date is in the saved list
            // Remove the Selected Date from the saved list
            if (SelectedDates.Contains(Calendar1.SelectedDate))
                SelectedDates.Remove(Calendar1.SelectedDate);
            else
                 SelectedDates.Add(Calendar1.SelectedDate);
            ViewState["Dates"] = SelectedDates;
        }
        protected void Calendar1_DayRender(object sender, System.Web.UI.WebControls.DayRenderEventArgs e)
        {
            if (e.Day.Date <= DateTime.Today)
            {

                e.Day.IsSelectable = false;
            }



            // Disable dates of past/future months
            if (e.Day.IsOtherMonth)
            {
                e.Day.IsSelectable = false;
                e.Cell.Text = "X";
            }
        }


    }

如果我不选择任何日期并尝试提交,则在 Calendar1.SelectedDates.Count 中显示 '1' 并且值为 '{12/29/9999} '。 如何从列表中删除它??

谢谢。

【问题讨论】:

    标签: c# asp.net


    【解决方案1】:

    你可以试试这个来选择多个日期:

    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();
            }
    }
    

    Source

    【讨论】:

    • 感谢您的回复。我的代码没有任何问题。但只有一件事是我无法在第一次点击时选择日期。如果我将使用 Session 作为日期,刷新后,已选择的日期将显示为已选择。
    猜你喜欢
    • 1970-01-01
    • 2015-06-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-16
    • 2019-02-13
    • 1970-01-01
    相关资源
    最近更新 更多