简体   繁体   English

从.ascx调用ascx.cs函数

[英]Callin ascx.cs function from .ascx

this is my sample code. 这是我的示例代码。 I'm tryin to call the method calculateCoordinates() , which is inside a javascript on a .ascx file tried OnClick but it won't work and now i'm trying OnClientClick and is not working either. 我正在尝试调用方法calculateCoordinates() ,该方法位于.ascx文件上的javascript中,它尝试过OnClick但它不起作用,现在我正在尝试OnClientClick ,也无法工作。

 //button
 <asp:Button CssClass="button" ID="CalcCoord_Btn" runat="server" Text="Calcular Coordenadas" OnClientClick="javascript: calculateCoordinates();"/>
 //also tried 
 <asp:Button CssClass="button" ID="CalcCoord_Btn" runat="server" Text="Calcular Coordenadas" OnClientClick="calculateCoordinates();"/>
 <asp:Button CssClass="button" ID="CalcCoord_Btn" runat="server" Text="Calcular Coordenadas" OnClientClick="javascript: calculateCoordinates(); return false"/>


 //.ascx javascript 
 <script type="text/javascript">
function calculateCoordinates() {
     alert("calculateCoordinates");
     var AddressLn1 = document.getElementById("AddressLine1");
     var AddressLn2 = document.getElementById("AddressLine2");
     var State = document.getElementById("State");
     var Town = document.getElementById("City");
     var Postcode = document.getElementById("ZipCode");
     var Country = document.getElementById("State");


     var address = AddressLn1.value + ', ';
     address += AddressLn2.value + ', ';
     address += Town.value + ', ';
     address += Postcode.value + ', ';
     address += Country.value;

     // Make REST call to get geocoded information (that is, the coordinates for the address)
     // This will use your call back procedure and return result in JSON format
     var geocodeRequest = "http://dev.virtualearth.net/REST/v1/Locations?query=" + encodeURI(address) + "&output=json&jsonp=GeocodeCallback&key=" + credentials;
     CallRestService(geocodeRequest);
     //return false;

 }

 function CallRestService(request) {
     alert("CallRestService");
     var script = document.createElement("script");
     script.setAttribute("type", "text/javascript");
     script.setAttribute("src", request);
     document.body.appendChild(script);
 }

 function GeocodeCallback(result) {
     alert("GeocodeCallback");
     var Lat = document.getElementById("hdnfldVariableLat");
     var Long = document.getElementById("hdnfldVariableLong");

     if (result &&
            result.resourceSets &&
            result.resourceSets.length > 0 &&
            result.resourceSets[0].resources &&
            result.resourceSets[0].resources.length > 0) {


         Lat.value = result.resourceSets[0].resources[0].point.coordinates[0];
         Long.value = result.resourceSets[0].resources[0].point.coordinates[1];
         var location = new Microsoft.Maps.Location(Lat.value, Long.value),
         pin = new Microsoft.Maps.Pushpin(location, { text: '2', draggable: true }); 
      }
   }
</script>

also tried "return false" at the end of calculateCoordinates() method. calculateCoordinates()方法末尾也尝试了“ return false”。 but it won't even give me one of those alerts to see if the method in fact is being called... i know it's simple but what am i doing wrong? 但是它甚至不会给我任何警报,以查看实际上是否正在调用该方法...我知道这很简单,但是我在做什么错呢? the method calculateCoordinates() was tested and works like a charm so i know it works. 该方法calculateCoordinates()已经过测试,就像一个魅力,所以我知道它的作品。

fixed it like this. 像这样修复它。

 <asp:Button CssClass="button" ID="CalcCoord_Btn" runat="server" Text="Calcular Coordenadas" OnClientClick="javascript: calculateCoordinates();"/>


 var calculateCoordinates = function() {
     alert("calculateCoordinates");
     var AddressLn1 = document.getElementById("AddressLine1");
     var AddressLn2 = document.getElementById("AddressLine2");
     var State = document.getElementById("State");
     var Town = document.getElementById("City");
     var Postcode = document.getElementById("ZipCode");
     var Country = document.getElementById("State");


     var address = AddressLn1.value + ', ';
     address += AddressLn2.value + ', ';
     address += Town.value + ', ';
     address += Postcode.value + ', ';
     address += Country.value;

     // Make REST call to get geocoded information (that is, the coordinates for the address)
     // This will use your call back procedure and return result in JSON format
     var geocodeRequest = "http://dev.virtualearth.net/REST/v1/Locations?query=" + encodeURI(address) + "&output=json&jsonp=GeocodeCallback&key=" + credentials;
     CallRestService(geocodeRequest);
     return false;

 }

try with onclick event instead of onClientClick. 尝试使用onclick事件而不是onClientClick。 May this will help !!! 可能会有所帮助!

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

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