【问题标题】:Add script on Page load c#在页面加载c#上添加脚本
【发布时间】:2015-02-28 07:36:41
【问题描述】:

我正在尝试向页面添加脚本,但没有任何反应,日历没有出现。 目标是呈现带有事件列表的日历,我试图避免使用 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' }

【问题讨论】:

    标签: javascript c# asp.net


    【解决方案1】:

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

    这是一个示例 sn-p:

    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 可能与您正在尝试做的事情有关。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-02-04
      • 1970-01-01
      相关资源
      最近更新 更多