Asp.net从代码隐藏中获取标签值。用于谷歌地图坐标

时间:2017-11-17 14:50:51

标签: javascript asp.net maps coordinate

我无法在后面的代码中获得标签的价值。我想保存谷歌地图的坐标。你能帮我看看我错过了什么吗?

HTML

 <span>Location:</span>
    <input type="text" id="txtPlaces" style="width: 250px" placeholder="Enter a location" />
    <div id="dvMap" style="width: 100%; height:300px;">
       <asp:Label ID="Label1" runat="server"></asp:Label>

          <asp:Label ID="Label2" runat="server"></asp:Label>


        <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />

的Javascript

<script type="text/javascript">
    google.maps.event.addDomListener(window, 'load', function () {
        var places = new google.maps.places.Autocomplete(document.getElementById('txtPlaces'));
        google.maps.event.addListener(places, 'place_changed', function () {
            var place = places.getPlace();
            var address = place.formatted_address;
            var latitude = place.geometry.location.lat();
            var longitude = place.geometry.location.lng();
            var mesg = "Address: " + address;
            mesg += "\nLatitude: " + latitude;
            mesg += "\nLongitude: " + longitude;
            document.getElementById('Label1').textContent = latitude;
            document.getElementById('Label2').textContent = longitude;

            var lat = document.getElementById('Label1').textContent;
            var lon = document.getElementById('Label2').textContent;
            var mapOptions = {
                center: new google.maps.LatLng(lat, lon),
                zoom: 16,
                mapTypeId: google.maps.MapTypeId.ROADMAP
            };
            var infoWindow = new google.maps.InfoWindow();
            var latlngbounds = new google.maps.LatLngBounds();
            var map = new google.maps.Map(document.getElementById("dvMap"), mapOptions);
            google.maps.event.addListener(map, 'click', function (e) {
                // alert("Latitude: " + e.latLng.lat() + "\r\nLongitude: " + e.latLng.lng());
            });
            alert(mesg);


        });
    });
</script>

代码隐藏

string example = Label1.Text; //或innerhtml或innertext ..

1 个答案:

答案 0 :(得分:0)

标签以及非输入元素的变化,例如divsspans等由于HTTP Post性质而未被发布。

我要做的就是添加隐藏并设置您想要在那里发布的值。就像这样:

<input runat="server" ID="Label1Value" class="form-control" type="hidden" />