此文源于在论坛里我所提问的关于通过采用atlas来实现用户名检测问题。
由于项目中加入的asp.net ajax构架,可检测用户注册时无法通过updatepanel来实现无刷新检测是否重名的功能。网上找不到太好的实现方法,又不想再引入其他ajax构架,于是在论坛提问等大大侠帮助。终于在一位高手的提示下看到了以前从未涉及的web service,于是继续查大量网页,终于实现了简单的操作功能。目前我知之甚少,只能停留在返回字符串的层面,不过其他应该也大同小异,努力ing。下面说下编写过程,希望对如我一样的菜鸟有所帮助。
WebService.aspx中代码:
<
script language
=
"
javascript
"
>
function sayhello()
... {
SayHello.HelloWorld(OnRequestComplete);
}
function saygoodbye()
... {
SayHello.GoodBye(OnRequestComplete);
}
function OnRequestComplete(result)
... {
document.getElementById("Text2").innerText=result;
}
</ script >
< form id = " form1 " runat = " server " >
< div >
& nbsp; < asp:ScriptManager ID = " ScriptManager1 " runat = " server " >
< Services >
< asp:ServiceReference Path = " SayHello.asmx " />
</ Services >
</ asp:ScriptManager >
< input id = " Text1 " type = " text " onfocus = " sayhello() " />
< input id = " Text3 " type = " text " onfocus = " saygoodbye() " />
< input id = " Text2 " type = " text " /></ div >
</ form >
function sayhello()
... {
SayHello.HelloWorld(OnRequestComplete);
}
function saygoodbye()
... {
SayHello.GoodBye(OnRequestComplete);
}
function OnRequestComplete(result)
... {
document.getElementById("Text2").innerText=result;
}
</ script >
< form id = " form1 " runat = " server " >
< div >
& nbsp; < asp:ScriptManager ID = " ScriptManager1 " runat = " server " >
< Services >
< asp:ServiceReference Path = " SayHello.asmx " />
</ Services >
</ asp:ScriptManager >
< input id = " Text1 " type = " text " onfocus = " sayhello() " />
< input id = " Text3 " type = " text " onfocus = " saygoodbye() " />
< input id = " Text2 " type = " text " /></ div >
</ form >
SayHello.asmx的SayHello.cs代码如下:
using
System;
using System.Web;
using System.Collections;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Web.Script.Services;
/**/ /// <summary>
/// SayHello 的摘要说明
/// </summary>
[WebService(Namespace = " http://tempuri.org/ " )]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[ScriptService]
public class SayHello : System.Web.Services.WebService ... {
public SayHello () ...{
//如果使用设计的组件,请取消注释以下行
//InitializeComponent();
}
[WebMethod]
public string HelloWorld() ...{
return "hello the world";
}
[WebMethod]
public string GoodBye()
...{
return "goodbye,i will go home now";
}
}
using System.Web;
using System.Collections;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Web.Script.Services;
/**/ /// <summary>
/// SayHello 的摘要说明
/// </summary>
[WebService(Namespace = " http://tempuri.org/ " )]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[ScriptService]
public class SayHello : System.Web.Services.WebService ... {
public SayHello () ...{
//如果使用设计的组件,请取消注释以下行
//InitializeComponent();
}
[WebMethod]
public string HelloWorld() ...{
return "hello the world";
}
[WebMethod]
public string GoodBye()
...{
return "goodbye,i will go home now";
}
}
回过头来看一下程序其实简单无比,可编写却走了不少弯路,包括:
1.SayHello.cs中
using
System.Web.Script.Services;
[ScriptService]
[ScriptService]
这两项是必须加入而在新建时并不存在的,目的就是采用js来调用web service
2.WebService.aspx中的js函数返回值采用了返回处理函数的形式,之前我一直采用
function sayhello()
... {
var msg;
msg = SayHello.HelloWorld(OnRequestComplete);
alert(msg);
}
... {
var msg;
msg = SayHello.HelloWorld(OnRequestComplete);
alert(msg);
}
可每次都是显示“undefined”,查了一些网站知道了一种通过处理函数解决的办法,其他方法暂不知道,希望看到的能够教我,我也会继续更新新的办法。