Talking about new features, useful things, frustrations in .NET and so on...
22-08-2007
MyCalendar - add events without postbacks
Most of the time a calendar is used to simply select a date, in some cases it will be stretched to give an overview of a schedule, getting the same look and feel as MS Outlook.
Now, what if you want to be able to add an event to a date ?
First drop a calendar control on your web user control or web page, be sure to stretch it in a similar way as below. Add a div containing one or more fields to insert info of the new events.
E.g.
Next step is to add 3 javascript functions : HideTaskPane ShowAddTaskPane ReceiveServerData
HideTaskPane will simply hide the div.
ShowAddTaskPane accepts 2 parameters, eventargs and the selected date. The function will display the task pane (the div) and call the server (by using a callback eventhandler, see later) to store the selecteddate, e.g. in the viewstate.
ReceiveServerData is called after the callback eventhandler, this can be used to display the data received from GetCallbackResult() (again, see later).
Now, moving to the C# (or VB.NET) code :
Dont forget to inherit from ICallBackEventHandler (in the System.Web.UI namespace).
In the Page_Load event, register the callback script if its not yet registered. The server-side RaiseCallBackEvent method is called from the client-side ShowAddTaskPane function, you can add the selected date to the viewstate and/or perform some other actions here. GetCallBackResult can be used for testing purpose (e.g. return the selected date from the viewstate) or to return data from the server to the client.
Last step is to add code in the DayRender event of the calendar. This is the place in which you can add controls to a day. This sample code will add events from a list of AgendaItems. Literals can be used to insert crlfs, labels to add existing events. An HtmlAnchor is used as a link to display the task pane, calling ShowAddTaskPane on click.