Search code examples
javascriptasp.netonclickonclientclick

onOnClientClick is working but onClick is not working on asp.net


I have this code on my javascript:

         function getListOfPOI() {
            geocoder = new google.maps.Geocoder();
            var address = document.getElementById('tbCity').value;

            geocoder.geocode({ 'address': address }, function (results, status) {
                if (status == google.maps.GeocoderStatus.OK) {
                    map.setCenter(results[0].geometry.location);
                    var request = {
                        location: results[0].geometry.location,
                        radius: 8000,
                        types: all //['store']
                    };
                    infowindow = new google.maps.InfoWindow();
                    var service = new google.maps.places.PlacesService(map);
                    service.nearbySearch(request, callback);
                } else {
                    alert('Geocode was not successful for the following reason: ' + status);
                }
            });

            return true;
        }

And I call this function with the button below:

<asp:Button ID="btnAddCity" Height="20px" Text="Add" runat="server" OnClientClick="return getListOfPOI();" OnClick="btnAddCity_Click" UseSubmitBehavior="false"    />

The OnClientClick is perfectly works, but the OnClick function is not fired. What should I do? Thank you in advance for your insights.

Cheers, Nisa


Solution

  • So with the help of @lucidgold, finally it is now working! The solution is to call the javascript function on the button server side. After tweaking around, here's the complete solution:

    1. I remove return true on the JavaScript function
    2. Remove the useSubmitBehaviour property on the button:
      <asp:Button ID="TestButton" Text="Test Me" OnClick="TestButton_Click" runat="server"/>

    3. Run RegisterStartupScript in C# as follows:
      ClientScript.RegisterStartupScript(GetType(), "script", "<script type ='text/javascript'> getListOfPOI(); </script>");