简体   繁体   English

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

[英]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.

相关问题 如何从ScriptManager.RegisterStartupScript调用JavaScript函数? - How to call javascript function from ScriptManager.RegisterStartupScript? Page_Load上的ScriptManager.RegisterStartupScript在Framework 2.0上不起作用 - ScriptManager.RegisterStartupScript On Page_Load Not Working on Framework 2.0 使用ScriptManager.RegisterStartupScript从Code后面调用javascript方法 - Calling javascript Method from Code behind using ScriptManager.RegisterStartupScript 通过ScriptManager.RegisterStartupScript调用时,JavaScript回调函数无法找到控件 - JavaScript callback function unable to find control when calling through ScriptManager.RegisterStartupScript 如何在 ScriptManager.RegisterStartupScript 中包含的 javascript function 中使用 getElementById() 调用 asp 控件 - How to call an asp control using getElementById() in a javascript function that is included in ScriptManager.RegisterStartupScript 如何在handler.ashx文件中使用ScriptManager.RegisterStartUpScript调用javascript函数? - how can i call javascript functions with ScriptManager.RegisterStartUpScript inside handler.ashx file? 我应该在哪里放置ScriptManager.RegisterStartupScript来调用我的JavaScript - Where should I place the ScriptManager.RegisterStartupScript to call my javascript ScriptManager.RegisterStartupScript - ScriptManager.RegisterStartupScript ScriptManager.RegisterStartupScript(...)是否有问题? - Is there an issue with ScriptManager.RegisterStartupScript(…); 在ScriptManager.RegisterStartupScript之后的代码中调用脚本后,jQuery document.ready不会触发 - jQuery document.ready doesn't fire after script called in code behind ScriptManager.RegisterStartupScript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM