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

Add script on Page load c#

本文关键字:添加 脚本 加载      更新时间:2023-09-26

>我正在尝试向页面添加脚本,但没有任何反应,日历没有出现。目标是呈现一个带有事件列表的日历,我试图避免使用网络服务来获取列表。

  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);

其中事件列表是这样的

   { 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' }

两件事。首先,当您使用脚本管理器注册脚本时,它应该只是 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 文档就绪与页面加载的问题可能与您尝试执行的操作有关。