Html ASP.NET按钮单击可重置标签文本
我正在开发我的第一个web应用程序。我正在尝试获取GPS位置并将其保存以供进一步处理。代码如下:Html ASP.NET按钮单击可重置标签文本,html,asp.net,web,Html,Asp.net,Web,我正在开发我的第一个web应用程序。我正在尝试获取GPS位置并将其保存以供进一步处理。代码如下: <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="CreatePlace.aspx.cs" Inherits="BeeMaster.CreatePlace" %> <!DOCTYPE html> <html style="background-image:url('images/background.
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="CreatePlace.aspx.cs" Inherits="BeeMaster.CreatePlace" %>
<!DOCTYPE html>
<html style="background-image:url('images/background.png'); background-repeat:repeat-x">
<head runat="server">
<title>Create new Place</title>
<link href="CreatePlace.css" rel="stylesheet" type="text/css" />
<style type="text/css">
#taComment {
height: 142px;
width: 339px;
}
</style>
</head>
<body>
<img src="Images/CNP.png" id="logo"/>
<br />
<br />
<br />
<br />
<br />
<br />
<div>
<p id="Comment" style="font-size: large">Click the button to get your position.</p>
<button onclick="getLocation()" style="font-size: xx-large; width: 380px; height: 50px;">
Get current location
</button>
<br />
<asp:Label ID="lblCoordinates" runat="server"></asp:Label><asp:Label ID="lblLat" runat="server"></asp:Label>,<asp:Label ID="lblLon" runat="server"></asp:Label>
<br />
<iframe
height="600"
style="border:0"
id="iFrame">
</iframe>
</div>
<form runat="server">
<br style="font-size: x-large" />
<br style="font-size: x-large" />
<asp:Label ID="lblName" runat="server" Text="Name:" Font-Size="X-Large"></asp:Label>
<br style="font-size: x-large" />
<asp:TextBox ID="tbName" runat="server" Font-Size="X-Large"></asp:TextBox>
<br style="font-size: x-large" />
<asp:Label ID="lblNameComment" runat="server" Text="It is recommended to give a name for the place." Font-Size="Medium"></asp:Label>
<br style="font-size: x-large" />
<br style="font-size: x-large" />
<asp:Label ID="lblComment" runat="server" Text="Comment:" Font-Size="X-Large"></asp:Label>
<br style="font-size: x-large" />
<textarea id="taComment" ></textarea>
<br style="font-size: x-large" />
<asp:Label ID="lblCommentComment" runat="server" Text="Comment specific for the physical place" Font-Size="Medium"></asp:Label>
<br style="font-size: x-large" />
<br style="font-size: x-large" />
<asp:Button ID="btSaveLocation" runat="server" Text="Save current location as new place" OnClick="btSaveLocation_Click" Font-Size="XX-Large" Height="50px" />
</form>
<script hidden="hidden">
var x = document.getElementById("Comment");
function getLocation() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(showPosition, showError);
} else {
x.innerHTML = "Geolocation is not supported by this browser.";
}
}
function showPosition(position) {
var latlon = position.coords.latitude + "," + position.coords.longitude;
document.getElementById("lblCoordinates").textContent += "Your location in Coordinates is: ";
document.getElementById("lblLat").innerHTML = position.coords.latitude;
document.getElementById("lblLon").innerHTML = position.coords.longitude;
document.getElementById("iFrame").src = "https://www.google.com/maps/embed/v1/place?key=<key>&q=" + latlon + "&maptype=satellite";
document.getElementById("iFrame").hidden = "";
}
function showError(error) {
switch (error.code) {
case error.PERMISSION_DENIED:
x.innerHTML = "User denied the request for Geolocation."
break;
case error.POSITION_UNAVAILABLE:
x.innerHTML = "Location information is unavailable."
break;
case error.TIMEOUT:
x.innerHTML = "The request to get user location timed out."
break;
case error.UNKNOWN_ERROR:
x.innerHTML = "An unknown error occurred."
break;
}
}
</script>
</body>
</html>
如您所见,Page_Load函数中没有任何内容,但它似乎仍然以某种方式将标签重置为初始状态,因为当我在运行时调试应用程序并在btSaveLocation_Click函数的开头设置断点时,我看到两个标签的文本已经为空
有人知道这里有什么问题吗
感谢标签的内容不是回发的一部分,因此不会在viewstate中更新。将您的值放在两个隐藏字段中,并使用它们与服务器逻辑通信。我找到了解决方案。实际上有两个问题:
谢谢大家这不是C!不要使用错误的标签!将值放入隐藏字段,因为通过JS设置的隐藏字段值可以在post中访问back@Olaf谁说了关于C的事?C#@好的,我试试看。Thanks@josibu:您添加了C标记!因此,我现在按照您的建议稍微更新了代码。但它仍然不起作用。行
document.getElementById(“tbLat”).innerText=latlon代码>似乎让它崩溃了,即使我在它未被执行后将它放在两行之上。我也尝试过修改textcontent和innerHTML-都是一样的。你应该更改值:document.getElementById(“lblLat”).value=position.coords.latitude;document.getElementById(“lblLon”).value=position.coords.longitude;很抱歉迟了答复。我无法让它工作。不管我做了什么。但是我真正需要的是访问我从CodeBehind(C#)中的JavaScript中获得的值。对不起。您的javascript应该是document.getElementById(“tbLat”).value=position.coords.latitude;document.getElementById(“tbLon”).value=position.coords.longitude;我的问题不是让标签(或文本框或任何东西)显示坐标,而是将值传递给codeBehind,因为在回发时,它会在函数请求标签(或文本框)的值之前将页面和元素初始化为默认值。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data.OleDb;
using System.Data.Odbc;
namespace BeeMaster
{
public partial class CreatePlace : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btSaveLocation_Click(object sender, EventArgs e)
{
string lat = lblLat.Text;
string lon = lblLon.Text;
}
}
}