2016-08-15 45 views
0

我是C#的初學者。如何從Javascript函數中獲取變量在C#asp.net

問題:我試圖從Javascript函數compute_shape_area()檢索變量area,我該如何在C#asp.net中實現?非常感謝。

<!DOCTYPE html> 
<html> 
    <head> 
    <title>test</title>  
    </head> 

<body onload="compute_shape_area()"> 
    <p id="area1"></p> 
<script> 
    var Obj_shape; 

    function initMap() { 
    var request = new XMLHttpRequest(); 
    request.open('GET', 'http://localhost:8080/shape.txt');   
    //GET Data and build Obj_shape 
    request.send(); 
    } 

    function compute_shape_area(){ 
    var area = parseFloat(google.maps.geometry.spherical.computeArea(Obj_shape.getPath())); 
    area = area.toFixed(4); 

    document.getElementById("area1").innerHTML = area; 
    } 

</script> 

<script src="https://maps.googleapis.com/maps/api/js?key=APIKey&callback=initMap"></script> 

</body> 
</html> 

回答

0

你可以使用一個asp:HiddenField變量共享落後於C#和JavaScript代碼(回傳時viewstateapproch)代碼之間的值。在那裏我應該把這個腳本

<script type="text/javascript"> var yourJsVariable= '<%=yourJsVariable%>'</script>

+0

原諒我,??:

或者你可以使用下面的代碼這裏是一個錯誤:名稱'yourJsVariable'在當前上下文中不存在 –

+0

@HenryWenHanLee我的答案中的代碼只是一個說明性示例,爲了顯示主體:)您應該使用您的真實代碼應用程序更改它。在你的情況下,考慮回發檢索asp:HiddenField – GibboK

相關問題