4. Integration with System.Web.Ui.WebControls.Calendar

For switching the date you can use the standard .NET Framework control System.Web.UI.WebControls.Calendar. You can use the PostBack event to change the DayPilot StartDate and Days properties.

calendar.gif

In our sample we will use the DayRender event to improve the calendar:

  • the days will become links to a specific day (i.e. no PostBack)
  • the days that contain an event will be bold
private void Calendar1_DayRender(object sender, System.Web.UI.WebControls.DayRenderEventArgs e)
{
	string fontWeight = "normal";
	if (isThereEvent(e.Day.Date))
		fontWeight = "bold";

	string color = "black";
	if (e.Day.IsOtherMonth)
		color = this.Calendar1.OtherMonthDayStyle.ForeColor.Name;

	e.Cell.Text = String.Format("<a href='Default.aspx?day={0:d}' style='color: "
		+ color + ";text-decoration:none; font-weight:" 
		+ fontWeight + "'>{1}</a>", e.Day.Date, e.Day.Date.Day);		
}

The method isThereEvent() returns true if a specific day contains any event. This method will be specific to your application. You can go through the data returned from the database (and supplied to DayPilotCalendar.DataSource) to avoid another database request.  We are not using the database in our sample so it is hard-coded:

private bool isThereEvent(DateTime date)
{
	DateTime today = DateTime.Now;
	DateTime tomorrow = today.AddDays(1);
	DateTime anotherDay = today.AddDays(3);

	// there are events today
	if ((date.DayOfYear == today.DayOfYear) && (date.Year == today.Year))
		return true;

	// there are events tomorrow
	if ((date.DayOfYear == tomorrow.DayOfYear) && (date.Year == tomorrow.Year))
		return true;

	// there are events on another day
	if ((date.DayOfYear == anotherDay.DayOfYear) && (date.Year == anotherDay.Year))
		return true;

	return false;
}