domster domster - 1 year ago 43
ASP.NET (C#) Question

Getting Value Of HiddenField and passing into var in JS

I'm trying to take the values from 2 asp:HiddenFields and storing them into the

var latitudeDB
,
var longitudeDB
in JavaScript. These 2 values set into the HiddenFields are from my database. I have already checked with
Debug.WriteLine
to check the 2 values upon start up of the website and they are both the exact values from the database.

May i ask how can i get the 2 values from the HiddenFields to store into
var latitudeDB
and
var longitudeDB
for future uses? And the two values ought to be FLOAT/DOUBLE to be used...

In Page_Load,

foreach (DataRow r in ds.Tables[0].Rows)
{
accountDB = r["AccountStatus"].ToString();
latitudeDB = Convert.ToDouble(r["Latitude"]);
longitudeDB = Convert.ToDouble(r["Longitude"]);
usernameDB = r["Username"].ToString();
ipDB = r["IPAddressOfCreation"].ToString();
}
HF_Latitude.Value = latitudeDB.ToString();
HF_Longitude.Value = longitudeDB.ToString();

Debug.WriteLine(HF_Latitude.Value);
Debug.WriteLine(HF_Longitude.Value);


In my server side form (JavaScript),

<script type="text/javascript">
function initMap() {
var latitudeDB = parseFloat(document.getElementById("<%= HF_Latitude.ClientID %>").value);
var longitudeDB = parseFloat(document.getElementById("<%= HF_Longitude.ClientID %>").value);

//var latitudeDB = $("#<%= HF_Latitude.ClientID %>").val();
//var longitudeDB = $("#<%= HF_Longitude.ClientID %>").val();

var mapProp = {
zoom: 15,
center: new google.maps.LatLng(latitudeDB, longitudeDB),
mapTypeId: google.maps.MapTypeId.HYBRID,
};

var map = new google.maps.Map(document.getElementById("generatedMap"), mapProp);

var marker = new google.maps.Marker({
position: new google.maps.LatLng(latitudeDB, longitudeDB),
map: map,
title: "<div style = 'height:50px;width:120px'><b>Consumer's location:</b><br />Latitude: " + latitudeDB + "<br />Longitude: " + longitudeDB,
});
}
</script>


HiddenFields,

<asp:HiddenField runat="server" ID="HF_Latitude" />
<asp:HiddenField runat="server" ID="HF_Longitude" />


Appreciate any help please! Thank you so much...

Answer Source

I solved my issue. The HiddenFields should be placed above the Javascript.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download