从客户端调用后台静态方法

1.Ajax Library方式

C#代码:

1
2
3
4
5
[WebMethod]
public  static  DateTime GetCurrentTime( string  str)
{
     return  DateTime.Now;
}

JS代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
< form  id="form1" runat="server">
< script  language=javascript type="text/javascript">
     function GetCurrentTime1() {
         PageMethods.GetCurrentTime('NewEgg ajax training', CheckIsSuccess);
     }
     function CheckIsSuccess(result) {
         alert(result);
     }
  </ script >
< div >
     < asp:ScriptManager  ID="ScriptManager1" runat="server" EnablePageMethods="true">
     </ asp:ScriptManager >
     < input  id="Button1" type="button" value="客户端控件调用服务器端的方法" onclick="GetCurrentTime1()" />
</ div >
</ form >

说明:

C#方法必须加 "[WebMethod]"

前台页面必须使用引用 服务器控件

<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true"> 
</asp:ScriptManager>

调用方法: PageMethods.后台方法名(参数[,参数....], 成功后调用的方法名);

Ajax Library

image

image

2. jQuery方式

C#代码:

1
2
3
4
5
[WebMethod]
public  static  string  ABC( string  ABC)
{
     return  ABC;
}

JS代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
$().ready(
         function () {
             $( "#AjaxDemo" ).click( function () {
                 $.ajax({
                     type: "POST" ,
                     url: "Default.aspx/ABC" ,
                     data: "{'ABC':'test'}" ,
                     contentType: "application/json; charset=utf-8" ,
                     success: function (msg) {alert(msg); }
                 })
             })
         }
     )

说明: 必须引用jQuery库文件.

3. 还有一种好像是要引用AJAX.dll文件的. 在后台注册前台方法. 这个好像在.net2.0的时候用的比较多. 具体没仔细研究.

还望有其他更简单方法的同学互相交流下~

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值