利用ScriptManager实现Javascript调用WebService中的方法

前台调用后台方法,或者后台调用前台方法。ScriptManager实现

开发过程中,我们总想在前台直接调用后台的method,其原因无非是想控制页面刷新,改善用户体验。可以达到这个目的的方法有很多:Ajax应该是用的最多的情况.这里要说的是利用Javascript和webservice的交互来达到目的。
1.当然要建立WebService(简便起见,WebService就建立在当前方案中):


None.gifusing System;
None.gif
using System.Web;
None.gif
using System.Collections;
None.gif
using System.Web.Services;
None.gif
using System.Web.Services.Protocols;
None.gif
None.gif
ExpandedBlockStart.gifContractedBlock.gif
/**//// <summary>
InBlock.gif
/// WebService 的摘要描述
InBlock.gif
/// </summary>
ExpandedBlockEnd.gif
///
None.gif[System.Web.Script.Services.ScriptService]  //注意要实现目的,这就必不可少
None.gif

None.gif[WebService(Namespace
= "http://tempuri.org/")]
None.gif[WebServiceBinding(ConformsTo
= WsiProfiles.BasicProfile1_1)]
ExpandedBlockStart.gif
public class WebService : System.Web.Services.WebService ...{
InBlock.gif
ExpandedSubBlockStart.gif    
public WebService () ...{
InBlock.gif
InBlock.gif        
//如果使用設計的元件,請取消註解下行程式碼
InBlock.gif        
//InitializeComponent();
ExpandedSubBlockEnd.gif
     }
InBlock.gif
InBlock.gif     [WebMethod]
InBlock.gif    
public string sayhi(string name,string age)
ExpandedSubBlockStart.gif    
...{
InBlock.gif        
//code here
InBlock.gif
        return "Hello,this is "+name+",i am"+age ;
ExpandedSubBlockEnd.gif     }

InBlock.gif    
ExpandedBlockEnd.gif}
2.新建页面ajaxWebService.aspx,建立前台代码,其中要用ScriptManager对service进行注册。先看代码:


ExpandedBlockStart.gif<%...@ Page Language="C#" AutoEventWireup="true" CodeFile="ajaxWebService.aspx.cs" Inherits="ajaxWebService" %>
None.gif
ExpandedBlockStart.gif
<%...@ Register Assembly="System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
ExpandedBlockEnd.gif     Namespace
="System.Web.UI" TagPrefix="asp" %>
None.gif
None.gif
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
None.gif
None.gif
<html xmlns="http://www.w3.org/1999/xhtml" >
None.gif
<head runat="server">
None.gif    
<title>未命名頁面</title>
ExpandedBlockStart.gif   
<script type="text/javascript" >...
InBlock.gif   
InBlock.gif    
function check()
ExpandedSubBlockStart.gif    
...{
InBlock.gif      
var name=document.getElementById("txtname").value;
InBlock.gif      
var age=document.getElementById("txtage").value;
InBlock.gif      
InBlock.gif      
//最后一个参数为回调函数名称
InBlock.gif
       WebService.sayhi(name,age,updatepage);
ExpandedSubBlockEnd.gif     }

InBlock.gif    
InBlock.gif    
//其中result即返回的结果
InBlock.gif
    function updatepage(result)
ExpandedSubBlockStart.gif    
...{
InBlock.gif       alert(result);
ExpandedSubBlockEnd.gif     }

ExpandedBlockEnd.gif   
</script>
None.gif
</head>
None.gif
<body>
None.gif    
<form id="form1" runat="server">
None.gif    
<div>
None.gif        
<asp:ScriptManager ID="ScriptManager1" runat="server">
None.gif          
<Services >
None.gif            
<asp:ServiceReference Path ="~/WebService.asmx" />
None.gif          
</Services>
None.gif        
</asp:ScriptManager>
None.gif        
<input id="txtname" name="txtname" type="text" />
None.gif        
<input id="txtage" name="txtage" type="text" />
None.gif        
<input id="Button1" type="button" value="button" onclick ="check()" />
None.gif    
</div>
None.gif    
</form>
None.gif
</body>
None.gif
</html>

3.说明:
除了上述两点需要注意外,还有一点需要强调一下:
调用的方法很简单,[WebServiceName].[MethodName](...),其中的参数列中最后一项应该为回调函数名称。而回调函数的参数既是回传的结果。见上例。

使用这些ajax控件,sys未定义,则要在web.config的 <system.web>节点下面添加以下语句:

   <httpHandlers>
      <remove verb="*" path="*.asmx"/>
      <add verb="*" path="*.asmx" validate="false" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
      <add verb="*" path="*_AppService.axd" validate="false" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
      <add verb="GET,HEAD" path="ScriptResource.axd" type="System.Web.Handlers.ScriptResourceHandler, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" validate="false"/>
    </httpHandlers>

转载于:https://www.cnblogs.com/dannyqiu/articles/1650970.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值