BookInfo. cs
Using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Text;
Namespace ClassProj
{
[Serializable]
Public class BookInfo
{
Public BookInfo (){}
// In VS2008, you can use the following simplified method to define attributes. Is it easy ?!
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>
/// Summary of the Book
/// </Summary>
[WebService (Namespace = "http://tempuri.org/")]
[WebServiceBinding (ConformsTo = WsiProfiles. BasicProfile1_1)]
[ToolboxItem (false)]
// To allow ASP. net ajax to call this Web service from a script, cancel the comments to the downstream.
[ScriptService]
[GenerateScriptType (typeof (BookInfo)]
Public class Book: System. Web. Services. WebService
{
[WebMethod]
Public string ShowInfo (BookInfo book)
{
Return "the book information is as follows -- title:" + book. Name + ", Price:" + book. Price + "Author:" + book. Author;
}
}
} Test. js
Function ShowInfo ()
{
Var book = new ClassProj. BookInfo ();
Book. Name = "conquer AJAX! ";
Book. Price = 25;
Book. Author = "dream ";
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> No title page </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 = "test" onclick = "ShowInfo ();"/>
<Div id = "result"> </div>
</Div>
</Form>
</Body>
</Html>