1

我研究并尝试了 3 种不同的解决方案,但无法克服恼人的错误:

Uncaught ReferenceError: SetupRichTextAndTags is not defined

情况 :

我正在使用数据(C# 后端)填充隐藏字段,这纯粹是 HTML,我将通过调用以下 javascript 来填充 SummerNote 富文本字段:

$(".summernote").code("your text");

我在 RegisterStartupScript 的尝试:

//ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "script", "$(function () { SetupRichTextAndTags(); });", true);
//ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "tmp", "<script type='text/javascript'>SetupRichTextAndTags();</script>", false);
ScriptManager.RegisterStartupScript(Page, GetType(), "SetupRichTextAndTags", "<script>SetupRichTextAndTags()</script>", false);

所有这些都给了我错误...

该脚本本身位于 aspx 页面中包含的 javascript 文件中,我认为这可能是问题所在。但是..我还没有找到任何解决方案来实际解决这个问题..

有小费吗 ?

4

1 回答 1

4

当您的注册脚本运行时,页面上的 JavaScript 功能SetupRichTextAndTags不可用。

在调用该函数之前,您需要将其加载到页面中。您可以在客户端脚本块中声明该函数,但是您必须将 JavaScript 写入 C# 代码,这并不容易使用。相反,您可以在普通的 JavaScript 文件中声明函数,然后将其加载到页面中。

这是一个模板,请注意检查脚本块是否已注册,以便在有回帖时不会再次添加它们。

ClientScriptManager csm = Page.ClientScript;

// this registers the include of the js file containing the function
if (!csm.IsClientScriptIncludeRegistered("SetupRichTextAndTags"))
{
    csm.RegisterClientScriptInclude("SetupRichTextAndTags", "/SetupRichTextAndTags.js");
}

// this registers the script which will call the function 
if (!csm.IsClientScriptBlockRegistered("CallSetupRichTextAndTags"))
{
    csm.RegisterClientScriptBlock(GetType(), "CallSetupRichTextAndTags", "SetupRichTextAndTags();", true);
}
于 2016-01-18T23:28:34.167 回答