如何通过脚本中获得的值(Dvdistance中的值)来编码Begind [英] How Do I Pass The Value Obtained In The Script (Value In Dvdistance) To Code Begind

查看:109
本文介绍了如何通过脚本中获得的值(Dvdistance中的值)来编码Begind的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

aspx代码



aspx code

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script src="Scripts/jquery-1.4.1.min.js" type="text/javascript"></script>
    <script src="Scripts/jquery-1.4.1.js" type="text/javascript"></script>
     <script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?sensor=false&libraries=places"></script>
    <script type="text/javascript" >

        var source, destination;
        var directionsDisplay;
        var directionsService = new google.maps.DirectionsService();
        google.maps.event.addDomListener(window, 'load', function () {
            new google.maps.places.SearchBox(document.getElementById('txtSource'));
            new google.maps.places.SearchBox(document.getElementById('txtDestination'));
            directionsDisplay = new google.maps.DirectionsRenderer({ 'draggable': true });
        });


        function abc() {


            //*********DIRECTIONS AND ROUTE**********************//
            source = document.getElementById("txtSource").value;
            destination = document.getElementById("txtDestination").value;

            var request = {
                origin: source,
                destination: destination,
                travelMode: google.maps.TravelMode.DRIVING
            };


            //*********DISTANCE AND DURATION**********************//
            var service = new google.maps.DistanceMatrixService();
            var dvDistance = document.getElementById("dvDistance");
            service.getDistanceMatrix({
                origins: [source],
                destinations: [destination],
                travelMode: google.maps.TravelMode.DRIVING,
                unitSystem: google.maps.UnitSystem.METRIC,
                avoidHighways: false,
                avoidTolls: false
            }, function (response, status) {
                if (status == google.maps.DistanceMatrixStatus.OK && response.rows[0].elements[0].status != "ZERO_RESULTS") {
                    var distance = response.rows[0].elements[0].distance.text;
                    var duration = response.rows[0].elements[0].duration.text;

                    dvDistance.innerHTML = "";
                    dvDistance.innerHTML += "" + distance + "";
                    dis= dis.innerHTML += "" + distance + "";
                } else {
                    alert("Unable to find the distance via road.");
                }
            });

         var dis = document.getElementById("dvDistance");
            document.getElementById('<%= HiddenField1.ClientID%>').value = dis;

        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:button id="Button1" runat="server" onclientclick = "abc();" text="Button"

            onclick="Button1_Click" />
        <br />
        <asp:textbox id="TextBox1" runat="server"></asp:textbox>
&nbsp;
        <asp:hiddenfield id="HiddenField1" runat="server"/>
    </div>
        <div><table>
        <tr>
            <td> Source : <input type="text" id="txtSource" style="width:200px"  />  </td>
            <td> Destination : <input type="text" id="txtDestination" style="width:200px"  /> </td>
        </tr>
        <tr><td><div id="dvDistance"></div></td>
        </tr>
        <tr>
            <td><asp:hiddenfield id="HiddenField2" runat="server"/></td>
             </tr>
    </table></div>
    </form>
</body>
</html>





代码背后





code behind

protected void Button1_Click(object sender, EventArgs e)
        {
            TextBox1.Text = HiddenField1.Value;
        }

推荐答案

我可能会解释这个错误,但你可以这样做:



I may be interpreting this wrong but you can do this:

var dis = document.getElementById("dvDistance").innerText;
document.getElementById('<%= TextBox1.ClientID%>').value = dis;





你甚至不需要回复。删除按钮OnClick并添加AutoPostback =false



但要回答您的具体问题:



加载控件后会发生Click事件,因此更改不会包含在渲染中。如果再次单击该按钮,值是否会更改?这是因为它会记住上次在加载控件之前设置值的时间。



有很多方法可以在加载控件之前获取事件的详细信息,但如果上面的javascript工作就没有必要了。



让我知道^ _ ^

Andy



You don't even need a postback for this. Remove the button OnClick and add AutoPostback="false"

But to answer your specific question:

The Click event happens after the control has been loaded so the change will not be included in the render. If you click the button again, does the value change? This is because it remembers the last time you set the value before it loads the control.

There are ways to get the details of the event before the controls are loaded, but if the javascript above works there there is no need.

Let me know ^_^
Andy

您需要在响应/回调方法中将距离值分配给隐藏字段
You need to assign the distance value to hidden field in the response/callback method
Quote:

function(response,status){

function (response, status) {

Google Maps服务google.maps.DistanceMatrixService。请注意,与谷歌服务的距离是一个异步过程。



有一个技巧可以做到这一点。



1.首先创建一个html输入按钮,用于计算距离并对最终用户可见。

2.创建另一个隐藏的asp.net按钮(style = dispay:无服务器端的服务器端点击事件。

3.现在输入按钮的onclick事件,计算距离并在隐藏字段中设置响应。此外,当你在响应中获得距离并且一切都有效时,jQuery会触发asp.net按钮。

4.在asp.net按钮的服务器端事件中获取与隐藏字段的距离。



希望这会有所帮助。

of Google Maps service "google.maps.DistanceMatrixService". Note that getting distance from google service is an async process.

There is a trick to do this.

1. First create one html input button which is used to get calculate the distance and is visible to the end user.
2. Create another asp.net button which stays hidden (style=dispay:none) with a server side click event at server side.
3. Now on onclick event of input button, calculate the distance and get the response set in hidden field. Also, jQuery trigger the asp.net button when you get the distance in the response and everything valid.
4. On server side event of the asp.net button get the distance from the hidden field.

Hope this helps.


这篇关于如何通过脚本中获得的值(Dvdistance中的值)来编码Begind的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆