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:
ASP.NET,
C#,
TipsTricks