简体   繁体   English

如何在回发中获取隐藏字段值

[英]How do I get my hidden field values on Postback

Here is my java script 这是我的Java脚本

 $('#geoLocation').click(function () {
        alert("I am Here");
        if (navigator.geolocation) {
            // Get Latitude and Longitude
            navigator.geolocation.getCurrentPosition(showPosition);

        }
        else {
            // Hide Locator Panel, if Browser not supported
            document.getElementById('panel1').style.display = 'none';
        }
    });

    function showPosition(position) {
        //  x.innerHTML = "Latitude: " + position.coords.latitude +
        // "<br>Longitude: " + position.coords.longitude;
          document.getElementById('latitude').innerHTML = position.coords.latitude;
          document.getElementById('longitude').innerHTML = position.coords.longitude;


    }

I need to get at the latitude and longitude values (which are hidden on the screen), in Postback on the CodeBehind. 我需要在CodeBehind上的回发中获取经度和纬度值(隐藏在屏幕上)。 How do I do this? 我该怎么做呢?

I assume 'latitude' and 'longitude' are HTML elements on the page ? 我假设“纬度”和“经度”是页面上的HTML元素?

That being the case, you should be able to add a runat="server" attribute to them and then they will be available in server-side code. 在这种情况下,您应该能够向它们添加runat =“ server”属性,然后它们将在服务器端代码中可用。 If you're site is ASP.NET 4/4.5 you can also add ClientIDMode="Static" and your showPosition function will work as it is; 如果您的网站是ASP.NET 4 / 4.5,则还可以添加ClientIDMode =“ Static”,并且showPosition函数将按原样运行; if not you could a class to them and use a jQuery class selector to get your two items. 如果不是,您可以给他们一个类,并使用jQuery类选择器来获取两个项目。

You can use $('#HiddenFieldId').val() to assign data to HiddenField at client side. 您可以使用$('#HiddenFieldId').val()在客户端将数据分配给HiddenField

<div id="geoLocation">Click me to retrieve Geo Location</div>
<asp:HiddenField runat="server" ID="LatitudeHiddenField" />
<asp:HiddenField runat="server" ID="LongitudeHiddenField" />

<script type="text/javascript" 
  src="//ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script type="text/javascript">
    $('#geoLocation').click(function () {
        if (navigator.geolocation) {
            // Get Latitude and Longitude
            navigator.geolocation.getCurrentPosition(showPosition, geoLocationErrors);
        } else {
            // Hide Locator Panel, if Browser not supported
            document.getElementById('panel1').style.display = 'none';
        }

        function showPosition(position) {
            $('#<%= LatitudeHiddenField.ClientID %>').val(position.coords.latitude);
            $('#<%= LongitudeHiddenField.ClientID %>').val(position.coords.longitude);    
        }    

        function geoLocationErrors() {
            alert("Your browser doesn't support Geo Location.");
        }
    });

</script>

Then you can retrieve those value from HiddenField at server side as - 然后,您可以从服务器端的HiddenField检索这些值,方法是-

var latitute = LatitudeHiddenField.Value;
var longitude = LongitudeHiddenField.Value;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM