javascript - 添加 MasterPage 时 ScriptManager.RegisterClientScriptBlock 不再工作

标签 javascript c# asp.net

我像这样从 C# 调用 javascript 函数:

        ScriptManager.RegisterClientScriptBlock(this, GetType(), "script", "$(function () {initializeMap(); });", true);

在我添加母版页之前它工作得很好:

MasterPageFile="~/Header.master

<%@ Register Assembly="myassembly" Namespace="mynamesapce" TagPrefix="mytag" %> <%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="ajaxToolkit" %>

并将我的代码放入 asp 内容中:

<asp:Content ID="Content1" ContentPlaceHolderID="Body" runat="Server">

添加完所有这些后,javascript 函数调用不再起作用,并且出现此错误:

Uncaught ReferenceError: $ is not defined

当我点击错误时,它也会出现

$(function () {initializeMap(); }); 您知道这个问题吗?

最佳答案

我敢打赌,如果您的 jQuery 声明仍在声明中,那么它在 HTML 中的位置会有所不同。

我倾向于使用 RegisterStartupScript,虽然不是万无一失,但不太可能导致这些问题,因为脚本将在页面底部呈现。

ScriptManager.RegisterStartupScript(this, GetType(), "script", "$(function () {initializeMap(); });", true);

关于javascript - 添加 MasterPage 时 ScriptManager.RegisterClientScriptBlock 不再工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44521195/

相关文章:

javascript - jquery 脚本 : z-index and setTimeout 的两个问题

javascript - 使用javascript添加动态CSS?

c# - 向后复制数组?阵列。复制?

c# - 未捕获的语法错误 : Unexpected token ILLEGAL ASP. NET mvc3

c# - Webdriver 在错误的输入元素中输入了错误的文本

c# - 如何在没有任何错误或警告的情况下找到构建失败的原因

javascript - 在 ejs 中有两个选项卡,当我返回到原始选项卡时它停留在第二个选项卡上,任何建议表示赞赏

javascript - 元素随着页面变宽而重新定位

javascript - 提交更改时如何避免 “Are you sure you want to navigate away from this page?”?在 IE 中点击 gridview 进行分页

c# - 在循环中使用 FindControl 获取以编程方式添加的文本框的值