![]() |
Web Development »
Custom Controls »
General
Intermediate
License: The Apache License, Version 2.0
DayPilot - Building an Outlook-Like Calendar Component for ASP.NETBy Dan LeteckyA good-looking ASP.NET control that shows events visually arranged in a day calendar. Includes design-time support and data binding. |
C#, Javascript, HTML, Windows, .NET, ASP.NET, Visual Studio, Dev
|
|
Advanced Search |
|
|
|
||||||||||||||||
The story is simple and you might have already heard it: I needed a component and because I wasn't able to find a good one I decided to write it. I was thinking like this: It would take me two hours to find it, another two hours to understand it and another two hours to customize it. Six hours. In six hours, I should be able to write a simple component by myself.
My project specification was simple.
DataTable with a specified structure as a data source.
In short, no AJAX at the moment. What I need is to show the events arranged and catch the most important events (clicking the event and clicking the free space).
This didn't take too long. After the first hour I had the draft:
Event" class will keep the basic information about the event, like starting and ending time, title, etc.
Day" class will keep all the events for a certain day. It will be able to load the events from a DataTable. Each day can contain zero or more Blocks.
Block" class will contain events that overlap with one another. Each Block contains one or more Columns.
Column" class will contain all the events inside a Block that can be in a single Column. Let's demonstrate it visually:

The algorithm can be described in the following steps:
Events so that they don't overlap to another day (e.g. if something starts yesterday, let's make it start today at 00:00).
Events' duration to 30 minutes blocks (we would have problems showing a duration of 5 minutes, 5 seconds - it wouldn't be possible to write any text into a rectangle of such a small height).
Blocks:
Block; otherwise create a new Block. Events into Columns inside a Block (do this for each Block):
Columns.
Events into Columns - find the first free Column from the left and place it there. Now, we can calculate the position of each Event because we know the Blocks and Columns:
To show the calendar we need to draw a table with the hour numbers and yellow free cells and then draw another layer on top of the calendar that contains the events. The other "layer" can be created by setting the cascading style:
position: absolute;
top: 20px;
left: 20px;
The position is defined by the "top" and "left" coordinates. These are related to the upper left corner of the document or to the upper left corner of the containing element that has the position defined (if any).
The biggest problem while calculating the positions was a major difference between Firefox and Internet Explorer:
To achieve the same behavior in IE and FF, I had to nest several DIV elements to create borders and padding. The resulting HTML of an event looks like this:
<div onselectstart="return false;"
onclick="event.cancelBubble=true;alert('Event ID: 4');"
onmouseover="this.firstChild.style.backgroundColor='#DCDCDC';
event.cancelBubble=true;"
onmouseout="this.firstChild.style.backgroundColor='white';
event.cancelBubble=true;"
style="-moz-user-select:none;-khtml-user-select:none;
user-select:none;cursor:pointer;cursor:hand;
position:absolute;font-family:Tahoma;font-size:8pt;
white-space:no-wrap;left:0%;top:1;width:100%;height:40;
background-color:gray;">
<div title="Breakfest (8:00 AM - 9:00 AM)"
style="margin-top:1px;display:block;height:38;
background-color:white;border-left:1px solid gray;
border-right:1px solid gray;overflow:hidden;">
<div style="float:left;width:5px;height:100%;
background-color:blue;">
</div>
<div style="float:left;width:2px;height:100%;"></div>
<div style="padding:2px;">Breakfest (8:00 AM - 9:00 AM)
</div>
</div>
</div>
It is a known problem. The symptoms were that the HtmlWriter class that is used to render the HTML of the component automatically translates the DIV elements to TABLE elements and the STYLE attribute contents to (sometimes) the corresponding HTML attributes. This results in different appearance in Internet Explorer and in other browsers.
There are two possible solutions:
HtmlWriter.Write(). The first option was not possible because of the additional configuration required so I had to create several helper classes and write it directly as whole strings.
Finally, it took me a few days instead of a few hours but now you can share the result with me:
Please visit the DayPilot site for the updates.
General
News
Question
Answer
Joke
Rant
Admin
|
PermaLink |
Privacy |
Terms of Use
Last Updated: 21 Mar 2006 Editor: Rinish Biju |
Copyright 2005 by Dan Letecky Everything else Copyright © CodeProject, 1999-2009 Web11 | Advertise on the Code Project |