簡體   English   中英

從.ascx調用ascx.cs函數

[英]Callin ascx.cs function from .ascx

這是我的示例代碼。 我正在嘗試調用方法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>

calculateCoordinates()方法末尾也嘗試了“ return false”。 但是它甚至不會給我任何警報,以查看實際上是否正在調用該方法...我知道這很簡單,但是我在做什么錯呢? 該方法calculateCoordinates()已經過測試,就像一個魅力,所以我知道它的作品。

像這樣修復它。

 <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;

 }

嘗試使用onclick事件而不是onClientClick。 可能會有所幫助!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM