javascriptasp.netmapscoordinate

Asp.net getting label value from codebehind. for google maps coordinate


i can not get value of label in code behind. i'd like to save the coordinates from google map. can you help me to see what i'm missing?

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>

CodeBehind

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


Solution

  • Changes in Labels, as well as in non-input elements e.g. divs, spans etc, won't be posted because of HTTP Post nature.

    What I would do in your place is adding a hidden and set the value you want to be posted there. Just something like this:

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