在页面加载时添加脚本 c#
Add script on Page load c#
我正在尝试向页面添加脚本,但没有任何反应,日历没有出现。
objective 用于呈现带有事件列表的日历,我试图避免 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 而不是包含在脚本标签中。其次,您很可能希望函数在 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 可能与您正在尝试做的事情有关。
我正在尝试向页面添加脚本,但没有任何反应,日历没有出现。 objective 用于呈现带有事件列表的日历,我试图避免 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 而不是包含在脚本标签中。其次,您很可能希望函数在 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 可能与您正在尝试做的事情有关。