简体   繁体   English

下面的代码有什么问题?

[英]what is wrong with the below code?

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 and ltrLon are asp label on client side. ltrLat 和 ltrLon 是客户端的 asp 标签。 moreover the labels are not displayed I do not get the alert.此外没有显示标签我没有收到警报。

i also tried something like this but what i got was that .net strings wont let you write scripts so i would suggest you to embed the script in .aspx and define global variables in .cs then in the script use these variables as我也尝试过类似的东西,但我得到的是 .net 字符串不会让你编写脚本,所以我建议你将脚本嵌入 .aspx 并在 .cs 中定义全局变量,然后在脚本中使用这些变量作为

<script>    
var x= <%= globalXInCS %>
</script>

Add the below code in .aspx if you want to use Ajax keep in mind you have to create a webmethod to handle ajax Requests in .cs file .aspx如果您想使用 Ajax,请在 .aspx 中添加以下代码请记住,您必须在 .cs 文件.aspx 中创建一个 webmethod 来处理 ajax 请求

google.maps.event.addListener(marker, 'dragend', function (evt) { 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 。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);
    }

I hope you will understand well我希望你能很好地理解

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM