[英]ScriptManager.RegisterStartupScript to fire javascript function in seperate javascript file after Page Load
I have studied and tried 3 different solutions, but haven't been able to get past the annoying error : 我研究并尝试了3种不同的解决方案,但始终无法克服烦人的错误:
Uncaught ReferenceError: SetupRichTextAndTags is not defined
Situation : 情况:
I am populating a hiddenfield with data (C# back-end), this is purely HTML which i will use to populate a SummerNote rich-text field by calling the following javascript : 我正在用数据填充隐藏字段(C#后端),这是纯HTML,我将使用它通过调用以下javascript来填充SummerNote富文本字段:
$(".summernote").code("your text");
My tries at RegisterStartupScript : 我在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);
All of these gives me the error... 所有这些都给了我错误...
The script itself is within an included javascript file in the aspx page, and i think that might be the issue.. But.. i have not found any solutions to how to actually fix that.. 该脚本本身在aspx页面的一个包含的javascript文件中,我认为可能是问题所在。....但是我还没有找到任何解决方法来解决这个问题。
Any tips ? 有小费吗 ?
The JavaScript function SetupRichTextAndTags
is not available on the page when your registered scripts run. 您注册的脚本运行时,JavaScript函数SetupRichTextAndTags
在页面上不可用。
Before you can call the function you need to load it into the page. 在调用该函数之前,需要将其加载到页面中。 You can declare the function in a client script block but then you have to write the JavaScript into the C# code which is not easy to work with. 您可以在客户端脚本块中声明该函数,但随后必须将JavaScript编写到不易使用的C#代码中。 Instead you can declare the functions in a normal JavaScript file and then load that into the page. 相反,您可以在普通的JavaScript文件中声明函数,然后将其加载到页面中。
Here is a template, note that you check if the script blocks are registered so they don't get added again if there is a post back. 这是一个模板,请注意,您检查脚本块是否已注册,以便在有回发的情况下不会再次添加它们。
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.