<script type="text/javascript">
function getLocation() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(getPostion);
}
}
function getPostion(position) {
var currentLat = position.coords.latitude;
var currentLon = position.coords.longitude;
alert(currentLat);
document.forms[0].lat.value = "123";
alert(document.forms[0].lat.value);
document.forms[1].lon.value = currentLon;
alert(document.forms[1].lon.value);
}
</script>
<input type="hidden" name="lat">
<input type="hidden" name="lon">
<asp:TextBox ID="status" runat="server" CssClass="text" MaxLength="100" Columns="40" TextMode="MultiLine"></asp:TextBox>
<asp:Button ID="textSubmit" runat="server" CssClass="textSubmit" Text="submit" OnClientClick="getLocation()" OnClick="Post" />
protected void Post(object sender, EventArgs e)
{
string latitude = Request.Form["lat"].ToString();
string longitude = Request.Form["lon"].ToString();
string text = status.Text.Trim().ToString();//breakpoint here
string connectionString = WebConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString;
SqlConnection con = new SqlConnection(connectionString);
try
{
con.Open();
SqlCommand cmd = new SqlCommand("INSERT INTO [dbo].[Locations] VALUES('jshmqjw'," + Convert.ToDouble(latitude) + ", " + Convert.ToDouble(longitude) + ",CONVERT(varchar(100), GETDATE(), 20), 'text' )", con);
cmd.ExecuteNonQuery();
}
catch (Exception err)
{
Response.Write("<script>alert('Error reading The database. " + err.Message + "') </script>");
}
finally
{
con.Close();
}
}
我想将变量从 javascript 发送到 C#。我首先使用隐藏字段来存储值,然后在 C# 中使用 request.form["name"] 来获取值。但是在我在一行之后设置断点后,结果发现变量为空。谁能帮忙!~