2016-07-08 17 views
0

オブジェクトを作成するためのページがあります。このページでは、チェックボックスをオンにすると、いくつかのテキストフィールドを含む隠し領域が表示されます。ユーザーがオブジェクトを送信しようとすると、サーバーは検証エラーを送信してユーザーがいくつかのフィールドを修正する必要があります。問題が発生した場合、追加のフィールドがある領域が再び非表示になります(チェックボックスがオンになっているため、チェックをはずしてからもう一度チェックする必要があります)。それで何か変更やサーバからエラーが出るたびにこの関数を呼び出すことは可能ですか?ASP.NETでのページの読み込み(更新/変更)でjavascript(またはjQuery)を呼び出す方法

function changeVisibilityOfCreateCustomerSection() { 
      if ($("#isnewcustomer").prop("checked") == true) { 
       $(".saf-createcustomersection > div > input").prop("disabled", false); 
       $(".saf-createcustomersection").show(); 
      } else { 
       $(".saf-createcustomersection > div > input").prop("disabled", true); 
       $(".saf-createcustomersection").hide(); 
      } 
     } 

私はこのようにそれを呼び出しています:私はjavascriptのタグにこのようなものを使用しようとしていた

@Html.EditorFor(model => model.isNewCustomer, 
         new { htmlAttributes = new { 
          @class = "checkBox", 
          onchange = "changeVisibilityOfCreateCustomerSection();", 
          id = "isnewcustomer" } }) 

私はこのjQueryのコードを使用しています追加のセクションを示すと隠すため
$(document).ready(function() { 
      changeVisibilityOfCreateCustomerSection(); 
     }); 

     $(window).load(function() { 
      changeVisibilityOfCreateCustomerSection(); 
     }); 

しかし、これらの機能はまったく機能しません。私はそのページが部分的でbodyタグは共有レイアウトになっていると言うべきなので、bodyタグなどのonloadでこのような関数を呼び出すのがよい習慣であるかどうかはわかりません...

答えて

0

あなたの部分ページあなたはPage_Loadを使って次のことを行うことができます:

protected void Page_Load(object sender, EventArgs e){ 
    // checks here if the page is loaded normally not with a submission to the server from the same page 
    if(!IsPostBack){ 
     ScriptManager.RegisterClientScriptBlock(this, typeof(Page), "functionName()", true); 
    } 
} 
+0

どこに配置すればよいですか? (私はプロジェクトでMVCパターンを使用しています) – natanao1995

+0

あなたのhtmlの背後にあるコード – inoabrian

関連する問題