[英]pass variable from javascript to ASP.NET behind code VB.Net
我试图显示并获取网站访问者位置的坐标,以将其存储在数据库中,我已经在javascript中使用google maps API进行了简单的试用。 现在,我需要将javascript中的坐标值传递给我的代码,以进行所需的计算并将其存储在数据库中。
这是定位器代码
<script>
function success(position) {
var s = document.querySelector('#status');
if (s.className == 'success') {
return;
}
s.innerHTML = "Gotcha!";
s.className = 'success';
var mapcanvas = document.createElement('div');
mapcanvas.id = 'mapcanvas';
mapcanvas.style.height = '400px';
mapcanvas.style.width = '560px';
document.querySelector('article').appendChild(mapcanvas);
var latlng = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);
var myOptions = {
zoom: 15,
center: latlng,
mapTypeControl: false,
navigationControlOptions: { style: google.maps.NavigationControlStyle.SMALL },
mapTypeId: google.maps.MapTypeId.ROADMAP
};
var map = new google.maps.Map(document.getElementById("mapcanvas"), myOptions);
var marker = new google.maps.Marker({
position: latlng,
map: map,
title: "You are here! (at least within a " + position.coords.accuracy + " meter radius)"
});
}
function error(msg) {
var s = document.querySelector('#status');
s.innerHTML = typeof msg == 'string' ? msg : "failed";
s.className = 'fail';
}
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(success, error);
} else {
error('not supported');
}
//the problem is here!
var getter = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);
document.getElementById('<%=send_laty.ClientID%>') = getter.lat;
document.getElementById('<%=send_lony.ClientID%>') = getter.lng;
alert(getter)
</script>
我创建了2个隐藏字段来将变量从javascript传递到后面的代码
<asp:HiddenField ID="send_laty" runat="server" Value="" />
<asp:HiddenField ID="send_lony" runat="server" Value="" />
然后像这样在aspx.vb页面的page_load中测试它们
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim output As String
output = send_laty.Value + " ___ " + send_lony.Value
MsgBox(output)
End Sub
但是输出是_______,这意味着未传递变量!
我需要提示为什么它没有传递值!
提前致谢,
您需要将结果分配给value
属性。
document.getElementById('<%=send_laty.ClientID%>').value = getter.lat;
document.getElementById('<%=send_lony.ClientID%>').value = getter.lng;
就像mga911所说的
aspx.vb页面的Page_Load在javascript之前执行,因此该值将始终为空白。 您需要回传值,以便将数据发送回服务器
而不是使用
dim x as string = send_lony.Value
和
document.getElementById('<%= send_laty.ClientID%>')
我用了
dim x as string = Request.Form("send_lony")
与
document.getElementById('<%=send_laty.ClientID%>').value
感谢您的帮助:)
要使用动态控件将值从javascript传递到后面的页面,请执行以下操作:
In aspx page header content,
<script language="javascript" type="text/javascript">
function executedelete(txt) {
alert(txt);
document.getElementById('<%=ctlHiddenField.ClientID%>').value = txt
__doPostBack("<%= deletetherecord.UniqueID %>", "");
}
</script>
在aspx页面正文内容中,
<asp:Button ID="deletetherecord" runat="server" style="display:none" />
<asp:HiddenField ID="ctlHiddenField" runat="server" />
在后面的页面中
Protected Sub deletetherecord_Click(sender As Object, e As System.EventArgs) Handles deletetherecord.Click
Dim txt As String = ctlHiddenField.Value
'do processing here
End Sub
在Page Behind函数中,创建动态控件,
SomeLabel.Text &= "<input id=""deletebutton"" type=""button"" value=""Delete"" onClick=""executedelete('" & gv.Rows(row).Cells(5).Text & "');"" />"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.