簡體   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