I have asp calendar to select multiple dates.

 <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>

Code as follow:

protected void Page_Load(object sender, EventArgs e)
        {



        }


        public List<DateTime> SelectedDates
        {
            get
            {
                if (ViewState["Dates"] != null)
                    return (List<DateTime>)ViewState["Dates"];
                else
                    // Add a hidden dateTime to clear the selection of Date when
                    // there is only one date in the selected Dates
                    return new List<DateTime>() { DateTime.MaxValue.AddDays(-2) };
            }
            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";
            }
        }


    }

On first click, its not possible to select date. Second click on wards selecting multiple date is possible.

If i add prerender code inside Page_Load(), I can't select multiple dates. Can you please help me in this.

Thank you.

Related posts

Recent Viewed