Introduction
Many times, I had questions about how to make a holiday calendar. Holiday calendar means what are the public holidays and what are the optional holidays and user defined holidays. These holidays must be added to the calendar and should show that day with different colors. So here, I am posting a tip with all the codes, maybe this will help you.
Using the Code
The ASPX source code is as follows:
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<table>
<tr>
<td colspan="2" align="center">
<asp:Label ID="lblName" runat="server" Text="Holiday
Calender" Font-Size="Large" Font-Bold="true"></asp:Label>
</td>
</tr>
<tr>
<td></td>
<td></td>
</tr>
<tr>
<td>
<asp:Calendar ID="Calendar1" runat="server" ondayrender="Calendar1_DayRender"
onvisiblemonthchanged="Calendar1_VisibleMonthChanged"></asp:Calendar>
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
Write the code behind:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
public partial class _Default : System.Web.UI.Page
{
SqlConnection con = new SqlConnection
(ConfigurationManager.ConnectionStrings["connection"].ConnectionString.ToString());
private List<DateTime> holidays=new List<DateTime>();
private List<string> holidayname = new List<string>();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
GetHolidays();
}
}
private void GetHolidays()
{
try
{
con.Open();
SqlCommand cmd = new SqlCommand("select * from tblHoliday",con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
holidays.Add((DateTime)ds.Tables[0].Rows[i][1]);
holidayname.Add((string)ds.Tables[0].Rows[i][2]);
}
}
catch { }
finally { con.Close(); }
}
protected void Calendar1_DayRender(object sender, DayRenderEventArgs e)
{
string tooltip = string.Empty;
if (IsEventDay(e.Day.Date, out tooltip))
{
e.Cell.BackColor = System.Drawing.Color.Pink;
e.Day.IsSelectable = false;
e.Cell.ToolTip = tooltip;
}
if (e.Day.IsWeekend)
{
e.Cell.BackColor = System.Drawing.Color.Black;
e.Cell.ForeColor = System.Drawing.Color.White;
e.Day.IsSelectable = false;
}
}
private bool IsEventDay(DateTime day, out string tooltipvalue)
{
tooltipvalue = string.Empty;
for (int i = 0; i < holidays.Count; i++)
{
if (holidays[i] == day)
{
tooltipvalue = holidayname[i];
return true;
}
}
return false;
}
protected void Calendar1_VisibleMonthChanged(object sender, MonthChangedEventArgs e)
{
GetHolidays();
}}
These are the entire code where you can highlight your holiday list and weekend list in a calendar from database.
Please note that a calendar control can't be bind data like gridview
, it has a DayRender
event. So you have to raise that event and write the code for that, and another event you have to raise that is VisbleMonthChanged
so that whatever the holidays list is for the current month as well as next moth will show and will be bound with the calendar control. I have attached the code and database. Please download the attached files and run it.