简体   繁体   English

如何从C#asp.net中的Javascript函数获取变量

[英]How to get variable from Javascript function in C# asp.net

I'm a beginner of C#. 我是C#的初学者。

Question: I'm trying to retrieve a variable area from Javascript function compute_shape_area() , how do I implement in C# asp.net? 问题:我正在尝试从Javascript函数compute_shape_area()检索变量area ,如何在C#asp.net中实现? Thanks a lot. 非常感谢。

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

You could use an asp:HiddenField variables to share values between code behind in C# and your JavaScript code (viewstateapproch during postbacks). 您可以使用asp:HiddenField变量在C#后面的代码和JavaScript代码(回发期间使用viewstateapproch)之间共享值。

Or you could use the following code: 或者,您可以使用以下代码:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM