http://www.cnblogs.com/juxiaoqi/archive/2008/08/21/1273059.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 >
<! 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 " ;
}
}
{
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"
本文介绍如何在JavaScript中调用ASP.NET的后台C#方法。需确保方法为静态且标记@WebMethod特性,同时客户端需定义回调函数处理返回结果,并配置ScriptManager启用页面方法。
6023

被折叠的 条评论
为什么被折叠?



