I have a LinkButton in aspx page where on OnClientClick I am updating the value of Hidden Field, and on OnClick I am saving that value to the database.
<asp:LinkButton runat="server" ID="lnkUpdateGeocode" Text="Update Geocode"
OnClientClick="updateGeoCode()" OnClick="lnkUpdateGeocode_Click" />
where my updateGeoCode() function is
function updateGeoCode() {
var address = document.getElementById('lblDisplayAddress');
var geocoder = new google.maps.Geocoder();
geocoder.geocode({ 'address': address }, function (results, status) {
if (status == google.maps.GeocoderStatus.OK) {
var latitude = results[0].geometry.location.lat();
var longitude = results[0].geometry.location.lng();
longitudeField = $get('<%=Longitude.ClientID %>');
latitudeField = $get('<%=Latitude.ClientID %>');
longitudeField.value = longitude;
latitudeField.value = latitude;
} else {
alert('Geocode was not successful');
}
});
};
and my lnkUpdateGeocode_Click() is
latitude = Latitude.Value.IsBlankOrNull() ? 0M : Convert.ToDecimal(Latitude.Value);
longitude = Longitude.Value.IsBlankOrNull() ? 0M : Convert.ToDecimal(Longitude.Value);
But I am always getting latitude and longitude values as 0.
So My Question is how to get the updated value of HiddenField in CodeBehind when I am changing that in javascript. Any help would be appreciated.
Update:
My HiddenField Value is set as runat="server"
<asp:HiddenField ID="Longitude" runat="server" EnableViewState="false" />
<asp:HiddenField ID="Latitude" runat="server" EnableViewState="false" />
I tried Request.Form[Latitude.UniqueID]
but it is also giving Blank string.
Set runat="server" attribute to your hidden field as shown below :
Now you can directly access your hidden field value in your code behind :
As you are updating value in javascript. You have to access updated value using Request.Form.
You will get updated value in Request.Form.
Example :