惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

J
Java Code Geeks
美团技术团队
Recent Announcements
Recent Announcements
B
Blog
GbyAI
GbyAI
雷峰网
雷峰网
博客园_首页
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
T
Tailwind CSS Blog
M
MIT News - Artificial intelligence
V
V2EX
人人都是产品经理
人人都是产品经理
爱范儿
爱范儿
L
LangChain Blog
Microsoft Security Blog
Microsoft Security Blog
宝玉的分享
宝玉的分享
A
About on SuperTechFans
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
U
Unit 42
Hugging Face - Blog
Hugging Face - Blog
F
Fortinet All Blogs
N
Netflix TechBlog - Medium
Last Week in AI
Last Week in AI
aimingoo的专栏
aimingoo的专栏

博客园 - Tiu

2010走了,又是一年,留个脚印 Commerce Server 2007 随笔一 2009眨眼间过去了,留个纪念 asp.net中慎用static全局变量 asp.net ajax随笔一 收集的关于依赖注入及Unity application block入门的一些资料 2008最后一篇:总结与展望 文件操作类简介 防止页面在提交的过程中多次点击按钮 第一次使用SQLCLR C#网络编程随笔一 装AJAX.NET 1.0的环境,我遇到个问题,进来解答下 关于邮件群发 XML学习一 asp.net窗中的两个Form问题 URL重写入门 动态从数据库中选择Top 个数 关于在数据层返回SqlDataReader 编写类和子程序的几个原则
asp.net ajax随笔二
Tiu · 2009-01-12 · via 博客园 - Tiu

      下面介绍下通过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