繁体   English   中英

ScriptManager.RegisterStartupScript在页面加载后在单独的javascript文件中触发javascript函数

[英]ScriptManager.RegisterStartupScript to fire javascript function in seperate javascript file after Page Load

我研究并尝试了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文件中,我认为可能是问题所在。....但是我还没有找到任何解决方法来解决这个问题。

有小费吗 ?

您注册的脚本运行时,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);
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM