如何从JavaScript获取CS文件方法的价值?

时间:2017-01-05 12:04:32

标签: javascript c# asp.net

我有UserControl。这是日历。现在我想在JavaScript中调用CS文件的函数,它将返回值。

这是我的嘲笑:

//cs method
public string getCalendarData()
{
    return "[{\"id\":\"35424af8-4fd2-e611-8104-c4346bac5238\",\"title\":\"NanjitestCall1\",\"start\":\"2017-01-12 00:00:00\",\"allDay\":false,\"end\":\"2017-01-12 07:29:00\",\"backgroundColor\":\"#8224e3\",\"url\":\"?detail-phonecall&id=35424af8-4fd2-e611-8104-c4346bac5238\"}]
}

//ascx page
<script type='text/javascript'>
    //loading start
    var el2 = jQuery("#calendar");
    App.blockUI(el2);
    jQuery('#calendar').fullCalendar('destroy'); // destroy the calendar
    jQuery('#calendar').fullCalendar({//re-initialize the calendar
        disableDragging: true,
        disableResizing: true,
        defaultView: 'month',
        slotMinutes: 15,
        minTime: 8,
        header: h,
        editable: true,
        lazyFetching: true,
        timeFormat: 'h(:mm) t - ', // uppercase H for 24-hour clock,lowercase h for 12-hour clock
        viewDisplay: function (view) {
            //App.blockUI(el2);
        },
        events: //call function in .cs file and return value

        //function call in PHP
        //events: {
        //    url: '<?php echo plugin_dir_url(__FILE__); ?>' + "json_events.php?scp_sugar_object=<?php echo get_option('dynamic_conn_object'); ?>&scp_user_id=<?php echo $_SESSION['scp_user_id']; ?>",
        //    success: function (data) {
        //        App.unblockUI(el2);   //loading stop
        //    }
        //}
});
</script>

任何人都可以建议我参考或提示这项工作吗?

1 个答案:

答案 0 :(得分:1)

您可以使用jQuery AJAX来调用获取数据值的C#代码。

要从用户控件获取日期的C#代码:

[WebMethod]
public static string GetDateValue()
{
    //string date = [logic to fetch date from user control]
    return date;
}

jQuery AJAX代码从C#获取日期值:

$.ajax({
    type: "GET",
    url: "<your aspx page>.aspx/GetDateValue",
    error: function (XMLHttpRequest, textStatus, errorThrown) {
        alert("Request: " + XMLHttpRequest.toString() + "\n\nStatus: " + textStatus + "\n\nError: " + errorThrown);
    },
    success: function (result) {
        alert("Date value: " + result);
    }
});