July 29, 2009

In this example, I will create a calendar of events using the asp.net Calendar control and a DataTable object. The event name will appear below the date of the calendar control.

Here is the main page.


In the Page_Load method,



Before the Page_Load method, I am declaring a DataTable object, dt. In the Page_Load method, I am calling the GetSchedule() method. Here, is the code for the GetSchedule() method.


The GetSchedule() method creates the schema of the DataTable - adding 2 columns date and event as string. Then, 5 rows of data are added to the DataTable. Note that date strings are added as would be returned by DataTime object's ToShortDateString() method.

The OnDayRender event is used to add the events to the Calendar control. Here, is the code of the OnDayRender event.



In this method, I am first adding a Literal control and applying a break to the Text property and adding it to the cells displaying the date. Then, I am checking if the cell's date exists in the DataTable or not and add the event name if it exist.

When the page is run, the following output is displayed.


2 comments:

Sruthish said...

Hey man this is cool It works fine, nice job. i had a event scheduler which had a page full of code. cool......

Anonymous said...

Hi this is nice but when i click on any date it clears all events even i press F5. Pls give the solution to this issue

Reference: Shahed Kazi at AspNetify.com