简体   繁体   English

从.ascx中的javascript调用.ascx的vb.net函数

[英]calling vb.net function of .ascx from javascript which is also in .ascx

i have a vb.net function which i want to call from javascript both are in .ascx. 我有一个我想从javascript调用的vb.net函数,都在.ascx中。 In this code i am using a jquery to popup a dialog and on click of the button(btnok) on dialog i want to call a function loadgraph() whihch is a vb.net function. 在这段代码中,我使用一个jQuery弹出一个对话框,然后单击对话框上的按钮(btnok),我想调用一个函数loadgraph(),这是一个vb.net函数。

<link href="css/jquery-ui.css" rel="stylesheet" type="text/css" />
<script src="javascript/jquery-1.8.3.js" type="text/javascript"></script>

<script src="javascript/jquery.bgiframe-2.1.2.js" type="text/javascript"></script>
<link href="css/demos.css" rel="stylesheet" type="text/css" />
<script src="javascript/jquery-ui.js" type="text/javascript"></script>


<script>
    // increase the default animation speed to exaggerate the effect
    $.fx.speeds._default = 1000;


    $(function() {
        $( "#element_to_pop_up" ).dialog({
            autoOpen: false,
            show: "blind",
            hide: "explode"
        });

        $( "#button" ).click(function() {
            $( "#element_to_pop_up" ).dialog( "open" );
            return false;
        });

         $( "#btnok" ).click(function(){
          $( "#element_to_pop_up" ).dialog( "close" );
             $.ajax({
  type: "POST",
  url: "Schart.ascx/loadgraph",
  data: "{}",
  contentType: "application/json; charset=utf-8",
  dataType: "json",
  success: function(msg) {
    // Do something interesting here.
    alert("called")
  }
});
          return false;
         });
    });
    </script>

What you are trying to use is known as PageMethods - however, page method code (the static method) has to be part of some page (aspx) code behind. 您尝试使用的方法称为PageMethods-但是,页面方法代码(静态方法)必须是后面某些页面(aspx)代码的一部分。 You cannot place page method code in user control (ascx) code behind. 您不能将页面方法代码放在用户控件(ascx)代码的后面。
I suspect the reason for this limitation is that urls ending with .ascx are not meant for client consumption (you will get 404) - they are purely meant for server side manipulation. 我怀疑出现此限制的原因是,以.ascx结尾的网址不是供客户端使用的(您将获得404)-它们纯粹是供服务器端处理的。

For you, the simple solution is to move the relevant method in the page (aspx) code behind and change the url such as "Schart.aspx/loadgraph" . 对您来说,简单的解决方案是将页面(aspx)代码中的相关方法移到后面,并更改URL,例如"Schart.aspx/loadgraph" You can always keep all the code in ascx file and call it from a dummy page method code and thereby keeping the related UI and code within ascx file. 您始终可以将所有代码保留在ascx文件中,并从虚拟页面方法代码中调用它,从而将相关的UI和代码保留在ascx文件中。

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

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