您的位置:首页 > Web前端 > JavaScript

JS调用CS里的方法:PageMethods

2009-08-14 16:39 267 查看
/article/5298673.html

举个列子:

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";
}
}

通过PageMethods方法来实现JS调用CS,必须注意一下几点:

【1】静态的方法

public static

【2】需要在cs方法上加上:

[System.Web.Services.WebMethod]

【3】需要自定义一个函数接受结果

function xxx(result)
{
alert(result);
}

【4】ScriptManager 必须设置成 EnablePageMethods="true"

Tag标签: ASP.NET,PageMethods,JS调CS
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: