Thursday, March 22, 2012

Full calendar - saving event using ajax / pagemethod/ webmethod in

In this post we will be saving the event in a full calendar plugin using jQuery ajax and pagemethod or web service. To go through the post I request you to first check the following post. We are going the proceed forward with the same post and its source code.

In addition to the previous script and CSS file references, we have added one more script file for this purpose-
<script src="" type="text/javascript"></script>
This file is used hare to to JSON stringify the event object.

Now how we are going to solve this issue is that on click of a day, we are going popup an entry form for the event and we will use ajax to save the event to the server and after successful saving we will be adding the event to the calender. Now calender plugin has some event to solve this purpose-
  1. dayClick - this event happen during the mouse click of a day.
  2. renderEvent - using this event we can add an event to the calender. We can set stick to true to permanently add the event to the calender.
For popup we are going to use jQuery dialog. For adding the event we can add the following HTML-
<div id="eventToAdd" style="display: none; font-size: 12px;">
    Event name:
    <input id="eventName" type="text" /><br />
    Event start date:
    <input id="eventStartDate" type="text" />(MM-dd-yyyy)<br />
    Event end date:
    <input id="eventEndDate" type="text" />(MM-dd-yyyy)<br />
Now we need to show popup dialog for adding event on the day click of the calendar day. To do so we will take the help of the dayClick event of the calender and it goes here-
<script type="text/javascript">
        dayClick: function (dateSelected, allDay, jsEvent, view) {
                title: "Add event",
                modal: true,
                buttons: {
                    "Add": function () {
                       //event adding logic goes here                 
Now on click of add button in the dialog we need to do two things. First save the event data using jQuery ajax and page method / web service. And secondly on the success of the ajax call add the event on the calender. Both the work is done here-
"Add": function () {
    var event = new Object(), eventToSave = new Object(); ;
    eventToSave.EventID = = Math.floor(200 * Math.random());
    event.start = new Date($("#eventToAdd #eventStartDate").val());
    eventToSave.StartDate = $("#eventToAdd #eventStartDate").val();
    if ($("#eventToAdd #eventEndDate").val() == "") {
        event.end = event.start;
        eventToSave.EndDate = eventToSave.StartDate;
    else {
        event.end = new Date($("#eventToAdd #eventEndDate").val());
        eventToSave.EndDate = $("#eventToAdd #eventEndDate").val();
    eventToSave.EventName = event.title = $("#eventToAdd #eventName").val();

    $("#eventToAdd input").val("");
        type: "POST",
        contentType: "application/json",
        data: "{eventdata:" + JSON.stringify(eventToSave) + "}",
        url: "FullcalenderwithPagemethod.aspx/AddEvents",
        dataType: "json",
        success: function (data) {
            $('div[id*=fullcal]').fullCalendar('renderEvent', event, true);
        error: function (XMLHttpRequest, textStatus, errorThrown) {
What we are doing here is in line 2 we are creating two JavaScript objects event and eventToSave and line 3-14 we are assigning the values from the popup. And finally we are saving the data to server using ajax call. Why we have taken two objects here is event data of the calender has different property name and the server event have different name. Once the saving of event is successful we are adding the event object to the calendar using line number 24. The page method using for this is as follows-
public static bool AddEvents(Event eventdata)
    List events;
    if (HttpContext.Current.Session["events"] != null)
        events = (List)HttpContext.Current.Session["events"];
        events = new List();
    HttpContext.Current.Session["events"] = events;
    return true;
For this example we have used in memory event object. We can easily replace it with proper database driven. The complete modified code for this is available here for download.


  1. Hey Anup
    Great Post on fullcalendar
    I was wondering how to retrieve the value of json object in php?

    1. Hi,
      I don't have any knowledge on PHP. But I think you can look into the following posts-

  2. Quick question, do you know how to save the events in bulk via Json rather than one event at a time?.

    1. Yes you can do this. You have to decide how the entry from should be for bulk of event. And saving events is nothing but passing a List type data to the pagemethod. you can check this post-

  3. Hello Anup
    I am new to Json & Jquery, so can you explain me how to display the time from morning 7 AM to 10 PM ?

  4. Hi,
    First of all enable day as default selection. Then get data to fill the event. While dragging an dropping you can compare your time range. It its exceeding the range do not allow to drop the event. Please refer the documentation.

  5. Hi Anup,
    Thank you for such a valuable post but i need to know i want to create a time table,how could i do that?

    1. Its a very general query. You can take help from this post and use ajax for creating timetable.