简体   繁体   中英

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.

 //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. 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.

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. May this will help !!!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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