下面的代码出了什么问题?

时间:2016-05-07 17:40:42

标签: javascript asp.net geolocation

string str = "<script type='text/javascript'>" +
                 "var lat=lon=-1;" +
                 "getLocation();" +
             "function getLocation() {" +
                 "if (navigator.geolocation) {" +
                     "navigator.geolocation.getCurrentPosition(showPosition);" +
                 "}" +
              "}" +
             "function showPosition(position) {" +
                 "lat=position.coords.latitude;" +
                 "lon=position.coords.longitude;" +
                 "document.getElementById('<%=ltrLat.ClientID%>').innerHTML=lat;"+
                 "document.getElementById('<%=ltrLon.ClientID%>').innerHTML= lon;"+
                 "var x= document.getElementById('<%=ltrLat.ClientID%>').innerHTML;"+
                 "var y= document.getElementById('<%=ltrLon.ClientID%>').innerHTML;" +
                 "alert('lat='+x+'lon='+ y);" +
             "}" +
             "</script>";
              ClientScriptManager cs = this.ClientScript;
              cs.RegisterStartupScript(this.GetType(),"xyz", str);

ltrLat和ltrLon是客户端的asp标签。 此外,标签不会显示 我没有得到警报。

2 个答案:

答案 0 :(得分:0)

我也试过这样的东西,但我得到的是.net字符串不会让你编写脚本所以我建议你在.aspx中嵌入脚本并在.cs中定义全局变量然后在脚本中使用这些变量

word

答案 1 :(得分:0)

如果要使用Ajax,请在.aspx中添加以下代码 请记住,您必须创建一个web方法来处理.cs文件中的ajax请求 的的.aspx

     google.maps.event.addListener(marker,'dragend',function(evt){

                    var lati = evt.latLng.lat();
                    var longi = evt.latLng.lng();
                    var data1 = JSON.stringify({ latitude: lati, longitude: longi, locationId: this.id });

                    Updatelocations(data1);
                });


 function Updatelocations(data1) {

            $.ajax({
                type: "POST",
                url: window.location.pathname + '/UpdateLoc',
                data: data1,
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function () {
                    alert("Location Updated..!");
                },
                error: function (xhr, status, error) {
                    alert(xhr.status);
                    alert(xhr.responseText);
                    alert(data1);
                }
            });
        }
</script>

<强>的.cs

[WebMethod]
    public static void UpdateLoc(double latitude, double longitude, long locationId)
    {
        double lti = Convert.ToDouble(latitude);
        double lngi = Convert.ToDouble(longitude);
        long locationid= Convert.ToInt64(locationId);
        DACLocation.UpdateLocation(lti, lngi, locationId);
    }

我希望你能理解