在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='删除' οnclick='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.  

转载于:https://www.cnblogs.com/Jeely/p/10772380.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值