BookInfo.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ClassProj
{
[Serializable]
public class BookInfo
{
public BookInfo() { }
//在VS2008中可使用如下简化方式定义属性,是不是很简单了呢?!
public string Name { set; get; }
public string Author { set; get; }
public int Price { set; get; }
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ClassProj
{
[Serializable]
public class BookInfo
{
public BookInfo() { }
//在VS2008中可使用如下简化方式定义属性,是不是很简单了呢?!
public string Name { set; get; }
public string Author { set; get; }
public int Price { set; get; }
}
}
Book.asmx.cs
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml.Linq;
using System.Web.Script.Services;
using ClassProj;
namespace Test
{
/**//// <summary>
/// Book 的摘要说明
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[ToolboxItem(false)]
// 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消对下行的注释。
[ScriptService]
[GenerateScriptType(typeof(BookInfo))]
public class Book : System.Web.Services.WebService
{
[WebMethod]
public string ShowInfo(BookInfo book)
{
return "书的信息如下——书名:" + book.Name + ",价格:" + book.Price + "作者:" + book.Author;
}
}
}
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml.Linq;
using System.Web.Script.Services;
using ClassProj;
namespace Test
{
/**//// <summary>
/// Book 的摘要说明
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[ToolboxItem(false)]
// 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消对下行的注释。
[ScriptService]
[GenerateScriptType(typeof(BookInfo))]
public class Book : System.Web.Services.WebService
{
[WebMethod]
public string ShowInfo(BookInfo book)
{
return "书的信息如下——书名:" + book.Name + ",价格:" + book.Price + "作者:" + book.Author;
}
}
}
Test.js
function ShowInfo()
{
var book=new ClassProj.BookInfo();
book.Name="征服AJAX!";
book.Price=25;
book.Author="梦想";
Test.Book.ShowInfo(book,CallBack_Function);
}
function CallBack_Function(ResponseStr)
{
$get("result").innerText=ResponseStr;
}
function ShowInfo()
{
var book=new ClassProj.BookInfo();
book.Name="征服AJAX!";
book.Price=25;
book.Author="梦想";
Test.Book.ShowInfo(book,CallBack_Function);
}
function CallBack_Function(ResponseStr)
{
$get("result").innerText=ResponseStr;
}
AjaxStudy.aspx.cs
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="AjaxStudy02.aspx.cs" Inherits="Test.AjaxStudy02" %>
<!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>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:ScriptManager ID="ScriptManager1" runat="server">
<Scripts>
<asp:ScriptReference Path="~/JsFile/Test.js" />
</Scripts>
<Services>
<asp:ServiceReference Path="~/Book.asmx" />
</Services>
</asp:ScriptManager>
<input id="Button1" type="button" value="测试一下" onclick="ShowInfo();" />
<div id="result"></div>
</div>
</form>
</body>
</html>
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="AjaxStudy02.aspx.cs" Inherits="Test.AjaxStudy02" %>
<!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>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:ScriptManager ID="ScriptManager1" runat="server">
<Scripts>
<asp:ScriptReference Path="~/JsFile/Test.js" />
</Scripts>
<Services>
<asp:ServiceReference Path="~/Book.asmx" />
</Services>
</asp:ScriptManager>
<input id="Button1" type="button" value="测试一下" onclick="ShowInfo();" />
<div id="result"></div>
</div>
</form>
</body>
</html>