简体   繁体   English

在asp.net codebehind中访问javascript变量

[英]access javascript variable in asp.net codebehind

I need to store the geolocation(lat,long) when on page_load to a db table. 我需要在page_load上将地理位置(lat,long)存储到db表。

For which I use this script. 我使用这个脚本。

<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>

here is my code behind public partial class getLocation : System.Web.UI.Page 这是我公共部分类getLocation背后的代码:System.Web.UI.Page

{ protected void Page_Load(object sender, EventArgs e) { {protected void Page_Load(object sender,EventArgs e){

    lblloc.Visible = true;

    lblloc.Text = hdnLocation.Value;
    txtloc.Text = hdnLocation.Value;

}

when I run the page I get values in hidden field when I inspect in browser. 当我运行页面时,我在浏览器中检查时会在隐藏字段中获取值。

在此输入图像描述

But can't access the hidden field values in code behind. 但无法访问后面的代码中的隐藏字段值。 Response.Write comes blank and lblloc.text comes null. Response.Write为空,lblloc.text为空。

What could be the problem. 可能是什么问题呢。

In order for your current jQuery selector to work, your HTML element needs to have the ClientIDMode property set to "Static". 为了使您当前的jQuery选择器正常工作,您的HTML元素需要将ClientIDMode属性设置为“Static”。

With server elements in .NET, the server id and the client id are two very different things. 对于.NET中的服务器元素,服务器ID和客户端ID是两个非常不同的东西。 This ensures they are the same for this element. 这可确保它们与此元素相同。

<asp:HiddenField ID="hdnLocation" runat="server" ClientIDMode="Static" />

This should work: 这应该工作:

HTML HTML

<script type="text/javascript">
function abc()
{
  var str="value";
  document.getElementById("Hidden1").value=str;
}


</script>
<body>
    <form id="form1" runat="server">
    <div>
        <input id="Hidden1" type="hidden" runat="server" />
        <asp:Button ID="Button1" runat="server" OnClientClick="abc()"  Text="Button"
            onclick="Button1_Click" />
    </div>
    </form>
</body>

Code behind 代码背后

Response.Write(Hidden1.Value);

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

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