从javascript调用.ascx的vb.net函数,它也在.ascx中



我有一个vb.net函数,我想从javascript调用两者都在。ascx。在这段代码中,我使用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>

您正在尝试使用的被称为PageMethods -然而,页面方法代码(静态方法)必须是某些页面(aspx)代码的一部分。您不能将页面方法代码放在用户控件(ascx)代码后面。
我怀疑这个限制的原因是以.ascx结尾的url不是为客户端使用的(你会得到404)——它们纯粹是为了服务器端操作。

对于您来说,简单的解决方案是将页面(aspx)代码中的相关方法移到后面,并更改url,例如"Schart.aspx/loadgraph"。您可以始终将所有代码保存在ascx文件中,并从虚拟页面方法代码中调用它,从而将相关的UI和代码保存在ascx文件中。

最新更新