This Article will allow you to customise the Calendar Control in ASP.NET according to your requirements.
Disabling Dates,Formating how dates Appears etc...
This Article will show you how to customise the Calendar Control in ASP.NET.
The Day_Render Event of the Calendar Control is used for many purposes like Enabling and Disabling Valid Dates.
Changing Visiblity of Selected Dates etc.
I will be showing some of the Usages of the Day_Render Event.
A) Restrict users to select date less than todays date.
The Following Code Has to be written in the Day_Render event of the Calendar Control.
protected void Calendar1_DayRender(object sender, DayRenderEventArgs e)
{
if (e.Day.Date >= DateTime.Today.Date)
{
e.Cell.Enabled= false; // Changes the Cells Enable Property
e.Day.IsSelectable = false; // Disables the Cells Click Property
}
}
B) Allow users only to select dates greater than todays date
protected void Calendar1_DayRender(object sender, DayRenderEventArgs e)
{
if (e.Day.Date <= DateTime.Today.Date)
{
e.Cell.Enabled= false; // Changes the Cells Enable Property
e.Day.IsSelectable = false; // Disables the Cells Click Property
}
}
C) Change apprearnce of selected dates and add tooltips or Disable the Click on the Selected Date
protected void Calendar1_DayRender(object sender, DayRenderEventArgs e)
{
if (e.Day.Date == DateTime.Today.AddDays(-3))//Code for Specific Date
{
e.Cell.ForeColor = System.Drawing.Color.Red;
e.Cell.ToolTip = "Holiday "; // ToolTip for the Date
e.Day.IsSelectable = false;
}
if (e.Day.Date == DateTime.Today.AddDays(-7))//Code for Specific Date
{
e.Cell.ForeColor = System.Drawing.Color.Red;
e.Cell.ToolTip = "Holiday "; // ToolTip for the Date
e.Day.IsSelectable = false;
}
}
D) To Mark weekends on the calander control
protected void Calendar1_DayRender(object sender, DayRenderEventArgs e)
{
if (e.Day.IsWeekend)
{
e.Cell.ForeColor = System.Drawing.Color.Red;
}
}
Regards
Hefin Dsouza