安装好ajax 1.0 beta 以后,新建一个ajax 网站。从左边工具拉一个Script Manage
<asp:ScriptManager ID="ScriptManager1" runat="server" >
<Services>
<asp:ServiceReference Path="WebService.asmx" />
</Services>
</asp:ScriptManager>
<Services>
<asp:ServiceReference Path="WebService.asmx" />
</Services>
</asp:ScriptManager>
在页上再拉一个button
<input id="Button1" type="button" value="button" language="javascript" onclick="return Button1_onclick()" style="width: 80px" />
添加javascript
<script language="javascript" type="text/javascript">
<!--
function Button1_onclick() {
WebService.getdate(OnRequestComplete);
}
function OnRequestComplete(result)
{
//$('p').innerHTML=result;
alert(result);
}
// -->
</script>
<!--
function Button1_onclick() {
WebService.getdate(OnRequestComplete);
}
function OnRequestComplete(result)
{
//$('p').innerHTML=result;
alert(result);
}
// -->
</script>
新建一个wervice
using System;
using System.Web;
using System.Collections;
using System.Web.Services;
using System.Web.Services.Protocols;
using Microsoft.Web.Script.Services;
/// <summary>
/// WebService 的摘要说明
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[Microsoft.Web.Script.Services.ScriptService]//请注意必须添加
public class WebService : System.Web.Services.WebService {
public WebService () {
//如果使用设计的组件,请取消注释以下行
//InitializeComponent();
}
[WebMethod]
public string HelloWorld() {
return "Hello World";
}
[WebMethod]
//[ScriptMethod(UseHttpGet = true)]
public string getdate()
{
return DateTime.Now.ToString();
}
}
using System.Web;
using System.Collections;
using System.Web.Services;
using System.Web.Services.Protocols;
using Microsoft.Web.Script.Services;
/// <summary>
/// WebService 的摘要说明
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[Microsoft.Web.Script.Services.ScriptService]//请注意必须添加
public class WebService : System.Web.Services.WebService {
public WebService () {
//如果使用设计的组件,请取消注释以下行
//InitializeComponent();
}
[WebMethod]
public string HelloWorld() {
return "Hello World";
}
[WebMethod]
//[ScriptMethod(UseHttpGet = true)]
public string getdate()
{
return DateTime.Now.ToString();
}
}
运行一下,看看,经过多次试验才成的。
(参考:http://ajax.asp.net/docs/tutorials/useWebServiceProxy.aspx)
代码如下:
/Files/fmyuan2000/AJAXCTPEnabledWebSite1.rar