在页面加载c#上添加脚本

时间:2015-01-01 20:10:35

标签: javascript c# asp.net

我正在尝试向页面添加脚本但没有任何反应,日历不会出现。 目标是使用事件列表呈现日历,我试图避免web服务获取列表。

  string CalendarScript = @"<script type=""text/javascript"">
        jQuery(function ($) {

            /* initialize the external events
                -----------------------------------------------------------------*/

            $('#external-events div.external-event').each(function () {

                // create an Event Object (http://arshaw.com/fullcalendar/docs/event_data/Event_Object/)
                // it doesn't need to have a start or end
                var eventObject = {
                    title: $.trim($(this).text()) // use the element's text as the event title
                };

                // store the Event Object in the DOM element so we can get to it later
                $(this).data('eventObject', eventObject);

                // make the event draggable using jQuery UI
                $(this).draggable({
                    zIndex: 999,
                    revert: true,      // will cause the event to go back to its
                    revertDuration: 0  //  original position after the drag
                });

            });

            /* initialize the calendar
            -----------------------------------------------------------------*/

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


            var calendar = $('#calendar').fullCalendar({
                lang: ""pt"",
                allDaySlot: false,
                minTime: ""07:00:00"", 
                maxTime: ""23:59:59"",

                defaultView: 'agendaWeek',

                header: {
                    left: ' ',
                    center: ' ',
                    right: ' '
                },
                events: [ " + EventsList + @"
                ]
    ,

                columnFormat: {

                    week: 'dddd', 

                },

                editable: false,
                droppable: false, // this allows things to be dropped onto the calendar !!!

                selectable: false,
                selectHelper: false


            });


        })
    </script>";
        ScriptManager.RegisterStartupScript(this.Page, Page.GetType(), "InitCalendarEvents", CalendarScript, false);

其中EventList就像这样

   { title: 'Some Event', start: new Date(y, m, d - 2, 16, 00), end: new Date(y, m, d - 2, 17, 00), allDay: false,  color: 'blue !important',  textColor: 'white !important' }

1 个答案:

答案 0 :(得分:1)

两件事。首先,当您使用脚本管理器注册脚本时,它应该只是javascript而不包含在脚本标记中。其次,您很可能希望在add_load事件上发生该函数。

这是一个示例代码段:

this.Page.ClientScript.RegisterStartupScript(
    this.GetType(), 
    "StartupScript", 
    "Sys.Application.add_load(function() { functioncall(); });", 
    true);

请注意,javascript函数的参数是javascript函数,而不是html脚本元素。此外,并非该功能组成Sys.Application.add_load

如果你做这两件事,至少应该执行脚本。问题Sys.application.add_load vs document ready vs pageload可能与您要做的事情有关。