2017-08-01 127 views
1

我試圖使用地理定位獲取值(緯度,經度)。我嘗試在另一個頁面中使用相同的Javascript(我將在該問題下發布它),但是此Javascript只會在點擊某個按鈕時觸發,使用Javascript獲取空值

重點關注OnClientClick =「call(); return false;」

<asp:Button ID="BTN_Register" runat="server" Text="Register" OnClientClick="call();return false;" CssClass="btn btn-theme-bg btn-3d btn-xs" OnClick="BTN_Register_Click" /> 

所以回到我的問題。我現在用的是相同的JavaScript代碼,因爲我是在另外的WebForm。而且它檢索經緯度然後將其解析成2 ASP的過程:HiddenFields,然後使用後面的代碼(aspx.cs)它們保存到數據庫中,現在

resultRecord = al.createLogEntry(username, externalIP, Convert.ToDouble(latitudeTB.Value), Convert.ToDouble(longitudeTB.Value), "Pending"); 

寫在不同的Web窗體上HTML端,

這是我的代碼,

<asp:Content ID="Content1" ContentPlaceHolderID="MainContent" Runat="Server"> 
<p class="text-success" runat="server" id="loginDetails"></p> 
<p class="text-danger" runat="server" id="logoutDetails"></p> 

<div> 
    <asp:HiddenField runat="server" ID="latitudeTB"/> 
    <asp:HiddenField runat="server" ID="longitudeTB"/> 

    <script type="text/javascript"> 
    //GeoLocation Retrieval 
    var geo = document.getElementById("geolocationValue"); 
    function getLocation() { 
     if (navigator.geolocation) { 
      navigator.geolocation.getCurrentPosition(function (position) { 
       var lat = position.coords.latitude; 
       var lng = position.coords.longitude; 
       document.getElementById("<%=latitudeTB.ClientID %>").value = lat; 
       document.getElementById("<%=longitudeTB.ClientID %>").value = lng; 
      }); 

     } else { 
      geo.innerHTML = "Geolocation is not supported by this browser."; 
     } 
    } 
    function call() { 
     getLocation(); 
    } 

    /*document.onreadystatechange = function (e) { 
     if (document.readyState === 'complete') { 
      //dom is ready, window.onload fires later 
      call(); 
     } 
    };*/ 

    /*$(window).load(function() { 
     call(); 
    });*/ 
</script> 

</div> 

而且在Page_Load中我用這個的RegisterClientScriptBlock實際上導致爲H iddenFields被分別填充緯度和經度,然後被保存到從其他Web窗體我上面貼同樣的方法數據庫,

ScriptManager.RegisterClientScriptBlock(this, GetType(), "StartGeo", "call();", true); 

    Debug.WriteLine(latitudeTB.Value); 
    Debug.WriteLine(longitudeTB.Value); 

我想我經度和緯度值是存在的當頁面加載,但似乎無濟於事。我檢查了HiddenFields的值,但我什麼也沒有得到。這意味着HiddenFields是空的還是空的?

請問我能做些什麼來解決這個問題?

請感謝您的任何幫助..謝謝!

+0

有幾個可能的問題。 1)你的代碼不調用getLocation()。 2)它實際上並不是將值寫入隱藏字段,或者3)當您檢入Page_Load時,它們不會被保存。 只是爲了仔細檢查...你確定你的getLocation()方法被調用嗎?而且它實際上是在寫價值? –

+0

這是否有幫助:https://stackoverflow.com/questions/21758137/asp-net-get-hidden-value-from-code-behind-in-page-load-function –

+0

我沒有太多的JavaScript經驗或我有從來沒有學過它..我真的不能告訴不幸..我怎麼測試?我實際上只是使用Debug.WriteLine來檢查HiddenFields是否有值.. – domster

回答

1

你已經把錯誤的變量。將「經度」改爲「經度」& lng。檢查下面的編輯代碼。

var lat = position.coords.latitude; 
var lng = position.coords.longitude; 
document.getElementById("<%=latitudeTB.ClientID %>").value = lat; 
document.getElementById("<%=longitudeTB.ClientID %>").value = lng; 

你以前的代碼是。

var lat = position.coords.latitude; 
var lng = position.coords.longitude; 
document.getElementById("<%=latitudeTB.ClientID %>").value = latitude; 
document.getElementById("<%=longitudeTB.ClientID %>").value = longitude; 
+0

謝謝桑尼爾,沒有意識到..但無濟於事在我的Debug中爲2 HiddenFields仍然會得到空值。 – domster

+0

嗨@domster可以在代碼片段var lat = position.coords.latitude下面放置一個警告(lat +「,」+ lng) var lng = position.coords.longitude;只是爲了檢查它是否正在調用navigator.geolocation。getCurrentPosition()函數 – Sanil

+0

@domster navigator.geolocation.getCurrentPosition將只能通過https工作。但它會爲localhost工作。如果您使用Chrome瀏覽器並且無法正常工作,請嘗試在Firefox瀏覽器中運行您的代碼。它一定會奏效。但請記住,地理位置是基於HTML5的API,因此請將您的瀏覽器更新爲最新版本。 – Sanil