javascript - eventRender 未被触发 Fullcalendar ASP.net C# webforms

标签 javascript c# asp.net json fullcalendar

我在全日历上显示事件时遇到问题。我正在使用 ASP.net Webforms 和 C#。我有 ashx 处理程序,我调用它来从数据库加载值,然后将其格式化为发回的 json 响应。下面是用于生成日历的 JavaScript:

$(document).ready(function () {

// update Dialog


// page is now ready, initialize the calendar...

var date = new Date();
var d = date.getDate();
var m = date.getMonth();
var y = date.getFullYear();

var calendar = $('#calendar').fullCalendar({
    // put your options and callbacks here
    header:
    {
        left: 'title',
        center: '',
        right: 'month,agendaDay,agendaWeek, prev,next'
    },
    height: 600,
    //contentHeight: auto,
    titleFormat: 'MMMM D YYYY',
    columnFormat: 'ddd D/M',
    defaultView: 'agendaWeek',
    handleWindowResize: true,
    allDaySlot: true,
    minTime: '09:00:00',
    maxTime: '18:00:00',
    slotLabelFormat: 'h(:mm)a',
    slotLabelInterval: '01:00:00',
    firstDay: 1,
    weekends: false,
    hiddenDays: [6, 7],
    selectHelper: true,
    select: selectDate,
    editable: true,
    eventDrop: eventDropped,
    eventResize: eventResized,
    events: {
        url: 'JsonResponse.ashx',
        color: 'yellow',
        error: function () {
            alert('Error while Getting events!');
        }
    },

    eventRender: function (event, element) {
        console.log("here");
        //alert(event.title);
        element.qtip({
            content: event.description,
            style: {
                border: {
                    width: 1,
                    radius: 3,
                    color: '#2779AA'

                },
                padding: 10,
                textAlign: 'center',
                tip: true, // Give it a speech bubble tip with automatic corner detection
                name: 'cream' // Style it according to the preset 'cream' style
            },
            error: function () {
                alert('Error while Getting events!');
            },
            success: function () {
                alert('Success!');
            }

        });
    }


});

});

这是我在浏览器网络跟踪中得到的 JSON 响应:

"{"id":8,"title":"测试者","start":"2018-01-30","end":"2018-01-30","allday":"false","描述":"测试人员"}"

用于生成 JSON 的 ASHX 文件:

{


public void ProcessRequest(HttpContext context)
{
    context.Response.ContentType = "application/json";

    DateTime start = new DateTime(1970, 1, 1);
    DateTime end = new DateTime(1970, 1, 1);

    start = Convert.ToDateTime(context.Request.QueryString["start"]);
    end = Convert.ToDateTime(context.Request.QueryString["end"]);


    String result = String.Empty;

    result += "\"[";
    eventSerializer newEvent = new eventSerializer();
    List<int> idList = new List<int>();
    foreach (CalendarEvent cevent in EventDAO.getEvents(start, end))
    {

        String allDay = "true";
        if (ConvertToTimestamp(cevent.start).ToString().Equals(ConvertToTimestamp(cevent.end).ToString()))
        {

            if (cevent.start.Hour == 0 && cevent.start.Minute == 0 && cevent.start.Second == 0)
            {
                allDay = "true";
            }
            else
            {
                allDay = "false";
            }
        }
        else
        {
            if (cevent.start.Hour == 0 && cevent.start.Minute == 0 && cevent.start.Second == 0
                && cevent.end.Hour == 0 && cevent.end.Minute == 0 && cevent.end.Second == 0)
            {
                allDay = "true";
            }
            else
            {
                allDay = "false";
            }
        }

        newEvent.id = cevent.id;
        newEvent.title = cevent.title;
        newEvent.start = cevent.start.ToString("yyyy-MM-dd");;
        newEvent.end = cevent.end.ToString("yyyy-MM-dd");;
        newEvent.allday = allDay;
        newEvent.description = cevent.description;

    }

    JavaScriptSerializer js = new JavaScriptSerializer();
    string jsonData = js.Serialize(newEvent);
    context.Response.Write(jsonData);
}

public bool IsReusable
{
    get
    {
        return false;
    }
}

private long ConvertToTimestamp(DateTime value)
{


    long epoch = (value.ToUniversalTime().Ticks - 621355968000000000) / 10000000;
    return epoch;

}

}

public class eventSerializer
{

public int id;
public string title;
public string start;
public string end;
public string allday;
public string description;
}

我尝试将 console.log 添加到 eventRender 函数中,但它似乎没有被触发。这些事件也没有添加到日历中。我没有收到任何错误。

任何关于我出错的地方的建议将不胜感激!

最佳答案

感谢@ADyson,问题似乎与 JSON 未以正确的格式返回有关。将 JSON 添加到列表并序列化列表后,事件将显示在日历上。请参阅下文了解更新后的 ASHX。

public class JsonResponse : IHttpHandler, IRequiresSessionState
{

public void ProcessRequest(HttpContext context)
{
    context.Response.ContentType = "application/json";

    DateTime start = new DateTime(1970, 1, 1);
    DateTime end = new DateTime(1970, 1, 1);

    start = Convert.ToDateTime(context.Request.QueryString["start"]);
    end = Convert.ToDateTime(context.Request.QueryString["end"]);

    List<int> idList = new List<int>();
    List<object> eventList = new List<object>();

    foreach (CalendarEvent cevent in EventDAO.getEvents(start, end))
    {
        eventSerializer newEvent = new eventSerializer();

        bool allDay = true;
        if (ConvertToTimestamp(cevent.start).ToString().Equals(ConvertToTimestamp(cevent.end).ToString()))
        {

            if (cevent.start.Hour == 0 && cevent.start.Minute == 0 && cevent.start.Second == 0)
            {
                allDay = true;
            }
            else
            {
                allDay = false;
            }
        }
        else
        {
            if (cevent.start.Hour == 0 && cevent.start.Minute == 0 && cevent.start.Second == 0
                && cevent.end.Hour == 0 && cevent.end.Minute == 0 && cevent.end.Second == 0)
            {
                allDay = true;
            }
            else
            {
                allDay = false;
            }
        }

        idList.Add(cevent.id);

        newEvent.id = cevent.id;
        newEvent.title = cevent.title;
        newEvent.start = cevent.start.ToString("yyyy-MM-dd HH:mm");
        newEvent.end = cevent.end.ToString("yyyy-MM-dd HH:mm");
        newEvent.allDay = allDay;
        newEvent.description = cevent.description;

        eventList.Add(newEvent);
    }

    //store list of event ids in Session, so that it can be accessed in web methods
    context.Session["idList"] = idList;

    JavaScriptSerializer js = new JavaScriptSerializer();
    string jsonData = js.Serialize(eventList);
    context.Response.Write(jsonData);
}

public bool IsReusable
{
    get
    {
        return false;
    }
}

private long ConvertToTimestamp(DateTime value)
{
    long epoch = (value.ToUniversalTime().Ticks - 621355968000000000) / 10000000;
    return epoch;
}

}

public class eventSerializer
{
public int id;
public string title;
public string start;
public string end;
public bool allDay;
public string description;
}

关于javascript - eventRender 未被触发 Fullcalendar ASP.net C# webforms,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48528865/

相关文章:

javascript - 如何在 Angularjs 中使用 document.getElementById() 方法

c# - 'Object.ReferenceEquals' 始终为 false,因为它是使用值类型调用的

javascript - 客户端数据到服务器端

c# - 如何判断用户是否使用绑定(bind)源修改了数据?

c# - 动态创建控件并跨回发保存控件值 - ASP.Net C#

asp.net - 母版页是否知道正在显示哪个页面?

javascript - 迁移到 React 16.0.0 错误 : Cannot find module "react/lib/ReactComponentTreeHook"

javascript - 在 Popup 上,通过 ajax 读取数据两次 : php mysql

javascript - 遍历到selectMenu并获取id(Javascript\JQuery)

c# - 如何以编程方式激活 SettingsPane 中的条目?