RUPA
RUPA

Reputation: 211

Multiple Date selection using asp calendar

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)

                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";
            }
        }


    }

If i wont select any date and try to submit, In Calendar1.SelectedDates.Count shows '1' and the value is '{12/29/9999} ' . How to remove this from the List ??

Thank you.

Upvotes: 3

Views: 3682

Answers (1)

Rahul Tripathi
Rahul Tripathi

Reputation: 172628

You can try this to select multiple dates:

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

Upvotes: 2

Related Questions