通过javascript将Input(box)中的数据添加到sql db

Add data in Input(box) to sql db through javascript

本文关键字:数据 添加 db sql javascript Input box 通过      更新时间:2023-09-26

这是我的代码,我正在使用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# 代码