Geeks With Blogs

News

Microsoft MVP


DZone MVB


Moderator at CodeASP.NET


Quiz Master







free counters
Free counters
Added on January 19,2012


Follow Me @vmsdurano

A bit About Me



Disclaimer
The opinions expressed herein are my own personal opinions and does not represent the opinions of my employers. Nor does it represent the opinion of my dog, because I don’t have one.


Vinz' Blog (ProudMonkey) "Code, Beer and Music ~ my way of being a programmer"

This demo shows on how to do multiple date selections in the standard ASP.NET Calendar Control.

The basic idea here is we use List collection to hold the selected dates from the calendar control and store them into Session to keep the selected dates on postbacks and assigned the selected dates back based from the list values at SelectionChanged event..

Here's the code blocks below:

First we need to declare the following namespace below for us to use the List Class

using System.Collections.Generic;


Then declare our list as a public like this:

public static List<DateTime> list = new List<DateTime>();

now here's the code for handling the multiple date selections

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


So when you run the page, you can select multiple calendar dates by just clicking on the dates from the Calendar control.. Here's the sample output below



Now lets print the selected dates. Here's the code block below:

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


Clicking on the Print Button will display all the dates that was selected from the Calendar..

Here's the code line for Clearing the SelectedDates from the Calendar Control.

Calendar1.SelectedDates.Clear();



That's it..

Technorati Tags: ,,
Posted on Monday, March 9, 2009 3:56 PM ASP.NET , C# , Tips&Tricks | Back to top


Comments on this post: Tip/Trick: ASPNET Calendar Control with Multiple Date Selections

# re: Tip/Trick: ASPNET Calendar Control with Multiple Date Selections
Requesting Gravatar...
Good post, very informative
Left by www.onlineisgreat.com on Nov 08, 2009 10:32 PM

# re: Tip/Trick: ASPNET Calendar Control with Multiple Date Selections
Requesting Gravatar...
great code man you saved me !!
Left by bush on Jan 14, 2010 7:30 AM

# re: Tip/Trick: ASPNET Calendar Control with Multiple Date Selections
Requesting Gravatar...
Dear Vince,

Awesome job with this. Just enough info, well presented. I had to translate it to VB.NET. I'd be happy to mail the code to you if you want to post it to help the VB world.

Best,

Mike
Left by Michael Cullina on May 13, 2010 6:45 AM

# re: Tip/Trick: ASPNET Calendar Control with Multiple Date Selections
Requesting Gravatar...
Thanks :)
It worked for me
Left by Chitra on Jun 17, 2010 1:47 PM

# re: Tip/Trick: ASPNET Calendar Control with Multiple Date Selections
Requesting Gravatar...
if i click on the seleted date again then it should be deseleted
please any help for this ?????
Left by Hasu on Jun 19, 2010 5:30 PM

# re: Tip/Trick: ASPNET Calendar Control with Multiple Date Selections
Requesting Gravatar...
If you want to keep them selected after another selection just put the selected dates in session and assign them again
Left by Ped on Jul 01, 2010 3:27 AM

# re: Tip/Trick: ASPNET Calendar Control with Multiple Date Selections
Requesting Gravatar...
once I selected the date on calender control, later it should be disable
also i want to provide a special text to the selected date...
vl u please help me...
Left by ramesh on Aug 18, 2010 11:36 PM

# re: Tip/Trick: ASPNET Calendar Control with Multiple Date Selections
Requesting Gravatar...
Greatwork Thanks a lot
Left by Anu on Sep 27, 2010 6:57 PM

# re: Tip/Trick: ASPNET Calendar Control with Multiple Date Selections
Requesting Gravatar...
Once a click a certain date and those date changed a background color like green ,but when i am once again click on same date then changed a color once again white not green. So it is possible
Left by amol on Oct 18, 2010 9:43 PM

# re: Tip/Trick: ASPNET Calendar Control with Multiple Date Selections
Requesting Gravatar...
How also bind saved date
Left by amol on Oct 19, 2010 4:52 PM

# re: Tip/Trick: ASPNET Calendar Control with Multiple Date Selections
Requesting Gravatar...
This code does not work if you change the month on the calendar. If you select multiple dates, change the month, and select multiple dates in the new month, you will lose the dates you had selected in the previous month. Also, the use of the static variable can cause unintended results.
Left by Haresh on Nov 10, 2010 11:17 PM

# re: Tip/Trick: ASPNET Calendar Control with Multiple Date Selections
Requesting Gravatar...
to disable selected date you must change code at foreach loop to:
foreach (DateTime dt in newList)
{
if (Calendar1.SelectedDates.Contains(dt) || Calendar1.SelectedDate == dt)
{
Calendar1.SelectedDates.Remove(dt);
}
else
{
Calendar1.SelectedDates.Add(dt);
}
}
Left by PluCIorX on Jan 31, 2011 7:37 PM

# re: Tip/Trick: ASPNET Calendar Control with Multiple Date Selections
Requesting Gravatar...
i want to save that selected date please help me
Left by neha pal on Mar 07, 2011 1:37 PM

# re: Tip/Trick: ASPNET Calendar Control with Multiple Date Selections
Requesting Gravatar...
Thank you for this easy solution.
Left by Arsen on Mar 27, 2011 4:12 PM

# re: Tip/Trick: ASPNET Calendar Control with Multiple Date Selections
Requesting Gravatar...
Thank you for the good solution.
Left by AP on May 04, 2011 4:50 PM

# re: Tip/Trick: ASPNET Calendar Control with Multiple Date Selections
Requesting Gravatar...
Great solution and kindly provide these in vb code
Left by munna on May 30, 2011 6:49 PM

# re: Tip/Trick: ASPNET Calendar Control with Multiple Date Selections
Requesting Gravatar...
@munna,
Use the online code converter: http://geekswithblogs.net/dotNETvinz/archive/2009/04/14/c-and-vb.net-code-converter.aspx
Left by Vinz on May 31, 2011 2:15 PM

# re: Tip/Trick: ASPNET Calendar Control with Multiple Date Selections
Requesting Gravatar...
great code man, thank u
Left by jr on Jul 05, 2012 4:26 AM

# re: Tip/Trick: ASPNET Calendar Control with Multiple Date Selections
Requesting Gravatar...
You can select multiple dates in calendar control.
There is a no direct way to select multiple dates in calendar control. You need to write some code for that.

Here is example for this.
In this example we take one calendar control and select multiple dates in that control and we display that selected dates in label.

This example is has big code so visit this link. Here you can also download sample project
http://jayeshsorathia.blogspot.com/2012/09/beginning-net-multiple-selection-of-date-in-calendar-control.html
Left by Jayesh Sorathia on Oct 10, 2012 4:11 PM

comments powered by Disqus

Copyright © Vincent Maverick Durano | Powered by: GeeksWithBlogs.net