2013-01-08 30 views
1

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

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

回答

1

你正在嘗試使用被稱爲PageMethods - 然而,頁面方法的代碼(靜態方法)必須的一些頁面(CSS)的代碼部分的後面。您不能將頁面方法代碼放在用戶控件(ascx)代碼後面。
我懷疑這個限制的原因是,以.ascx結尾的網址並不意味着客戶端消費(你會得到404) - 它們純粹是爲了服務器端操作。

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