Displaying a Special Day in Calendar Control with special effects
In this article we are going to see how we can display or color a special day such as Diwali which is on 3rd November with a Different color and add a caption to that day as Diwali in the Calendar Control
In this article we are going to see how we can display or color a special day such as Diwali which is on 3rd November with a Different color and add a caption to that day as Diwali in the Calendar Control.
1. Add a Calendar Control to your WebPage as shown below:
<asp:Calendar ID="Calendar1" runat="server"></asp:Calendar>
2. Now go to design view of the WebPage and select the Calendar Control. Press F4 to go to the properties of the Calendar Control. Now select the Events tab which is next to the properties tab and double click on DayRender event. The events tab is highlighted as below:
This generates the Calendar1_DayRender event in the code behind file as shown below:
protected void Calendar1_DayRender(object sender, DayRenderEventArgs e)
{
}
3.Now add the below code to the Calendar1_DayRender event handler. This code checks if the day being rendered is 3rd November 2013(Diwali). Then If the date matches then it creates a Label control with text as "Diwali" and ForeColor as Maroon. It also highlights this day by assigning backcolor to this cell with LightBlue color. Now add this Label control to the Cell object's Controls collection in the DayRender event handler. The Calendar control's DayRender event fires when each day is being rendered.
protected void Calendar1_DayRender(object sender, DayRenderEventArgs e)
{
if (e.Day.Date.Equals(new DateTime(2013, 11, 3)))
{
Label lblCaption = new Label();
lblCaption.ForeColor = System.Drawing.Color.Maroon;
lblCaption.Text = "<br/>Diwali";
e.Cell.BackColor = System.Drawing.Color.LightBlue;
e.Cell.Controls.Add(lblCaption);
}
}
Finally run the application and the output looks as shown below:
The Calendar control doesn't directly support knowledge binding — that's, you are doing not bind the calendar as a full to an information supply. Instead, you write code to induce the info you would like, and so within the DayRender event, you'll be able to compare the presently rendered date against the info you've got scan from an information supply.
To show info knowledge within the Calendar management
Use ADO.NET varieties to attach to a info and question for the dates to show.
within the Calendar control's DayRender event, compare the date presently being rendered against the info you've got retrieved from the info. If there's a match, customize the day show.
To specify that a personal day is hand-picked
during a technique for the Calendar control's Day Render event, verify what day is being rendered by obtaining info from the Date property of the Day object.
Set that day's IsSelectable property to true.
the subsequent example shows a way to set the date October one, 2005, as selectable; all different dates square measure created not selectable.
protected DataSet dsHolidays;
protected void Page_Load(object sender, EventArgs e)
}
protected void Holidaydate()
protected DateTime GetFirstDayOfNextMonth()
else
DateTime lastDate = new DateTime(Yearno, MonthNo, 1);
come lastDate;
}
protected DataSet GetCurrentMonthData(DateTime firstDate,
DateTime lastDate)
DataSet dsMonth = new DataSet();
ConnectionStringSettings cs;
{cs = ConfigurationManager.ConnectionStrings["ConnectionString1"];
String connString = cs.ConnectionString;
SqlConnection dbConnection = new SqlConnection(connString);
String query;
query = "SELECT HolidayDate FROM Holidays " + 9
" 9 HolidayDate >= @firstDate AND HolidayDate < @lastDate";
SqlCommand dbCommand = new SqlCommand(query, dbConnection);
dbCommand.Parameters.Add(new SqlParameter("@firstDate",
firstDate));
dbCommand.Parameters.Add(new SqlParameter("@lastDate", lastDate));
SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(dbCommand);
try
catch
return dsMonth;
}
protected void Calendar1_DayRender(object sender, DayRenderEventArgs e)
DateTime nextDate;
if(dsHolidays != null)
{
foreach(DataRow dr in dsHolidays.Tables[0].Rows)
{
nextDate = (DateTime) dr[
}
}
}
protected void Calendar1_VisibleMonthChanged(object sender,
MonthlyeventArgs e)
{
Holidaydate();
}