0

任何人都可以幫助我調用我的javascript函數內的VB.NET方法嗎?我的方法不共享/靜態,並不返回任何東西。它只是簡單地將數據保存到數據庫並重定向用戶。請幫幫我,這裏是我的代碼:在客戶端函數(Javascript)中調用Serverside方法

VB方法

Public Function SaveVehicles() 
      Dim res As Boolean 
      If res = True Then 
      Dim sModel As String = cboModel.SelectedValue 
      Dim sVariant As String = cboVariant.SelectedValue 
      Dim sColor As String = cboColor.SelectedValue 

      cboModel.SelectedValue = sModel 
      cboVariant.SelectedValue = sVariant 
      cboColor.SelectedValue = sColor 


      Dim oData As New WebServVehSwapping 
      Dim strSql As String 
      Dim sDealer As String 
      Dim sUserName As String 

      'sDealer = "TBP01" 
      sDealer = Trim(Request.QueryString("dealercode")) 
      If sDealer = "" Then sDealer = "TBP01" 
      sUserName = "User1" 

      '------------------------------------------ 
      strSql = oData.InsertTblRequirement(_ 
       sDealer _ 
      , Now.ToString _ 
      , sUserName _ 
      , cboColor.Text.Trim _ 
      , cboModel.Text.Trim _ 
      , cboVariant.Text.Trim, "Open") 
      MsgBox("OKAY") 
      Response.Redirect("MyRequirements.aspx?DealerCode=" & sDealer) 
     Else 
      'do Nothing 
     End If 
    End Function 

,這裏是我的JavaScript函數

function ConfirmView() 
    { 
     var Ok = confirm('There is/are existing vehicle(s) in Network Vehiches for sale, View Vehicle(s)?'); 
     if(Ok==true) 
     { 

     location.href = 'NetworkVehiclesforSale.aspx'; 
     return false; 
     } 
     else if (Ok!=true) 
     { 

     //THE VB METHOD GOES HERE  
     } 
} 

我已經試過了回調處理程序,它只是與功能的工作原理是返回的東西/字符串

我試過Pagemethod,但它只是與靜態/共享功能。請幫助我,我真的很需要它。 pleaase即時通訊pleasee。由於

+0

如果我的回答對你有幫助,如果你考慮將其標記爲答案或詢問後續問題,我將不勝感激。 – Steve 2011-06-06 07:13:47

回答

1

您可能需要閱讀「介紹構建Windows通信基礎服務」 - http://msdn.microsoft.com/en-us/library/aa480190.aspx

,特別是:「指南設計和構建RESTful Web服務與WCF 3.5」 - http://msdn.microsoft.com/en-us/library/dd203052.aspx

,並檢查出,這使得調用REST Web服務的簡單,像jQuery一些JavaScript庫 - http://www.jquery.com/

使用jQuery,你可以撥打電話,以這樣你的服務器端的VB.NET代碼:

$.ajax({ 
    type: "GET", 
    contentType: 'text/json', 
    dataType: 'json', 
    url: "https://URL-TO-SERVICE-HERE/...", 
    timeout: 10000, 
    error: function() { 

     // Deal with the error 

    }, 
    success: function (data) { 

     // Do something with the result 
     // Example: 
     if (data.dealerCode) 
      location.href = 'MyRequirements.aspx?DealerCode=' + data.dealerCode; 


    } 
}); 
+0

我將在哪裏放置該代碼? – 2011-05-23 00:32:31

+0

以及我可以調用SaveVehicles()方法的代碼部分? – 2011-05-23 00:37:17

+0

你會把這個JavaScript片段放在你寫的「// VB方法到這裏」你的問題。您可以在代碼片段中更改「URL-TO-SERVICE-HERE」以指向您的.svc文件(WCF服務) - 這將運行您的「SaveVehicles()」方法。 – Steve 2011-05-23 02:57:45

1

.Net Web服務無法執行魔術,即您無法對服務器上的Ajax請求發出重定向響應,並期望重定向整個頁面。 唯一會發生的事情是,Ajax調用被重定向到另一個頁面,並嘗試從那裏獲取數據。如果您想在客戶端瀏覽器中更改頁面,則必須通過JavaScript在客戶端執行此操作,例如, document.location = url_returned_by_your_function_through_ajax_call

+0

如何調用SaveVehicles()方法? – 2011-05-23 00:37:54

相關問題