How can I get Latitude and Longitude in c# webforms from codebehind

Soykan Ozcelik 0 Reputation points

How can I get Latitude and Longitude in c# web forms from codebehind? or another suggested way js or?

My goal find lat and lon insert that values to the table while sales operation inserting or updating records.

A set of technologies in the .NET Framework for building web applications and XML web services.
3,371 questions
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,497 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Lan Huang-MSFT 27,796 Reputation points Microsoft Vendor

    Hi @Soykan Ozcelik,

    It is not very clear how you get the latitude and longitude, but you can refer to the following method.

    Add asp hidden fields, with runat="server" to hold the lat and lng. When you get the lat and lng in your javascript simply populate the hidden fields with those values. Then you can reference the hidden fields values directly in your OnClick handler.

    <html xmlns=""> 
       <head id="Head1" runat="server">
       <script type="text/javascript" src=""></script>
       <script type="text/javascript">
           var long = 0;
           var lat = 0;
           window.onload = function () {
               var mapOptions = {
                   center: new google.maps.LatLng(20.9600, 80.0000),
                   zoom: 14,
                   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) {
                   long = e.latLng.lng();
                   lat =;
                   document.getElementById("lat").value = lat;
                   document.getElementById("lng").value = long;
                   alert("Latitude: " + lat + "\r\nLongitude: " + long);
    <form id="myForm" runat="server">
       <div id="dvMap" style="width: 300px; height: 300px">
       <asp:HiddenField ID="lat" runat="server" />
       <asp:HiddenField ID="lng" runat="server" />
       <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
     protected void Button1_Click(object sender, EventArgs e)
                Double latitude = Convert.ToDouble(lat.Value);
                Double longitude = Convert.ToDouble(lng.Value);
                SqlConnection con = new SqlConnection();
                con.ConnectionString = "****";
                string query1 = "insert into Courses(longi,lati) values (@lati, @longi)";
                SqlCommand cmd1 = new SqlCommand(query1, con);
                cmd1.Parameters.AddWithValue("@lati", latitude);
                cmd1.Parameters.AddWithValue("@longi", longitude);

    Best regards,
    Lan Huang

    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments