首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从.ascx调用ascx.cs函数

从.ascx调用ascx.cs函数
EN

Stack Overflow用户
提问于 2014-07-24 03:35:40
回答 2查看 580关注 0票数 0

这是我的示例代码。我正在尝试调用calculateCoordinates()方法,它位于一个.ascx文件的javascript中,尝试过OnClick,但它不起作用,现在我正在尝试OnClientClick,也不起作用。

代码语言:javascript
复制
 //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()方法是经过测试的,它的效果很好,所以我知道它是有效的。

EN

回答 2

Stack Overflow用户

发布于 2014-07-24 22:35:28

像这样修好了。

代码语言:javascript
复制
 <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;

 }
票数 0
EN

Stack Overflow用户

发布于 2014-07-24 22:38:25

尝试使用onclick事件而不是onClientClick。希望这能有所帮助!

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/24919397

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档