Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
290 views
in Technique[技术] by (71.8m points)

c# - Get updated HiddenField Value in code behind

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.

enter image description here

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Set runat="server" attribute to your hidden field as shown below :

 <input type="hidden" value="" id="hiddenField" runat="server" />

Now you can directly access your hidden field value in your code behind :

var val = this.hiddenField.Value;

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...