简体   繁体   English

如何在后面的c#代码中获取javascript值

[英]How to get javascript value in c# code behind

I tried a hidden field control, but it doesn't return the javascript value to the code behind in c#.我尝试了一个隐藏字段控件,但它不会将 javascript 值返回到 c# 中的代码后面。 When I execute the code below, it always returns 0.当我执行下面的代码时,它总是返回 0。

How do I get the distance value from javascript to c#?如何获取从 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";
        }

See Distance Matrix Service Specification .请参阅距离矩阵服务规范

The method return only one DistanceMatrixResponse object.该方法只返回一个DistanceMatrixResponse对象。 If you want status of your request, you have to parse received JSON element as presented on linked page : rows[index].elements.status.如果您想要请求的状态,您必须解析接收到的 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"
      }
    } ]
  } ]
}

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

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