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

推荐订阅源

T
The Blog of Author Tim Ferriss
S
Securelist
D
Docker
The Register - Security
The Register - Security
GbyAI
GbyAI
Recorded Future
Recorded Future
Engineering at Meta
Engineering at Meta
Stack Overflow Blog
Stack Overflow Blog
云风的 BLOG
云风的 BLOG
P
Proofpoint News Feed
罗磊的独立博客
博客园 - 【当耐特】
F
Full Disclosure
WordPress大学
WordPress大学
腾讯CDC
小众软件
小众软件
大猫的无限游戏
大猫的无限游戏
D
DataBreaches.Net
SecWiki News
SecWiki News
L
Lohrmann on Cybersecurity
I
InfoQ
MyScale Blog
MyScale Blog
量子位
Cyberwarzone
Cyberwarzone
博客园 - 三生石上(FineUI控件)
The Hacker News
The Hacker News
F
Fortinet All Blogs
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
Jina AI
Jina AI
博客园_首页
H
Help Net Security
K
Kaspersky official blog
酷 壳 – CoolShell
酷 壳 – CoolShell
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
www.infosecurity-magazine.com
www.infosecurity-magazine.com
Webroot Blog
Webroot Blog
Blog — PlanetScale
Blog — PlanetScale
V
Vulnerabilities – Threatpost
Y
Y Combinator Blog
The Cloudflare Blog
P
Proofpoint News Feed
V
Visual Studio Blog
C
Cyber Attacks, Cyber Crime and Cyber Security
T
Tailwind CSS Blog
爱范儿
爱范儿
P
Privacy International News Feed
Security Archives - TechRepublic
Security Archives - TechRepublic
The GitHub Blog
The GitHub Blog
C
Cybersecurity and Infrastructure Security Agency CISA
B
Blog RSS Feed

博客园 - 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