Click here to Skip to main content
Click here to Skip to main content

Event Calendar [ ASP.NET 2.0 / C# ]

By , 18 May 2008
 

Introduction

Tracking anything in dates is one of the common requirement now a days. Tracking events, project milestones, tracking history, tracking schedule etc etc. It was quite a complex job until the wonderful Calendar Control of ASP.NET 2.0. It is one of the Standard Server Side Web Control of ASP.NET 2.0, which when used properly can help in accomplishing the various requirements, tracking being one of them. Here we are going to see how can this Calendar control can help us in tracking the events.

Event Calendar 

One of best ways in which events can be displayed is by displaying the events by month. What it basically means is - display the complete month and display the events against each date. Here is how our Event Calendar is going to look like:

Screenshot - CalendarWithEventInColor.jpg

How can this be achieved  

It is quite simple and straight forward. ASP.NET Calendar control posses an event "DayRender" which has got "DayRenderEventArgs" as one of the parameters. This DayRenderEventArgs has got two very useful things - Day and Cell. Day is of the type "CalendarDay" and Cell is of the type "TableCell". Day can help us in identifying the day, date etc which it is going to render and Cell gives us handle to the table cell corresponding to that day. Now since we have handle to Table Cell of displaying Date, we can play with it to cater to our needs. For event Calendar we are simply going to use this event to add the event details in the Table Cells associated with the dates.

        CalendarDay d = ((DayRenderEventArgs)e).Day; 
        TableCell c = ((DayRenderEventArgs)e).Cell;    
    
        if ( !d.IsOtherMonth )    
            c.Controls.Add(new LiteralControl(dr["EventDetails"].ToString()));
     

And thats it, we are done.

Event Calendar Control

If we are aware that we are going to use the same functionality again and again, it is better to make it a re-usable component. So here, we are going to extend the capabilities of basic ASP.NET 2.0 Calendar control. The basic requirement of data driven applications is that source of information basically is data store. It can be database, XML File, Excel or anything for that matter. Here we are going to limit our scope and going to implement the EventCalendar with ADO.NET DataTable as the source of data. Let us see the ingredients of this calendar control

  1. A class which extends System.Web.UI.WebControls.Calendar
  2. The necessary properties:

    • EventSource: DataTable with Event Details
    • EventStartDateColumnName: ColumnName of the Column of the type DateTime in the EventSource, which stores the Start Date associated with Events
    • EventEndDateColumnName: ColumnName of the Column of the type DateTime in the EventSource, which stores the End Date associated with Events
    • EventHeaderColumnName: ColumnName of the Column of the type String in the EventSource, which stores the Event Header,
    • EventDescriptionColumnName: ColumnName of the Column of the type String in the EventSource, which stores the Event Detailed Description,
    • ShowDescriptionAsToolTip: Boolean to determine whether to display Event Description as Tool Tip or not.
    • EventForeColor: ColumnName of the Column to specify the Fore ( Font ) Color for the event. We can specify any Color Name which belongs to System.Drawing.Color namespace.
    • EventBackColor: ColumnName of the Column to specify the Back Color for the event.We can specify any Color Name which belongs to System.Drawing.Color namespace.
  3. EventCalendarDayRender Event: Place where the actual logic to show the events in Calendar is implemented

This describes the skeleton for your EventCalendar Class. Refer to the EventCalendar.cs class file in the attached demo for complete implementation. By extending the control in such way we can keep the basic features provided along with the new features/capabilities that we need to cater to our requirements.

Version History

Version 3 

Support for Fore and Back Color for Events has been added. Click here to download its demo

Version 2

Support for Events which spills over more that one days has been added. Click here to download its demo

Version 1

Basic Event Calendar with support for Events which occurs on a single date Click here to download its demo

Wrapping Up

ASP.NET Controls are undergoing development and continuous evolution with each release, ASP.NET 2.0 Controls are no exception. These controls not only getting smarter and smarter, but also giving a developer better handle to its basic buidling blocks like TableCell of Calendar Control.

Please spare some time to rate and provide feedback about this article. Your couple of minutes can help in enhancing the quality of this article.

If interested, Click here to view all my published articles.

License

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

About the Author

Neeraj Saluja
Web Developer
United States United States
Member
.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 ^ ]

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
Questionconsulta necesito controlar el tamaño del control eventcalendarmemberdiana ysabel3 Jan '13 - 10:25 
Buen dia una consulta como puedo controlar el tamaño del control event calendar ya que cuando tiene varios eventos se expande demasiado, otra cosa hay forma de que el dia del calendrio se vea mas grande que el texto del evento, gracias por su respuesta de antemano.
QuestionNeed to display single event in multiple cellsmemberMember 806441714 Nov '12 - 16:51 
how to display single event in multiple cells? Eg. The sample is show same event in 3 days from 18th to 20th, any idea to show the event stretch from 18th to 20th instead of showing events in 3 days?
QuestionNeed to display single event in multiple cellsmemberMember 47683617 Nov '12 - 4:17 
Instead of showing events in 3 days can I show the events that stretch from 18th to 20th like outlook
GeneralEvents on the same day [modified]memberMember 23788889 Aug '12 - 2:28 
Hello
 
Very good code. Who can be solved if I have more than one event the same day?
 
regards

modified 9 Aug '12 - 8:50.

GeneralSee also DayPilot (Day/Week/Resources View)memberDan Letecky19 Jun '12 - 10:36 
If you need to show calendar events in a day/week/resources view you may want to check DayPilot:
 
Day/Week (Calendar control):
DayPilot - Building an Outlook-Like Calendar Component for ASP.NET [CodeProject]
 
Multiple Resources (Scheduler control):
DayPilot Scheduler Control for ASP.NET [CodeProject]
 
Nice article!
--
My open-source ASP.NET 2.0 controls:
DayPilot - Outlook-like calendar/scheduling control
DayPilot MonthPicker - Light-weight month picker
MenuPilot - Hover context menu

QuestionAdding and Editing EventsmemberKeith Plummer7 Jun '12 - 5:54 
How do I enter events into the calendar? Is that something I need to write or am I missing something? When I click on the date number the event shows up, but I don't know how to edit the event or add a new one.
 
It looks great at first glance and should be exactly what I was looking for.
 
Thanks.
 
Keith
GeneralMy vote of 5memberNilesh Patil Kolhapur24 May '12 - 20:13 
Excellent
QuestionColor on event datesmemberMik Jensen13 May '12 - 0:37 
Is it possible to have all the event dates to look the same as the selected date? I want same background color on whole day instead of only background color on the text.
QuestionShow any monthmembersai21808 May '12 - 3:30 
HI,
 
Its very useful control.
 
I have small query.
 
Always this calender control by default shows current month. Is it possible to show the month which I will sent as a parameter. Because in my scenario, dynamically I have to send the month and year. So that it will load the particular month of that year.
 
Please help me to solve this issue.
 
Thanks
Sai
AnswerRe: Show any monthmembersai218011 May '12 - 1:57 
Hi,
 
Above problem is solved.
 
I have only one issue left. Can we show year drop-down along with this control, for speed navigation.
 
Thanks
Sai

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web02 | 2.6.130523.1 | Last Updated 19 May 2008
Article Copyright 2006 by Neeraj Saluja
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid