0
votes

I have js code which gets which gets users lat long but the problem is when I call the js function in my asp.net control button it does not work and no alert is shown and the function getLocation does not work

var x = document.getElementById("demo"); function getLocation() { if (navigator.geolocation) { navigator.geolocation.getCurrentPosition(showPosition, showError); } else { x.innerHTML = "Geolocation is not supported by this browser."; } }

function showPosition(position) {
   // var latlondata =  position.coords.latitude + "," +position.coords.longitude;
    var latlon =  position.coords.latitude;
    alert(latlon)

    document.getElementById('<%=abc123.ClientID%>').innerText = latlon;


}

function showError(error) {
    if (error.code == 1) {
        x.innerHTML = "User denied the request for Geolocation."
    }
    else if (err.code == 2) {
        x.innerHTML = "Location information is unavailable."
    }
    else if (err.code == 3) {
        x.innerHTML = "The request to get user location timed out."
    }
    else {
        x.innerHTML = "An unknown error occurred."
    }
}

and here is my control code

<asp:Label ID="abc123" runat="server" Text="Label"></asp:Label>

<asp:Button ID="Button1" runat="server" Text="Button" OnClientClick="getLocation()" OnClick="Button1_Click " />
2
Did you make sure the JS works as expected?Romano Zumbé
yes js is working if i do a simple html button call like this <button onclick="getLocation()">Get your Location</button> but do not work for asp.net controluser3819586
please add your "getLocation()" code function in your question.Govinda Rajbhar

2 Answers

1
votes

The OnClick event will reload the page and therefore the JS called from the OnClientClick will never be triggered. Remove the OnClick event.

Edit:

If you need to trigger the serverside event let your JS trigger it after the alert.

0
votes

Just add the following code to your button:

OnClientClick="getLocation(); return false;"