通过javascript将Input(box)中的数据添加到sql db
Add data in Input(box) to sql db through javascript
这是我的代码,我正在使用JavaScript中的C#代码将input(box)"纬度"和"longitude"中的数据写入数据库。
我想要的只是每当按下按钮时插入数据。此代码仅在初始化时插入一次数据。按钮单击时不会产生任何影响。
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="Markers.WebForm1" %>
<!DOCTYPE html>
<html>
<head>
<style type="text/css">
#google_map {width: 550px; height: 450px;margin-top:50px;margin-left:auto;margin-right:auto;}
</style>
<script type="text/javascript"
src="https://maps.googleapis.com/maps/api/js?v=3.exp">
</script>
<script type="text/javascript">
var mapCenter = new google.maps.LatLng(33.650077, 73.034054); //Google map Coordinates
var map
var marker
var marker1
function initialize() //function initializes Google map
{
var googleMapOptions =
{
center: mapCenter, // map center
zoom: 15, //zoom level, 0 = earth view to higher value
panControl: true, //enable pan Control
zoomControl: true, //enable zoom control
zoomControlOptions: {
style: google.maps.ZoomControlStyle.SMALL //zoom control size
},
scaleControl: true, // enable scale control
mapTypeId: google.maps.MapTypeId.ROADMAP // google map type
};
map = new google.maps.Map(document.getElementById("google_map"), googleMapOptions);
marker = new google.maps.Marker({
position: mapCenter,
map: map,
draggable: true,
animation: google.maps.Animation.DROP,
title: "This a new marker!",
icon: "http://maps.google.com/mapfiles/ms/micons/blue.png"
});
marker1 = new google.maps.Marker({
position: mapCenter,
map: map,
draggable: true,
animation: google.maps.Animation.DROP,
title: "This a new marker!",
icon: "http://maps.google.com/mapfiles/ms/micons/blue.png"
});
google.maps.event.addListener(marker, 'dragend', function (evt) {
document.getElementById('latitude').value = evt.latLng.lat();
document.getElementById('longitude').value = evt.latLng.lng();
});
google.maps.event.addListener(marker1, 'dragend', function (evt) {
document.getElementById('latitude1').value = evt.latLng.lat();
document.getElementById('longitude1').value = evt.latLng.lng();
});
}
function addMyMarker() { //function that will add markers on button click
<% Markers.DataClasses1DataContext db = new Markers.DataClasses1DataContext();%>
<% Markers.LocationDataPacket l = new Markers.LocationDataPacket(); %>
<% l.cellPhoneGPSData = 1; %>
<% DateTime dt = DateTime.Now ; %>
<% l.Lat = Convert.ToDouble(latitude.Value.ToString());%>
<% l.time = dt; %>
<% l.Long = Convert.ToDouble(longitude.Value.ToString()); %>
<% db.LocationDataPackets.InsertOnSubmit(l); %>
<% save(db); %>
}
</script>
</head>
<body onLoad="initialize()">
<div id="google_map" ></div><button id="drop" onClick="addMyMarker()">Drop Markers</button>
<input id="latitude" runat="server" value="3.33" />
<input id="longitude" type="text" runat="server" value="3.33" />
<input id="latitude1" type="text" value="3.33"/>
<input id="longitude1" type="text" value="3.33"/>
</body>
</html>
我没有用javascript编码的经验。任何帮助将不胜感激。
JavaScript 运行客户端,不能将 c# "嵌入"到其中,为此,我将 c# 代码移动到它自己的页面并使用 AJAX 向服务器发送/接收数据并相应地执行 C# 代码
相关文章:
- javascript函数将数据添加到屏幕,但随后被另一个函数覆盖
- 将嵌套对象数据添加到窗体中
- 挖空.js - 向服务器端发送的数据添加属性
- 如何将数据添加到json的子节点
- 无法将 json 数据添加到 pyjade 中的数据属性
- 通过变量将数据添加到图形中
- 预先构建的GUI,用于上传.csv并将数据添加到mysql数据库
- Fullcalendar-我们可以将自定义数据添加到我们的活动Json数据中吗
- 将多个csv文件中的时间戳数据添加到highchart中
- 将文本框中的数据添加到表Javascript、HTML中
- 无法使用asp.net mvc将数据添加到数据库中
- 用javascript将数据添加到对象数组中
- 我该如何将自己的数据添加到响应迅速的美国各州地图中
- 在$.ajax调用中将附加数据添加到jQuery包装的对象数据中
- 将新的系列[0]数据添加到现有的高位图表中
- 为高图实时数据添加一个附加点
- 如何在D3中为用于显示饼图的数据添加键
- CSS 为 JSON 数据添加两行字符
- 将数据添加到 ko.observablearray 的末尾
- 向数据表中的 json 数据添加文本