【问题标题】:Asp.net getting label value from codebehind. for google maps coordinateAsp.net 从代码隐藏中获取标签值。谷歌地图坐标
【发布时间】:2017-11-17 14:50:50
【问题描述】:

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

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>

代码隐藏

字符串示例 = Label1.Text; // 或innerhtml 或innertext..

【问题讨论】:

    标签: javascript asp.net maps coordinate


    【解决方案1】:

    标签的变化,以及非输入元素的变化,例如divsspans 等,由于 HTTP Post 特性,不会发布。

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

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

    【讨论】:

    • 谢谢 :) 并添加 clientidmode="static"。因为在母版页中。谢谢:)
    猜你喜欢
    • 2021-01-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多