Eman Aboellil Eman Aboellil - 1 year ago 120
Vb.net Question

pass variable from javascript to ASP.NET behind code VB.Net

I'm trying to show and get coordinates of website visitors' location to store it in my database, I've done a simple trial using google maps API in javascript.
now I need to pass the coordinates values from javascript to my code to make the calculations required and store it in database.

Here's the locater code

function success(position) {
var s = document.querySelector('#status');
if (s.className == 'success') {
s.innerHTML = "Gotcha!";
s.className = 'success';

var mapcanvas = document.createElement('div');
mapcanvas.id = 'mapcanvas';
mapcanvas.style.height = '400px';
mapcanvas.style.width = '560px';


var latlng = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);

var myOptions = {
zoom: 15,
center: latlng,
mapTypeControl: false,
navigationControlOptions: { style: google.maps.NavigationControlStyle.SMALL },
mapTypeId: google.maps.MapTypeId.ROADMAP
var map = new google.maps.Map(document.getElementById("mapcanvas"), myOptions);

var marker = new google.maps.Marker({
position: latlng,
map: map,
title: "You are here! (at least within a " + position.coords.accuracy + " meter radius)"

function error(msg) {
var s = document.querySelector('#status');
s.innerHTML = typeof msg == 'string' ? msg : "failed";
s.className = 'fail';

if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(success, error);
} else {
error('not supported');

//the problem is here!
var getter = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);
document.getElementById('<%=send_laty.ClientID%>') = getter.lat;
document.getElementById('<%=send_lony.ClientID%>') = getter.lng;


I've created 2 hidden fields to carry the variables from javascript to code behind

<asp:HiddenField ID="send_laty" runat="server" Value="" />
<asp:HiddenField ID="send_lony" runat="server" Value="" />

and I'm testing them on the page_load of the aspx.vb page like this

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim output As String
output = send_laty.Value + " ___ " + send_lony.Value

End Sub

but the output is _______ which means that the variables are not passed!

I need a hint about why it's not passing the values!

Thanks in advance,

Answer Source

You need to assign the result to the value property.

document.getElementById('<%=send_laty.ClientID%>').value = getter.lat;
document.getElementById('<%=send_lony.ClientID%>').value = getter.lng;