Get updated HiddenField Value in code behind

我只是一个虾纸丫 提交于 2019-12-14 03:29:07

问题


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.


回答1:


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;



回答2:


As you are updating value in javascript. You have to access updated value using Request.Form.

You will get updated value in Request.Form.

string latitudeValue = Request.Form[Latitude.UniqueId];

Example :

<asp:Content ID="BodyContent" ContentPlaceHolderID="MainContent" runat="server">
    <div class="col-md-4">
        <asp:HiddenField ID="HiddenField1" runat="server" Value="1" />
        <asp:LinkButton ID="LinkButton1" runat="server" OnClick="LinkButton1_Click" OnClientClick="CallThis()">LinkButton</asp:LinkButton>
    </div>

    <script type="text/javascript">
        function CallThis() {
            $('#<%= HiddenField1.ClientID %>').val(100);
        }
    </script>
</asp:Content>



来源:https://stackoverflow.com/questions/27421475/get-updated-hiddenfield-value-in-code-behind

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!