页面加载服务器端的Coords

时间:2017-01-07 20:49:50

标签: javascript jquery asp.net navigator coords

所以我试图在asp.net页面的页面加载上记录位置坐标。问题是,在页面完全加载之前,我的隐藏字段的值不存在。我试图捕获卸载事件,希望它在渲染之后进一步无效,因为服务器仍然在服务。我只想在页面加载事件中将我的数据写入我的数据库。我不希望用户必须单击按钮来记录数据,因为我能够这样做,但不希望用户需要额外的输入。

获取Coords Code JS:

navigator.geolocation.getCurrentPosition(
    function showPosition(position) {
    $("[id*=hdnLatitude]").val(position.coords.latitude);
    $("[id*=hdnLongitude]").val(position.coords.longitude);
                    },
function (error) {
        }, {
        enableHighAccuracy: true
        , timeout: 5000
       }
    );

在页面加载事件ASP.Net:

Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
   'write to my db
End Sub

所以我知道如何在页面加载时写入数据库,但正如我上面所说,隐藏字段中还没有任何数据。我的问题是如何在页面加载之前获取数据。这似乎不可能,因为它还没有为客户端提供服务,但是我可以强制回页或页面加载时获取这些值,因为它们将被加载吗?我真的不希望页面在用户上闪烁,即加载两次。希望这是有道理的。提前谢谢。

1 个答案:

答案 0 :(得分:0)

所以我决定使用Ajax Timer。我把它设置为6000,它在服务器端激活,并做我需要的。我把它放在更新面板中,它会在不重新加载整个页面的情况下触发。也许不是最好的解决方案,但可以胜任。

<asp:UpdatePanel ID="UpdatePanel1" runat="server">
    <ContentTemplate>
            <asp:Timer ID="tmrRecord" runat="server" Interval="6000">
</asp:Timer>
<asp:Label ID="lblLocation" runat="server"></asp:Label>
    </ContentTemplate>
</asp:UpdatePanel>



Protected Sub tmrRecord_Tick(sender As Object, e As EventArgs) Handles tmrRecord.Tick
    'record coords to database
    tmrRecord.Enabled = False
End Sub
相关问题