2015-06-24 36 views
-1

我試過一個隱藏的字段控件,但它不會將javascript值返回給c#後面的代碼。當我執行下面的代碼時,它總是返回0.如何在c#代碼後面獲取javascript值

如何獲得JavaScript到C#的距離值?

<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script> 

<script type="text/javascript"> 
    function GetDistance() 
    { 
     var origin = document.getElementById("orig").value, 
      destination = document.getElementById("dest").value, 
      service = new google.maps.DistanceMatrixService(); 
     service.getDistanceMatrix(
      { 
       origins: [origin], 
       destinations: [destination], 
       travelMode: google.maps.TravelMode.DRIVING, 
       avoidHighways: false, 
       avoidTolls: false 
      }, 
      callback 
     ); 

     function callback(response, status) { 
      alert('hello2'); 
      var orig = document.getElementById("orig"), 
       dest = document.getElementById("dest"), 
       dist = document.getElementById("dist"); 
      if (status == "OK") { 
       orig.value = response.originAddresses[0]; 
       dest.value = response.destinationAddresses[0]; 
       dist.value = response.rows[0].elements[0].distance.text; 
       document.getElementById('<%=hdnResultValue.ClientID%>').value = dist.value; 

      } else { 
       alert("Error: " + status); 
      } 
     } 
    } 
</script> 


<form id="frm1" runat="server" method="post"> 
    <br> 
    Basic example for using the Distance Matrix.<br><br> 
    Origin: <input id="orig" type="text" style="width:35em"><br><br> 
    Destination: <input id="dest" type="text" style="width:35em"><br><br> 
    Distance: <input id="dist" type="text" style="width:35em"> 
    <asp:Button ID="btnSubmit" runat="server" Text ="Submit Application" OnClientClick="GetDistance();" OnClick="btnSubmit_Click" /> 
    <asp:Label ID="lblDistance" runat="server" Text=" " /> 
    <asp:HiddenField ID="hdnResultValue" Value="0" runat="server" /> 
    </form> 
protected void btnSubmit_Click(object sender, EventArgs e) 
     { 
      lblDistance.Text = hdnResultValue.Value; 
      lblDistance.Text += "Calculated Successfully"; 
     } 
+0

在後面的代碼中,你是否試圖獲取hdnResultValue的值? –

回答

0

請參閱Distance Matrix Service Specification

該方法只返回一個DistanceMatrixResponse對象。如果您想要請求的狀態,則必須解析接收到的JSON元素,如鏈接頁面上顯示的那樣:rows [index] .elements.status。

{ 
    "origin_addresses": [ "Greenwich, Greater London, UK", "13 Great Carleton Square, Edinburgh, City of Edinburgh EH16 4, UK" ], 
    "destination_addresses": [ "Stockholm County, Sweden", "Dlouhá 609/2, 110 00 Praha-Staré Město, Česká republika" ], 
    "rows": [ { 
    "elements": [ { 
     "status": "OK", 
     "duration": { 
     "value": 70778, 
     "text": "19 hours 40 mins" 
     }, 
     "distance": { 
     "value": 1887508, 
     "text": "1173 mi" 
     } 
    }, { 
     "status": "OK", 
     "duration": { 
     "value": 44476, 
     "text": "12 hours 21 mins" 
     }, 
     "distance": { 
     "value": 1262780, 
     "text": "785 mi" 
     } 
    } ] 
    }, { 
    "elements": [ { 
     "status": "OK", 
     "duration": { 
     "value": 96000, 
     "text": "1 day 3 hours" 
     }, 
     "distance": { 
     "value": 2566737, 
     "text": "1595 mi" 
     } 
    }, { 
     "status": "OK", 
     "duration": { 
     "value": 69698, 
     "text": "19 hours 22 mins" 
     }, 
     "distance": { 
     "value": 1942009, 
     "text": "1207 mi" 
     } 
    } ] 
    } ] 
} 
相關問題