Storing geolocation (lat,long) to a database table on page load is needed.
Here is the script used for this purpose:
<body onload="getLocation()">
<p>Click the button to get your coordinates.</p>
<%--<button onclick="getLocation()">Try It</button>--%>
<p id="demo"></p>
<form id="form1" runat="server">
<div>
<p>
<asp:HiddenField ID="hdnLocation" ClientIDMode="Static" runat="server" />
<asp:Label ID="lblloc" runat="server"></asp:Label>
<asp:TextBox ID="txtloc" runat="server"></asp:TextBox>
<asp:Button ID="btnLoc" text="submit" runat="server" OnClick="btnLoc_Click" />
</p>
</div>
</form>
<script>
var x = document.getElementById("demo");
function getLocation() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(showPosition);
} else {
x.innerHTML = "Geolocation is not supported by this browser.";
}
}
function showPosition(position) {
x.innerHTML = "Latitude: " + position.coords.latitude +
"<br>Longitude: " + position.coords.longitude;
$("[id*=hdnLocation]").val(position.coords.latitude + ' ' + position.coords.longitude);
// $("[id*=btnLoc]").trigger("click");
}
</script>
</body>
</html>
The code behind includes:
public partial class getLocation : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) {
lblloc.Visible = true;
lblloc.Text = hdnLocation.Value;
txtloc.Text = hdnLocation.Value;
}
When running the page, the hidden field values are visible in the browser inspection but cannot be accessed in the code behind. Response.Write returns blank and lblloc.text shows null. What could be causing this issue?