簡體   English   中英

從.ascx中的javascript調用.ascx的vb.net函數

[英]calling vb.net function of .ascx from javascript which is also in .ascx

我有一個我想從javascript調用的vb.net函數,都在.ascx中。 在這段代碼中,我使用一個jQuery彈出一個對話框,然后單擊對話框上的按鈕(btnok),我想調用一個函數loadgraph(),這是一個vb.net函數。

<link href="css/jquery-ui.css" rel="stylesheet" type="text/css" />
<script src="javascript/jquery-1.8.3.js" type="text/javascript"></script>

<script src="javascript/jquery.bgiframe-2.1.2.js" type="text/javascript"></script>
<link href="css/demos.css" rel="stylesheet" type="text/css" />
<script src="javascript/jquery-ui.js" type="text/javascript"></script>


<script>
    // increase the default animation speed to exaggerate the effect
    $.fx.speeds._default = 1000;


    $(function() {
        $( "#element_to_pop_up" ).dialog({
            autoOpen: false,
            show: "blind",
            hide: "explode"
        });

        $( "#button" ).click(function() {
            $( "#element_to_pop_up" ).dialog( "open" );
            return false;
        });

         $( "#btnok" ).click(function(){
          $( "#element_to_pop_up" ).dialog( "close" );
             $.ajax({
  type: "POST",
  url: "Schart.ascx/loadgraph",
  data: "{}",
  contentType: "application/json; charset=utf-8",
  dataType: "json",
  success: function(msg) {
    // Do something interesting here.
    alert("called")
  }
});
          return false;
         });
    });
    </script>

您嘗試使用的方法稱為PageMethods-但是,頁面方法代碼(靜態方法)必須是后面某些頁面(aspx)代碼的一部分。 您不能將頁面方法代碼放在用戶控件(ascx)代碼的后面。
我懷疑出現此限制的原因是,以.ascx結尾的網址不是供客戶端使用的(您將獲得404)-它們純粹是供服務器端處理的。

對您來說,簡單的解決方案是將頁面(aspx)代碼中的相關方法移到后面,並更改URL,例如"Schart.aspx/loadgraph" 您始終可以將所有代碼保留在ascx文件中,並從虛擬頁面方法代碼中調用它,從而將相關的UI和代碼保留在ascx文件中。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM