来自.ascx的Callin ascx.cs函数

时间:2014-07-23 19:35:40

标签: c# javascript 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()经过测试,就像魅力一样,所以我知道它有效。

2 个答案:

答案 0 :(得分:0)

像这样修好了。

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

 }

答案 1 :(得分:0)

尝试使用onclick事件而不是onClientClick。 愿这会有所帮助!!!