简体   繁体   English

使用ASP.NET和JQuery Mobile的innerHtml

[英]innerHtml with ASP.NET and JQuery Mobile

I'm working on a mobile search, that uses your location to determine a zip code server side, but when trying to set two labels to the latitude and longitude to send to the server, I get an error claiming that the innerHtml is null. 我正在进行移动搜索,使用您的位置来确定邮政编码服务器端,但是当尝试将两个标签设置为纬度和经度以发送到服务器时,我收到一个错误,声称innerHtml为空。 Upon further inspection, the element is null. 进一步检查后,元素为空。 Why could this be? 为什么会这样?

<%@ Page Title="" Language="C#" MasterPageFile="~/Site1.Master" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="Search.Default" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
<script language="javascript" type="text/javascript">
    navigator.geolocation.getCurrentPosition(foundLocation, noLocation);
    var latitude;
    var longitude;
    function foundLocation(position) {
        latitude = position.coords.latitude;
        longitude = position.coords.longitude;
        alert('Your location: ' + latitude + ', ' + longitude);
    }
    function noLocation() {
        //Do something here in the case that no location is found, or user denies access
    }
    function getLocation() {
        document.getElementById("ct100_ContentPlaceHolder1_lblLat").innerHtml = latitude;
        document.getElementById("ct100_ContentPlaceHolder1_lblLong").innerHtml = longitude;
    }
</script>   
<div data-role="page">
    <div data-role="header">
        <h1>Provider Search</h1>
    </div>
    <div data-role="content">
        <asp:Button ID="btnSearch" Text="Search" runat="server" data-role="button" OnClientClick="getLocation()" OnClick="btnSearch_Clicked"></asp:Button>
        <asp:Label ID="lblLat" runat="server"></asp:Label>
        <asp:Label ID="lblLong" runat="server"></asp:Label>
        <p></p> 

    </div>

</div>

</asp:Content>

I'd like to note that everything in this document is working perfectly, except for the setting of the labels. 我想请注意,除了标签的设置外,本文档中的所有内容都完美无缺。

Also, the ID prefix ct100_ContentPlaceHolder1_ is generated by asp.net during runtime, and this can be confirmed by looking at the page source when I am debugging. 此外,ID前缀ct100_ContentPlaceHolder1_是由asp.net在运行时生成的,这可以通过在调试时查看页面源来确认。

Any help? 有帮助吗?

In your getLocation() function could you try doing the following: 在你的getLocation()函数中,你可以尝试执行以下操作:

function getLocation() {
    $('#<%=lblLat.ClientID%>').html(latitude);
    $('#<%=lblLong.ClientID%>').html(latitude);
}

You could also try setting the text property of both labels in a similar manner: 您也可以尝试以类似的方式设置两个标签的text属性:

function getLocation() {
    $('#<%=lblLat.ClientID%>').text(latitude);
    $('#<%=lblLong.ClientID%>').text(latitude);
}

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

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