ASP.NET AJAX ScriptManager调用WebService
ScriptManager是一个ASP.NET AJAX中的核心控件。它为ASP.NET AJAX开发的带了很多的便利,它是我们做ASP.NET AJAX的程序不可缺少的控件,它的作用非比寻常,我们来看看他是如何调用WebService的!
首先建立一个MyWebService.asmx文件。(一定注意看中文注释)
以下为MyWebService.cs代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
/// <summary>
/// Summary description for MyWebService
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
//下面这行属性是用ScriptManager调用WebService的必要属性。
[System.Web.Script.Services.ScriptService]
public class MyWebService : System.Web.Services.WebService
{
public MyWebService()
{
//Uncomment the following line if using designed components
//InitializeComponent();
}
[WebMethod]
public int GetTotal(int x, int y)
{
return x + y;
}
}
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
/// <summary>
/// Summary description for MyWebService
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
//下面这行属性是用ScriptManager调用WebService的必要属性。
[System.Web.Script.Services.ScriptService]
public class MyWebService : System.Web.Services.WebService
{
public MyWebService()
{
//Uncomment the following line if using designed components
//InitializeComponent();
}
[WebMethod]
public int GetTotal(int x, int y)
{
return x + y;
}
}
在Default.aspx页面中添加ScriptManager控件并引用WebService
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="ScriptWebSerivce.aspx.cs" Inherits="ScriptWebSerivce" %>
<!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">
function ShowResult(res) {
alert(res);}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:ScriptManager ID="ScriptManager1" runat="server">
<Services>
<asp:ServiceReference Path="MyWebService.asmx" />
</Services>
</asp:ScriptManager>
<script type="text/javascript">
MyWebService.GetTotal(5, 4, ShowResult, null, "abc");
</script>
</div>
</form>
</body>
</html>
<!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">
function ShowResult(res) {
alert(res);}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:ScriptManager ID="ScriptManager1" runat="server">
<Services>
<asp:ServiceReference Path="MyWebService.asmx" />
</Services>
</asp:ScriptManager>
<script type="text/javascript">
MyWebService.GetTotal(5, 4, ShowResult, null, "abc");
</script>
</div>
</form>
</body>
</html>
但我们在ScriptManager中引用了WebService就可以在JS脚本里面调用WebService了。
GetTotal是在WebService中对应的WebService方法,但在这里它多了3个参数。
GetTotal(number x,number y,onSuccess,onError,userContent);
大家看看这个参数名就应该知道是什么意思了吧!!
其实用ScriptManager调用WebService很简单的!
有任何问题可以给我留言!