Get Label value after PostBack in ASP.Net using C# and VB.Net

Last Reply 3 months ago By pandeyism

Posted 3 months ago

hello,

i am using this geo api for user current location tracking, it works but the problem is on post back i am unble to fetch the value of lbladdress, all the time it gives blank value, bcs i think javascript loads after the postback completion. 

<script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?&key=AIzaSyBE1J5Pe_GZXBR_x9TXOv6TU5vtCSmEPW4"></script>
<script type="text/javascript">
    if (navigator.geolocation) {
        navigator.geolocation.getCurrentPosition(function (p) {
            var LatLng = new google.maps.LatLng(p.coords.latitude, p.coords.longitude);
            var geocoder = geocoder = new google.maps.Geocoder();
            geocoder.geocode({ 'latLng': LatLng }, function (results, status) {
                if (status == google.maps.GeocoderStatus.OK) {
                    if (results[1]) {
                        document.getElementById('<%= lblAddress.ClientID %>').innerHTML = "" + results[1].formatted_address;
                    }
                }
            });
        });
    } else {
        alert('Geo Location feature is not supported in this browser.');
    }
</script>

 

Posted 3 months ago Modified on 3 months ago

Hey nauna,

The issue is , Label is converted into span element and html elements like span and div do't have ViewState. So use HiddenField and store your value in HiddenField and get label value on PostBack.

Please refer below sample.

HTML

<script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?&key=AIzaSyBE1J5Pe_GZXBR_x9TXOv6TU5vtCSmEPW4"></script>
<script type="text/javascript">
    if (navigator.geolocation) {
        navigator.geolocation.getCurrentPosition(function (p) {
            var LatLng = new google.maps.LatLng(p.coords.latitude, p.coords.longitude);
            var geocoder = geocoder = new google.maps.Geocoder();
            geocoder.geocode({ 'latLng': LatLng }, function (results, status) {
                if (status == google.maps.GeocoderStatus.OK) {
                    if (results[1]) {
                        document.getElementById('<%= hfLabelValue.ClientID %>').value = "" + results[1].formatted_address;
                    }
                }
            });
        });
    } else {
        alert('Geo Location feature is not supported in this browser.');
    }        
</script>
<div>
    <asp:Button Text="GetLocation" ID="btnGet" runat="server" OnClick="GetLocation" />
    <asp:HiddenField ID="hfLabelValue" runat="server" />
    <br />
    <asp:Label ID="lblAddress" runat="server" />
</div>

Code

C#

protected void GetLocation(object sender, EventArgs e)
{
    string location = hfLabelValue.Value;
    lblAddress.Text = location;
}

VB.Net

Protected Sub GetLocation(sender As Object, e As System.EventArgs) Handles btnGet.Click
    Dim location As String = hfLabelValue.Value
    lblAddress.Text = location
End Sub

Screenshot