Currently working on the jquery fullcalendar, I've been unable to add events to it from code behind.
I've tried all of these possible solutions but none of them worked for me:
- Fullcalendar/Fetching JSON feed(Edited)
- fullCalendar events not showing even though correct JSON feed
- JQuery FullCalendar not displaying events from aspx page
This is my code
[WebMethod]
public static String TestOnWebService()
{
int i = 1;
string myJsonString = "";
List<object> myList = new List<object>();
dt = db.getPublicHolidays();
foreach (DataRow dr in dt.Rows)
{
var id = i;
var title = dr["name"].ToString();
var start = dr["startDate"].ToString();
var end = dr["endDate"].ToString();
//Convert Implicity typed var to Date Time
DateTime RealStartDate = Convert.ToDateTime(start);
DateTime RealEndDate = Convert.ToDateTime(end);
//Convert Date Time to ISO
String SendStartDate = RealStartDate.ToString("s");
String SendEndDate = RealEndDate.ToString("s");
Events t_table = new Events(id, title, start, end);
myList.Add(t_table);
i++;
}
myJsonString = (new JavaScriptSerializer()).Serialize(myList);
return myJsonString;
}
}
and my events class
public class Events
{
public int id { get; set; }
public String title { get; set; }
public String start { get; set; }
public String end { get; set; }
public Events(int id2, String I, String t, String ds)
{
this.id = id2;
this.title = I;
this.start = t;
this.end = ds;
}
}
Here is the front end code
events: function (start, end, callback) {
$.ajax({
type: "POST", //WebMethods will not allow GET
url: '<%= ResolveUrl("EmpHolidays.aspx/TestOnWebService") %>', //url of a webmethod - example below
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (doc) {
var events = []; //javascript event object created here
var obj = $.parseJSON(doc.d); //.net returns json wrapped in "d"
for(var i=0;i<obj.length;i++){
console.log(obj[i]['id']);
console.log(obj[i]['title']);
console.log(obj[i]['start']);
console.log(obj[i]['end']);
}
$(obj.event).each(function () { //yours is obj.calevent
events.push({
title: $(this).attr('title'), //your calevent object has identical parameters 'title', 'start', ect, so this will work
start: $(this).attr('start'), // will be parsed into DateTime object
end: $(this).attr('end'),
id: $(this).attr('id')
});
});
callback(events);
}
});
}
This is the result I get
See Question&Answers more detail:os