下面介绍下通过Web Service和Page 方法进行异步调用。

首先新建一个asp.net ajax 的项目,添加一个book类和author类

    public class Book
    {
        
public string Title{ getset; }
        
public string Content{ getset; }
        
public double Price { getset; }

    }

   public class Author
    {
        public string Name { set; get; }
        public string Description { get; set; }
    }


然后添加添加一个web service ,由于webservice中没有用到Author类,所以如果想在客户端javascript用到author类的话必须加入[GenerateScriptType(typeof(Author))]

    [ScriptService]
    [GenerateScriptType(
typeof(Author))]
    [WebService(Namespace 
= "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo 
= WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(
false)]
    
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    
// [System.Web.Script.Services.ScriptService]
    public class bookService : System.Web.Services.WebService
    
{
        [WebMethod]
        
public List<Book> GetBooks()
        
{
            List
<Book> books = new List<Book>();
            Book book 
= new Book();
            book.Title 
= "Inside C#";
            book.Content 
= "The book is about c# knowledge";
            book.Price 
= 56;
            books.Add(book);
            Book book2 
= new Book();
            book2.Title 
= "asp.net ajax in action";
            book2.Content 
= "ajax";
            book2.Price 
= 46;
            books.Add(book2);
            
return books;
        }

 

这样就可以在客户端异步调用此方法了。下面贴出aspx页面的代码及html内容

        [WebMethod]
        
public static string HiAuthor(Author author)
        {
            
return string.Format("Hi {0}--{1}", author.Name, author.Description);
        }

 

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="ajaxDemo._Default" %>

<!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 getBooks() {
        ajaxDemo.bookService.GetBooks(onGetBooksSuccess, onGetBooksFailure);
    }

    function onGetBooksSuccess(result, context, methodName) 
{
        var sb 
= new Sys.StringBuilder();
        
for (var i = 0; i < result.length; i++{
            var book 
= result[i];
            sb.append(book.Title 
+ "-");
            sb.append(book.Content 
+ "-");
            sb.append(book.Price 
+ "<br/>");
        }

        $
get("books").innerHTML = sb.toString();
    }

    function onGetBooksFailure(error, context, methodName) 
{
        $
get("books").innerHTML = error.get_message();
    }

    
//客户端实例化一个实体类
    function initialAuthor() {
        var author 
= new ajaxDemo.Author();
        author.Name 
= "Sean";
        author.Description 
= "PageMethod";
        
//通过一个page 方法进行异步调用
        PageMethods.HiAuthor(author, onHiAuthorSuccess);
    }

    function onHiAuthorSuccess(result, context, methodName) 
{
        $
get("author").innerHTML = result;
    }

</script>
</head>
<body>
    
<form id="form1" runat="server">
    
<asp:ScriptManager ID="scriptManager" runat="server" EnablePageMethods="true">
      
<Services>
       
<asp:ServiceReference Path="~/bookService.asmx" InlineScript="true" />
      
</Services>
    
</asp:ScriptManager>
    
<div>
    
<input type="button" id="GetBooks" value="Get Books" onclick="getBooks()" />
    
<div id="books"></div>
    
</div>
    
<input type="button" value="PageMethod" onclick="initialAuthor()" />
    
<div id="author"></div>
    
</form>
</body>
</html>

 上面的两个按钮一个通过webservice的方法异步调用,一个是通过page 方法调用,在进行pagemethod的方式调用时,要不ScriptManager的EnablePageMethods的属性设置为True

posted on 2009-01-12 22:16  Tiu  阅读(239)  评论(0编辑  收藏  举报