2017-01-03 139 views
0
<script type="text/javascript"> 
    $(document).ready(function() { 
     $('#loadbtn').click(function() { // can 't load 
      opts = { 
       title: 'ABCD', 
       series: [{ 
        neighborThreshold: 0 
       }], 
       axes: { 
        xaxis: { 
         //smethg 
        }, 
        yaxis: { 

         //smethg 
        } 
       }, 

      }; 
      PageMethods.LoadAsset(LoadSucc, LoadFail); //Want to load by button click event 
      function LoadSucc(obj) { 
       goog = obj; 
       //Something 
       alert("Data loaded"); 
      } 

      function LoadFail() { 
       alert("Data missing"); 
      } 
     }); 
    }); 
</script> 

加載Pagemethods在我的C#代碼通過點擊按鈕在jQuery的

[System.Web.Services.WebMethod] 
public static Array LoadAssetAssignView() 
{ 
    //something 
    return something; 
} 

ASP:

<asp:Button runat="server" ID="loadbtn" Text="Load" /> 

其實功能開始在文件準備工作的功能。我想在點擊按鈕時加載該功能。我該怎麼做呢?

回答

0

我想你錯過了在html中聲明該方法。例如:

<div> 
Your Name : 
<asp:TextBox ID="txtUserName" runat="server"></asp:TextBox> 
<input id="btnGetTime" type="button" value="Show Current Time" 
    onclick = "ShowCurrentTime()" /> 
</div> 

然後調用jQuery的:

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js" type="text/javascript"></script> 
<script type = "text/javascript"> 
function ShowCurrentTime() { 
    $.ajax({ 
     type: "POST", 
     url: "CS.aspx/GetCurrentTime", 
     data: '{name: "' + $("#<%=txtUserName.ClientID%>")[0].value + '" }', 
     contentType: "application/json; charset=utf-8", 
     dataType: "json", 
     success: OnSuccess, 
     failure: function(response) { 
      alert(response.d); 
     } 
    }); 
} 
function OnSuccess(response) { 
    alert(response.d); 
} 
</script> 

和最後的PageMethod的。您可以通過此鏈接查詢: http://www.aspsnippets.com/Articles/Call-ASPNet-Page-Method-using-jQuery-AJAX-Example.aspx