简体   繁体   中英

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

<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. You cannot place page method code in user control (ascx) code behind.
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.

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

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