0

私のページには、スイッチがオンになったときに jQuery 関数を挿入するユーザー コントロールがあります。そして、これはユーザーコントロールの背後にある私のコードです:

public bool isRequired {
  set {
    if (value == true) {
      ClientScriptManager cs = Page.ClientScript;

      string csname = "isRequiredScript";
      if (!cs.IsClientScriptBlockRegistered(this.GetType(), csname)) {
        StringBuilder cstext = new StringBuilder();
        cstext.Append("<script type=\"text/javascript\">");
        cstext.Append("$(document).ready(function () {");
        cstext.Append("function QuestionwithConditionalInfo_validation() {");
        cstext.Append("if ($(\"#MainPlaceHolder_" + QuestionOption.ClientID + " :checked\").val() == null) {");
        cstext.Append("alert(\"Please answer the question '" + setQuestionText + "'\");");
        cstext.Append("return false;");
        cstext.Append("}} });");
        cstext.Append("</script>");
        cs.RegisterClientScriptBlock(this.GetType(), csname, cstext.ToString(), false);
      }
    }
  }
}

次に、メイン ページで、その jQuery 関数を呼び出すことを計画しました。

function childpage_validation() {
  if (QuestionwithConditionalInfo_validation() == false)
    return false;
}

<asp:Button ID="page1_Next" Text="Next page" runat="server" OnClick="page1_Next_Command" OnClientClick="return childpage_validation()" />

次に、関数が定義されていないというエラーが発生しました。その後、同じエラーが発生した代わりQuestionwithConditionalInfo_validation()に試しました。誰かが理由を知っていますか?RegisterStartupScriptRegisterClientScriptBlock

4

1 に答える 1

1

ドキュメントの準備ができているのは、そのイベント中に実行できるようにするためだけに必要であることを理解してください。これは実際にはイベントハンドラーです。それは必要ないので(イベントはすでに発生しています)、コードから削除できます。

cstext.Append("<script type=\"text/javascript\">");
cstext.Append("function QuestionwithConditionalInfo_validation() {");
cstext.Append("if ($(\"#MainPlaceHolder_" + QuestionOption.ClientID + " :checked\").val() == null) {");
cstext.Append("alert(\"Please answer the question '" + setQuestionText + "'\");");
cstext.Append("return false;");
cstext.Append("}};");
cstext.Append("</script>");

また、それを削除することで、そのクロージャーを削除し、グローバルオブジェクトになり、希望を表明するときにページからアクセスできるようになります。

于 2013-03-04T19:20:37.483 に答える