通过javascript将输入(框)中的数据添加到sql db

时间:2014-12-02 14:22:01

标签: javascript c# google-maps

这是我用于在输入(框)"纬度"中写入数据的代码。和经度"使用javascript中的c#代码访问数据库。

我想要的只是按下按钮时插入数据。此代码仅在初始化时插入一次数据。在按钮上没有效果。

<%@ 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进行编码的经验。 任何帮助将受到高度赞赏。

1 个答案:

答案 0 :(得分:1)

JavaScript运行客户端,不能拥有c#&#34; embedded&#34;为此,我要将c#代码移动到自己的页面并使用AJAX向服务器发送/接收数据并相应地执行c#代码