简体   繁体   English

asp.net中的javascript未设置隐藏字段值

[英]javascript inside asp.net not setting hidden field value

I am new to Asp.net. 我是Asp.net的新手。 I am trying to get the latitude and longitude using GeoLocation and trying to set the values to the hidden fields on PageLoad. 我试图使用GeoLocation获取纬度和经度,并试图将值设置为PageLoad上的隐藏字段。

Problem is the value of the Hidden Fields always remains empty. 问题是“隐藏字段”的值始终为空。 But please see the alert statement in the JavaScript call, the alert correctly displays both latitude and longitude value. 但是,请参阅JavaScript调用中的警报语句,警报将正确显示纬度和经度值。 How can I populate the hidden fields in the C# code so I can send these values as email when Page Loads. 如何填充C#代码中的隐藏字段,以便在页面加载时可以将这些值作为电子邮件发送。 Please help 请帮忙

aspx code: aspx代码:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <script>
       window.onload = getLocation();
       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) {
            document.getElementById('<%=Latitide.ClientID%>').value = position.coords.latitude;
            document.getElementById('<%=Longitude.ClientID%>').value = position.coords.longitude;
           alert("Latitude: " + position.coords.latitude +
               "<br>Longitude: " + position.coords.longitude);

       }

        </script>
</head>
<body>
    <form id="form1" runat="server">

    <br />
   <asp:HiddenField runat="server" ID="Latitide" ClientIDMode="Static" Value="" />
   <asp:HiddenField runat="server" ID="Longitude" ClientIDMode="Static" Value="" />
    </form>
</body>
</html>

Code behind: 后面的代码:

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

        Response.Write("Latitude " + Latitide.Value + "-Longitude" + Request.Form["Longitude"]);
    }


}

Your server-side code (Page_Load) executes when the page is first requested, at which point those values are empty, as set within ASPX markup (towards the bottom of that file). 您的服务器端代码(Page_Load)在首次请求页面时执行,此时这些值是空的,这是ASPX标记(朝该文件的底部)中设置的。

When that file is then served to the browser, it executes the JavaScript code that you have (onload), at which point values of those hidden fields are set (but you still won't see them within page-source as such). 然后将该文件提供给浏览器后,它将执行(加载)您拥有的JavaScript代码,此时将设置这些隐藏字段的值(但您仍然不会在页面源代码中看到它们)。 If you javascript-alert this: document.getElementById('<%=Longitude.ClientID%>').value ... I think you'll see what you expect. 如果您使用javascript警报,请执行以下操作: document.getElementById('<%=Longitude.ClientID%>').value ...我想您会看到您的期望。

I'd need to know more about the intention of that webpage to provide more guidance. 我需要更多地了解该网页的意图,以提供更多指导。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM