• 在JS中调用CS里的方法(PageMethods)


    在JS中调用CS里的方法(PageMethods)

    最近一直在看别人写好的一个项目的源代码,感觉好多东西都是之前没有接触过的。今天在代码中看到了一个类PageMethods,于是就在想,这个类是系统类还是自定义的呢?后面再网上百度了一下,原来PageMethods是用来在JS里调用CS里写好的方法。感觉这种方法的功能特别强调,所以在这里记录一下,也希望对大家有所帮助。

    实例:

    Default.aspx 代码

    
    
    1.  
      <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
    2.  
       
    3.  
       
    4.  
       
    5.  
      <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    6.  
       
    7.  
       
    8.  
       
    9.  
      <html xmlns="http://www.w3.org/1999/xhtml">
    10.  
       
    11.  
      <head runat="server">
    12.  
       
    13.  
      <title>无标题页</title>
    14.  
       
    15.  
      <script type="text/javascript" language="javascript">
    16.  
       
    17.  
      <!--
    18.  
       
    19.  
      function minbzdm()
    20.  
       
    21.  
      {
    22.  
       
    23.  
      PageMethods.OK(xxx);
    24.  
       
    25.  
      }
    26.  
       
    27.  
      function xxx(result)
    28.  
       
    29.  
      {
    30.  
       
    31.  
      alert(result);
    32.  
       
    33.  
      }
    34.  
       
    35.  
      //-->
    36.  
       
    37.  
      </script>
    38.  
       
    39.  
      </head>
    40.  
       
    41.  
      <body>
    42.  
       
    43.  
      <form id="form1" runat="server">
    44.  
       
    45.  
      <asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true">
    46.  
       
    47.  
      </asp:ScriptManager>
    48.  
       
    49.  
      <div>
    50.  
       
    51.  
      <input type='button' value='删除' onclick='minbzdm()' />
    52.  
       
    53.  
      </div>
    54.  
       
    55.  
      </form>
    56.  
       
    57.  
      </body>
    58.  
       
    59.  
      </html>


    
    

    Default.aspx.cs的代码

    1.  
      public partial class _Default : System.Web.UI.Page
    2.  
       
    3.  
      {
    4.  
       
    5.  
      protected void Page_Load(object sender, EventArgs e)
    6.  
       
    7.  
      {
    8.  
       
    9.  
      }
    10.  
       
    11.  
       
    12.  
       
    13.  
      [System.Web.Services.WebMethod]
    14.  
       
    15.  
      public static string OK()
    16.  
       
    17.  
      {
    18.  
       
    19.  
      return "OK";
    20.  
       
    21.  
      }
    22.  
       
    23.  
  • 相关阅读:
    细节决定成败
    关于结构体大小一篇很详细的文章
    Mysql Innodb cluster集群搭建
    Oracle:imp导入dmp文件
    alert弹出窗口,点击确认后关闭页面
    CAS总结之Ticket篇
    web.xml文件的作用
    单点登陆(SSO)
    session和cookie的区别
    oracle问题之数据库恢复(三)
  • 原文地址:https://www.cnblogs.com/Jeely/p/10772380.html
Copyright © 2020-2023  润新知