如何在 ASP.NET 中将数据动态追加到 JQuery 控件变量

本文关键字:追加 动态 JQuery 变量 控件 数据 ASP NET | 更新日期: 2023-09-27 18:34:51

我想在 Jquery 日历控件中显示报告,ASP.net 我需要从数据库中检索数据。我写了这样的服务

    [WebMethod]
public static string GetData()
{
    Horticulture h = new Horticulture();
    return h.GetProjectedYield().GetXml();
}

在这里,我将数据集转换为xml,然后在javascript中调用该函数,如下所示

 $(document).ready(function () {
 var response;
     $.ajax({
         type: 'POST',
         dataType: 'json',
         contentType: 'application/json',
         url: 'ProjectedYieldCalender.aspx/GetData',
         data: '{}',
         success:
                function (res) {
                    var xmlDoc = $.parseXML(res.d);                      
                    var xml = $(xmlDoc);
                    var customers = xml.find("Table1");
                }
     });

然后我需要将回收的数据绑定到 JQuery 日历控件,如下所示

$('#calendar').fullCalendar({
         header: {
             left: 'prev,next today',
             center: 'title',
             right: 'month,basicWeek,basicDay'
         },
         //defaultDate: '2014-08-12',
         editable: true,
         eventLimit: true, // allow "more" link when too many events
         events: [
               for(var i=0; i<customers.length; i++)
            {
            "{"
                "title:" +customers[i].Column1+","
                "start:"+customers[i].Column3
                "}"

            if(i!=customers.length-1)
            {
                ","
            }   
            }
    ]
     });
 });
但我

没有得到正确的输出(无法找到(,但我需要生成如下

$(document).ready(function() {
    $('#calendar').fullCalendar({
        defaultDate: '2014-11-12',
        editable: true,
        eventLimit: true, // allow "more" link when too many events
        events: [
            {
                title: 'All Day Event',
                start: '2014-11-01'
            },
            {
                title: 'Long Event',
                start: '2014-11-07',
                end: '2014-11-10'
            },
            {
                id: 999,
                title: 'Repeating Event',
                start: '2014-11-09T16:00:00'
            },
            {
                id: 999,
                title: 'Repeating Event',
                start: '2014-11-16T16:00:00'
            },
            {
                title: 'Conference',
                start: '2014-11-11',
                end: '2014-11-13'
            },
            {
                title: 'Meeting',
                start: '2014-11-12T10:30:00',
                end: '2014-11-12T12:30:00'
            },
            {
                title: 'Lunch',
                start: '2014-11-12T12:00:00'
            },
            {
                title: 'Meeting',
                start: '2014-11-12T14:30:00'
            },
            {
                title: 'Happy Hour',
                start: '2014-11-12T17:30:00'
            }
        ]
    });
});

任何人都可以让我们知道如何做到这一点吗?我哪里出错了?

如何在 ASP.NET 中将数据动态追加到 JQuery 控件变量

让它变得简单 - 不要在 Web 服务中返回 XML,返回事件对象列表。

public class CEvent
{
    public int id {get;set;}
    public string title {get;set;}
    public DateTime start {get;set;}
}
[WebMethod]
public List<CEvent> GetData()
{
    return new List<CEvent>{...};
}
$(document).ready(function () {
 var response;
     $.ajax({
         type: 'POST',
         dataType: 'json',
         contentType: 'application/json',
         url: 'ProjectedYieldCalender.aspx/GetData',
         data: '{}',
         success:
                function (data) {
                    var eventList = data;
                    // OR var eventList = data.d
                    initCalendar(eventList);
                }
     });
function initCalendar(eventList){
    $('#calendar').fullCalendar({
        defaultDate: '2014-11-12',
        editable: true,
        eventLimit: true, // allow "more" link when too many events
        events: eventList
    });
}