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 に答える