我試圖使用地理定位獲取值(緯度,經度)。我嘗試在另一個頁面中使用相同的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是空的還是空的?
請問我能做些什麼來解決這個問題?
請感謝您的任何幫助..謝謝!
有幾個可能的問題。 1)你的代碼不調用getLocation()。 2)它實際上並不是將值寫入隱藏字段,或者3)當您檢入Page_Load時,它們不會被保存。 只是爲了仔細檢查...你確定你的getLocation()方法被調用嗎?而且它實際上是在寫價值? –
這是否有幫助:https://stackoverflow.com/questions/21758137/asp-net-get-hidden-value-from-code-behind-in-page-load-function –
我沒有太多的JavaScript經驗或我有從來沒有學過它..我真的不能告訴不幸..我怎麼測試?我實際上只是使用Debug.WriteLine來檢查HiddenFields是否有值.. – domster