當前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
JS调用CS里的方法:PageMethods
生活随笔
收集整理的這篇文章主要介紹了
JS调用CS里的方法:PageMethods
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
想要在頁面里JS代碼里onclick去調(diào)用后臺文件中的一個方法,搞了半天,才弄懂怎么做。
原來是通過PageMethods來實現(xiàn)的。
?
舉個列子:?
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方法來實現(xiàn)JS調(diào)用CS,必須注意一下幾點:
【1】靜態(tài)的方法
?????????public?static
?
【2】需要在cs方法上加上:
???????? [System.Web.Services.WebMethod]
?
【3】需要自定義一個函數(shù)接受結果
??????? function xxx(result)
??????? {
??????? alert(result);
??????? }
?
【4】ScriptManager 必須設置成 ?EnablePageMethods="true"
轉(zhuǎn)載于:https://www.cnblogs.com/juxiaoqi/archive/2008/08/21/1273059.html
總結
以上是生活随笔為你收集整理的JS调用CS里的方法:PageMethods的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 利用扩展欧几里得算法编程求逆元
- 下一篇: 关于IE无法打开flex程序的问题