Click here to Skip to main content
15,893,588 members
Articles / Web Development / ASP.NET

Event Calendar [ ASP.NET 2.0 / C# ]

Rate me:
Please Sign up or sign in to vote.
4.49/5 (62 votes)
18 May 2008CPOL4 min read 856K   38.7K   257  
Basic Calendar Control of ASP.NET 2.0 can be extended to cater one of most frequent requirement of tracking events, project milestones, history, schedule etc.
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using System.Drawing;

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

    private DataTable GetEvents()
    {
        DataTable dt = new DataTable();
        dt.Columns.Add("Id", Type.GetType("System.Int32"));
        dt.Columns.Add("EventStartDate",Type.GetType("System.DateTime"));
        dt.Columns.Add("EventEndDate", Type.GetType("System.DateTime"));
        dt.Columns.Add("EventHeader",Type.GetType("System.String"));
        dt.Columns.Add("EventDescription",Type.GetType("System.String"));
        dt.Columns.Add("EventForeColor", Type.GetType("System.String"));
        dt.Columns.Add("EventBackColor", Type.GetType("System.String"));

        int idCount = 1;

        DataRow dr;

        // Yesterday's Events
        dr = dt.NewRow();
        dr["Id"] = idCount++;
        dr["EventStartDate"] = DateTime.Now.AddDays(-1);
        dr["EventEndDate"] = DateTime.Now.AddDays(-1);
        dr["EventHeader"] = "My Yesterday's Single Day Event";
        dr["EventDescription"] = "My Yesterday's Single Day Event Details";
        dr["EventForeColor"] = "White";
        dr["EventBackColor"] = "Navy";
        dt.Rows.Add(dr);

        // Three Day's Event Starting Tomorrow
        dr = dt.NewRow();
        dr["Id"] = idCount++;
        dr["EventStartDate"] = DateTime.Now.AddDays(1);
        dr["EventEndDate"] = DateTime.Now.AddDays(+3);
        dr["EventHeader"] = "My Three Days Event";
        dr["EventDescription"] = "My Three Days Event Details, which starts tomorrow";
        dr["EventForeColor"] = "White";
        dr["EventBackColor"] = "Green";
        dt.Rows.Add(dr);

        
        return dt;
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        Calendar1.EventStartDateColumnName = "EventStartDate";
        Calendar1.EventEndDateColumnName = "EventEndDate";
        Calendar1.EventDescriptionColumnName = "EventDescription";
        Calendar1.EventHeaderColumnName = "EventHeader";
        Calendar1.EventBackColorName = "EventBackColor";
        Calendar1.EventForeColorName = "EventForeColor";


        Calendar1.EventSource = GetEvents();
    }
    protected void Calendar1_SelectionChanged(object sender, EventArgs e)
    {
        SelectedDatesCollection theDates = Calendar1.SelectedDates;
        DataTable dtSelectedDateEvents = Calendar1.EventSource.Clone();
        DataRow dr;

        foreach (DataRow drEvent in Calendar1.EventSource.Rows)
            foreach (DateTime selectedDate in theDates)
                if (selectedDate.Date >= (Convert.ToDateTime(drEvent[Calendar1.EventStartDateColumnName])).Date
                    && selectedDate.Date <= (Convert.ToDateTime(drEvent[Calendar1.EventEndDateColumnName])).Date)
                {
                    // This Condition is just to ensure that Every Event Details are added just only once
                    // irrespective of the number of days for which the event occurs.
                    if (dtSelectedDateEvents.Select("Id= " + Convert.ToInt32(drEvent["Id"])).Length > 0)
                        continue;

                    dr = dtSelectedDateEvents.NewRow();                    
                    dr["Id"] = drEvent["Id"];
                    dr[Calendar1.EventStartDateColumnName] = drEvent[Calendar1.EventStartDateColumnName];
                    dr[Calendar1.EventEndDateColumnName] = drEvent[Calendar1.EventEndDateColumnName];
                    dr[Calendar1.EventHeaderColumnName] = drEvent[Calendar1.EventHeaderColumnName];
                    dr[Calendar1.EventDescriptionColumnName] = drEvent[Calendar1.EventDescriptionColumnName];

                    dr[Calendar1.EventForeColorName] = drEvent[Calendar1.EventForeColorName];
                    dr[Calendar1.EventBackColorName] = drEvent[Calendar1.EventBackColorName];

                    dtSelectedDateEvents.Rows.Add(dr);
                }        

        gvSelectedDateEvents.DataSource = dtSelectedDateEvents;
        gvSelectedDateEvents.DataBind();

    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
United States United States
.NET Professional, working with a leading global firm.

Primarily works in .NET using C# with Oracle and MS SQL Server 2000 as backend.

Learning .Net ...

[ My Linked In Profile ^ ]

Comments and Discussions