我需要将page_load上的地理位置(lat,long)存储到db表中。在asp.net代码隐藏中访问javascript变量
我使用这个脚本。
<body onload="getLocation()">
<p>Click the button to get your coordinates.</p>
<%--<button onclick="getLocation()">Try It</button>--%>
<p id="demo"></p>
<form id="form1" runat="server">
<div>
<p>
<asp:HiddenField ID="hdnLocation" ClientIDMode="Static" runat="server" />
<asp:Label ID="lblloc" runat="server"></asp:Label>
<asp:TextBox ID="txtloc" runat="server"></asp:TextBox>
<asp:Button ID="btnLoc" text="submit" runat="server" OnClick="btnLoc_Click" />
</p>
</div>
</form>
<script>
var x = document.getElementById("demo");
function getLocation() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(showPosition);
} else {
x.innerHTML = "Geolocation is not supported by this browser.";
}
}
function showPosition(position) {
x.innerHTML = "Latitude: " + position.coords.latitude +
"<br>Longitude: " + position.coords.longitude;
$("[id*=hdnLocation]").val(position.coords.latitude + ' ' + position.coords.longitude);
// $("[id*=btnLoc]").trigger("click");
}
</script>
</body>
</html>
这里是背后 公共部分类的getLocation我的代码:System.Web.UI.Page
{ 保护无效的Page_Load(对象发件人,EventArgs的){
lblloc.Visible = true;
lblloc.Text = hdnLocation.Value;
txtloc.Text = hdnLocation.Value;
}
当我运行该页面时,当我在浏览器中检查时,我在隐藏字段中获取值。
但在代码无法访问的隐藏字段值后面。 Response.Write为空,lblloc.text为空。
可能是什么问题。
这并不工作 –