Question

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

Was it helpful?

Solution 2

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>");

OTHER TIPS

Another way to fire JavaScript Code + Server code is to do the following where we make use of ClientScript.RegisterStartupScript and call our JavaScript function!

In your .ASPX Page:

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script>
        function clientTestClick() {
            document.write("Test!");
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Button ID="TestButton" Text="Test Me" UseSubmitBehavior="false" OnClick="TestButton_Click" runat="server"/>
    </div>
    </form>
</body>
</html>

Depending on code behind language..

C#:

protected void TestButton_Click(object sender, EventArgs e)
{
    ClientScript.RegisterStartupScript(Page.GetType, "JavaScript", "clientTestClick();", true);

    /*  PUT YOUR SERVER CODE HERE */
}

VB.NET:

Protected Sub TestButton_Click(sender As Object, e As EventArgs) Handles TestButton.Click

    ClientScript.RegisterStartupScript(Me.GetType(), "JavaScript", "clientTestClick()",True)

    Dim x As String 
    x="hello" 'You can set a debug-point here and see that this will fire

End Sub

Hope this helps, let me know!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top