您现在的位置是:首页 > 电脑技术查询 > web开发

在JS中调用CS里的步骤(PageMethods)

编辑:chaxungu时间:2022-10-10 23:24:18分类:web开发

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

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

实例:

Default.aspx 代码

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head runat="server"><title>无标题页</title><script type="text/javascript" language="javascript"><!--function minbzdm(){PageMethods.OK(xxx);}function xxx(result){alert(result);}//--></script></head><body>    <form id="form1" runat="server">    <asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true">    </asp:ScriptManager>    <div>    <input type='button' value='删除' onclick='minbzdm()' />    </div>    </form></body></html>


Default.aspx.cs的代码

public partial class _Default : System.Web.UI.Page{    protected void Page_Load(object sender, EventArgs e)    {    }    [System.Web.Services.WebMethod]    public static string OK()     {        return "OK";    }}