Create an Event Calendar

Calendar control in .net framework allows you to add a nice calendar to your web site without writing any codes. You only need to just drag and drop calendar control from the tool box.
But many cases you will need add some event’s to specific dates in your calendar. So I’m going to explain a simple way to creating a event calendar.
  • Drag and drop a calendar control from the toolbox to your webpage
  • Create a hash table for adding your specific dates and events. Inside the page load event, add specific dates and events to the hash table.
  • To add the day event’s to your calendar you need to write your codes inside the DayRender method of calendar control.

Refer the following example,


using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{

Hashtable schedule = new Hashtable();
    protected void Page_Load(object sender, EventArgs e)
    {
        GetSchedule();

    }

    private void GetSchedule()
    {
        schedule["7/1/2011"] = "My B'Day";
        schedule["8/8/2011"] = "Mom's B'Day";
        schedule["8/24/2011"] = "Senu's B'Day";
        schedule["4/29/2011"] = "Father's B'Day";
        schedule["4/14/2011"] = "New Year Day";
        schedule["2/4/2011"] = "National Day";
    }
protected void Calendar1_DayRender(object sender, DayRenderEventArgs e)
    {
     
      
      
       
        if (schedule[e.Day.Date.ToShortDateString()] != null)
        {
            Label lbl = new Label();
            lbl.Visible = true;
            lbl.Text = "<br/>"+(string)schedule[e.Day.Date.ToShortDateString()];
            e.Cell.Controls.Add(lbl);
        }
       
      
    }

}