Full Calendar with JSON data source using asp.net web service/ pagemethod/ webmethod

Full Calendar with JSON data source using asp.net web service/ pagemethod/ webmethod

In this blog post we are going to discuss using full calendar plugin with JSON data source through asp.net webservice / pagemethod / webmethod. We can get the detail of the plugin here. 

For using this plugin we need to add the reference of the following files- 
1
2
3
4
5
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js" type="text/javascript"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.6/jquery-ui.min.js" type="text/javascript"></script>
<link rel='stylesheet' type='text/css' href='http://arshaw.com/js/fullcalendar-1.5.2/fullcalendar/fullcalendar.css' />
Basic style HTML infrastructure using for this implementation is as follows- 
        #loading
        {
            width: 600px;
            height: 550px;
            position: absolute;
            background-color: gray;
            color: white;
            text-align: center;
            vertical-align: middle;
            display: table-cell;
        }
        #fullcal
        {
            width: 600px;
            height: 600px;
            position: absolute;
            display: none;
        }
1
2
3
4
5
6
7
8
<div>
    <div id="loading">
        <label style="top: 50%; position: relative">
            loading events....</label>
    </div>
    <div id="fullcal">
    </div>
</div>
In the div with id fullcal we will be loading the calendar control. By default this div is marked as hidden, and a place holder div (with id loading) is added in the place. This is just to show while server data is getting loaded from server. Once the data is loaded on the calendar we will show back the calendar and hide the placeholder. 

The script used for activating calendar is as follows- 
1
2
3
4
5
6
7
8
9
$('div[id*=fullcal]').fullCalendar({
    header: {
        left: 'prev,next today',
        center: 'title',
        right: 'month,agendaWeek,agendaDay'
    },
    editable: true,
    events: <b>list of event here</b>
});
Now in full calendar the event object has lots of information. We can get more information about the event object here. Lets represent the event as a C# class as follows- 
1
2
3
4
5
6
7
public class Event
{
    public int EventID { get; set; }
    public string EventName { get; set; }
    public string StartDate { get; set; }
    public string EndDate { get; set; }
}
There are many other properties for the event object. For the sack of implementation we are taking these only. And I think the class properties are self explanatory. We can get the data for the event from database. For this post we are creating in memory object for data source. The page method used for data retrieval is as follows- 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
   [WebMethod]
    public List<event> GetEvents()
    {
        List<event> events = new List<event>();
        events.Add(new Event()
        {
            EventID = 1,
            EventName = "EventName 1",
            StartDate = DateTime.Now.ToString("MM-dd-yyyy"),
            EndDate = DateTime.Now.AddDays(2).ToString("MM-dd-yyyy")
        });
        events.Add(new Event()
        {
            EventID = 2,
            EventName = "EventName 2",
            StartDate = DateTime.Now.AddDays(4).ToString("MM-dd-yyyy"),
            EndDate = DateTime.Now.AddDays(5).ToString("MM-dd-yyyy")
        });
        events.Add(new Event()
        {
            EventID = 3,
            EventName = "EventName 3",
            StartDate = DateTime.Now.AddDays(10).ToString("MM-dd-yyyy"),
            EndDate = DateTime.Now.AddDays(11).ToString("MM-dd-yyyy")
        });
        events.Add(new Event()
        {
            EventID = 4,
            EventName = "EventName 4",
            StartDate = DateTime.Now.AddDays(22).ToString("MM-dd-yyyy"),
            EndDate = DateTime.Now.AddDays(25).ToString("MM-dd-yyyy")
        });
        return events;
    }
</event></event></event>
Now we can read the web service data using jQuery and fill the full calendar using server driven event list like following- 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
$.ajax({
    type: "POST",
    contentType: "application/json",
    data: "{}",
    url: "FullcalenderwithWebservice.asmx/GetEvents",
    dataType: "json",
    success: function(data) {
        $('div[id*=fullcal]').fullCalendar({
            header: {
                left: 'prev,next today',
                center: 'title',
                right: 'month,agendaWeek,agendaDay'
            },
            editable: true,
            events: data.d
 });
        $("div[id=loading]").hide();
        $("div[id=fullcal]").show();
    },
    error: function(XMLHttpRequest, textStatus, errorThrown) {
        debugger;
    }
});
Now here is a slight problem, the calendar will get loaded but not the list of events returned. Few points we need to remember here-
  1.  The full calendar event object has certain naming like EventID should be id, EventName – title, StartDate – start, EndDate – end and so on. So, we have to map the returned object to full calendar’s desired object.
  2. One more thing that we need to remember is that the start and end need to be in date type. The proper conversation is as follows-
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
$('div[id*=fullcal]').fullCalendar({
    header: {
        left: 'prev,next today',
        center: 'title',
        right: 'month,agendaWeek,agendaDay'
    },
    editable: true,
    events: $.map(data.d, function(item, i) {
                var event = new Object();
                event.id = item.EventID;
                event.start = new Date(item.StartDate);
                event.end = new Date(item.EndDate);
                event.title = item.EventName;
                return event;
            })
});
We can note one more thing that we are returning the date in MM-dd-yyyy format (DateTime.Now.ToString("MM-dd-yyyy")). The reason is that we can pass such a string to Date constructor in JavaScript to create the date. 

We can download the full source code from here.

Torna su
Categoria

JQuery - Javascript (473)


Autore

http://growingtech.blogspot.it/


Data pubblicazione.

15/07/2014



Recensioni

Articolo non ancora recensito