Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

Here is my code:

<script type="text/javascript">
            var geocoder;
            var map;
            var latlngstr;
            function initialize() {
                geocoder = new google.maps.Geocoder();
                var latlng = new google.maps.LatLng(-34.397, 150.644);
                var mapOptions = {
                    zoom: 15,
                    center: latlng,
                    mapTypeId: google.maps.MapTypeId.ROADMAP
                };
                map = new google.maps.Map(document.getElementById("map-canvas"), mapOptions);
            }


            function codeAddress() {
                var address = document.getElementById('address').value;

                geocoder.geocode({ 'address': address }, function (results, status) {
                    if (status == google.maps.GeocoderStatus.OK) {

                        latlngstr = results[0].geometry.location;

                        map.setCenter(results[0].geometry.location);
                        var marker = new google.maps.Marker({
                            map: map,
                            position: results[0].geometry.location
                        });
                    } else {
                        alert('Geocode was not successful for the following reason: ' + status);
                    }
                });


                document.getElementById('lat').value = latlngstr.lat();
                document.getElementById('lng').value = latlngstr.lng();
            }
        </script>
      </head>
      <body onload="initialize()">
          <form  id="form1" runat="server">

        <div>
            <input id="address" type="text" value="sydney" />
            <input type="button" value="Geocode" onclick="codeAddress()">
            <input id="lat" type="text" />

            <input id="lng" type="text" />
        </div>


            <asp:Button ID="Button1" runat="server" 
                  Text="Button" EnableViewState="False" 
                  ViewStateMode="Disabled" OnClientClick="javascript:return codeAddress();"/>



          <div id="map-canvas"></div>
       </form>   
      </body>
    </html>

I want to call the function codeAddress. When I call this function with an html button, the button with the value Geocode, it works fine. But when I call that function with an ASP.Net button the 'else' part is executed and it produces no result.

share|improve this question
add comment

1 Answer

I infer that what you want to do is use the geocoding code to obtain the latitude and longitude and then post back with those.

Rather than make one button do client-side code and (at the same time) post back, I'd keep the HTML button but start a form post from your JavaScript code, once the fields have been set:

            document.getElementById('lat').value = latlngstr.lat();
            document.getElementById('lng').value = latlngstr.lng();
            document.forms[0].submit();
share|improve this answer
 
thanks for the reply Ann but I want to call the function on the click of asp.net button control only....I passed the function name in the onclientclick attribute of the button but it execute the else part of the function.... –  Deepak Garg Mar 24 '13 at 18:31
 
This may or may not be the problem, but the syntax you used is incorrect. Rather than javascript:return codeAddress(); it should be codeAddress(), like with the HTML button. –  Ann L. Mar 24 '13 at 18:48
 
thanks for the reply Ann....my problem is solved...i have added the UseSubmitBehavior="false" attribute to button control and now it is working fine... –  Deepak Garg Mar 25 '13 at 15:43
 
@DeepakGarg Great! –  Ann L. Mar 25 '13 at 15:53
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.