The eXperT’s Voice ® in .
neT
Companion
https://www.yakibooki.com/download/foundations-of-asp-net-ajax-2nd-edition/
Foundations of
ASP.NET
ASP.NET AJAX
AJAX
Build AJAX Applications in ASP.NET
Robin Pars, Laurence Moroney,
and John Grieb
Foundations of ASP.NET AJAX (2nd Edition) Moroney
https://www.yakibooki.com/download/foundations-of-asp-net-ajax-2nd-edition/
This Book is Available on YakiBooki.com
Foundations of ASP.NET AJAX (2nd Edition) Moroney
https://www.yakibooki.com/download/foundations-of-asp-net-ajax-2nd-edition/
Foundations of ASP.NET
AJAX
Robin Pars, Laurence Moroney, and John Grieb
This Book is Available on YakiBooki.com
Foundations of ASP.NET AJAX (2nd Edition) Moroney
Foundations of ASP.NET AJAX
Copyright © 2007 by Robin Pars, Laurence Moroney, and John Grieb
https://www.yakibooki.com/download/foundations-of-asp-net-ajax-2nd-edition/
All rights reserved. No part of this work may be reproduced or transmitted in any form or by any means,
electronic or mechanical, including photocopying, recording, or by any information storage or retrieval
system, without the prior written permission of the copyright owner and the publisher.
ISBN-13 (pbk): 978-1-59059-828-3
ISBN-10 (pbk): 1-59059-828-8
Printed and bound in the United States of America 9 8 7 6 5 4 3 2 1
Trademarked names may appear in this book. Rather than use a trademark symbol with every occurrence
of a trademarked name, we use the names only in an editorial fashion and to the benefit of the trademark
owner, with no intention of infringement of the trademark.
Lead Editor: Ewan Buckingham
Technical Reviewers: Andy Olsen, Fabio Claudio Ferracchiati
Editorial Board: Steve Anglin, Ewan Buckingham, Tony Campbell, Gary Cornell, Jonathan Gennick,
Jason Gilmore, Kevin Goff, Jonathan Hassell, Matthew Moodie, Joseph Ottinger, Jeffrey Pepper,
Ben Renow-Clarke, Dominic Shakeshaft, Matt Wade, Tom Welsh
Project Manager: Beth Christmas
Copy Editor: Julie McNamee
Associate Production Director: Kari Brooks-Copony
Production Editor: Janet Vail
Compositor: Gina Rexrode
Proofreader: Lisa Hamilton
Indexer: Broccoli Information Management
Cover Designer: Kurt Krames
Manufacturing Director: Tom Debolski
Distributed to the book trade worldwide by Springer-Verlag New York, Inc., 233 Spring Street, 6th Floor,
New York, NY 10013. Phone 1-800-SPRINGER, fax 201-348-4505, e-mail
[email protected], or
visit http://www.springeronline.com.
For information on translations, please contact Apress directly at 2855 Telegraph Avenue, Suite 600,
Berkeley, CA 94705. Phone 510-549-5930, fax 510-549-5939, e-mail
[email protected], or visit
http://www.apress.com.
The information in this book is distributed on an “as is” basis, without warranty. Although every precau-
tion has been taken in the preparation of this work, neither the author(s) nor Apress shall have any
liability to any person or entity with respect to any loss or damage caused or alleged to be caused directly
or indirectly by the information contained in this work.
The source code for this book is available to readers at http://www.apress.com.
This Book is Available on YakiBooki.com
Foundations of ASP.NET AJAX (2nd Edition) Moroney
https://www.yakibooki.com/download/foundations-of-asp-net-ajax-2nd-edition/
I would like to dedicate this book to the memory of Steve Irwin. May the world have more
people as knowledgeable, genuine, enthusiastic, benevolent, and compassionate as him.
—Robin Pars
This book is dedicated to Rebecca, my wonderful wife and constant supporter.
I just don’t know what I would do without her.
Also to Claudia and Christopher, the greatest daughter and son a guy could ask for!
—Laurence Moroney
This Book is Available on YakiBooki.com
Foundations of ASP.NET AJAX (2nd Edition) Moroney
https://www.yakibooki.com/download/foundations-of-asp-net-ajax-2nd-edition/
This Book is Available on YakiBooki.com
Foundations of ASP.NET AJAX (2nd Edition) Moroney
Contents at a Glance
https://www.yakibooki.com/download/foundations-of-asp-net-ajax-2nd-edition/
About the Authors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xiii
About the Technical Reviewers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xiv
Acknowledgments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xv
Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xvii
■CHAPTER 1 Introducing AJAX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
■CHAPTER 2 Taking AJAX to the Next Level . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
■CHAPTER 3 The Microsoft AJAX Library: Making Client-Side
JavaScript Easier . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
■CHAPTER 4 ASP.NET AJAX Client Libraries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55
■CHAPTER 5 Introducing Server Controls in ASP.NET AJAX . . . . . . . . . . . . . . . . . . . 81
■CHAPTER 6 Using Server Controls in ASP.NET AJAX . . . . . . . . . . . . . . . . . . . . . . . . 109
■CHAPTER 7 Using the ASP.NET AJAX Control Toolkit (Part 1) . . . . . . . . . . . . . . . . 131
■CHAPTER 8 Using the ASP.NET AJAX Control Toolkit (Part 2) . . . . . . . . . . . . . . . . 165
■CHAPTER 9 AJAX-Style Mapping Using the Virtual Earth SDK. . . . . . . . . . . . . . . 205
■CHAPTER 10 Building a Sample Application Using ASP.NET AJAX . . . . . . . . . . . . 225
■INDEX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 257
This Book is Available on YakiBooki.com
Foundations of ASP.NET AJAX (2nd Edition) Moroney
https://www.yakibooki.com/download/foundations-of-asp-net-ajax-2nd-edition/
This Book is Available on YakiBooki.com
Foundations of ASP.NET AJAX (2nd Edition) Moroney
Contents
https://www.yakibooki.com/download/foundations-of-asp-net-ajax-2nd-edition/
About the Authors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xiii
About the Technical Reviewers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xiv
Acknowledgments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xv
Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xvii
■CHAPTER 1 Introducing AJAX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
Delving into the History of Web Application Technology . . . . . . . . . . . . . . . . 1
Thin Client Applications Save the Day . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
AJAX Enters the Picture . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
Using the XMLHttpRequest Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
Using Visual Studio 2005 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
Seeing a Simple Example in Action . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
■CHAPTER 2 Taking AJAX to the Next Level . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
Introducing ASP.NET 2.0 Server Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
Synchronous vs. Asynchronous Web Applications . . . . . . . . . . . . . . . . . . . . 24
Introducing the ASP.NET AJAX Architecture . . . . . . . . . . . . . . . . . . . . . . . . . 25
An Overview of the AJAX Library. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
The Microsoft AJAX Library and Web Services . . . . . . . . . . . . . . . . . . 27
JavaScript Object Notation (JSON) . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
An Overview of the ASP.NET 2.0 AJAX Extensions . . . . . . . . . . . . . . . 28
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
vii
This Book is Available on YakiBooki.com
Foundations of ASP.NET AJAX (2nd Edition) Moroney
viii ■CONTENTS
■CHAPTER 3 The Microsoft AJAX Library: Making Client-Side
JavaScript Easier . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
https://www.yakibooki.com/download/foundations-of-asp-net-ajax-2nd-edition/
JavaScript with the Microsoft AJAX Library . . . . . . . . . . . . . . . . . . . . . . . . . 31
Downloading and Installing ASP.NET 2.0 AJAX Extension 1.0 . . . . . 32
Creating Your First AJAX Application. . . . . . . . . . . . . . . . . . . . . . . . . . . 32
Adding a Custom JavaScript Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
Using the AJAX Script Manager to Deliver Your Custom Class. . . . . 37
Coding and Running the Application. . . . . . . . . . . . . . . . . . . . . . . . . . . 40
Using Namespaces and Classes in JavaScript . . . . . . . . . . . . . . . . . . . . . . . 41
Using Inheritance in JavaScript. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43
Implementing Interfaces in JavaScript . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45
Accessing Server Resources from JavaScript . . . . . . . . . . . . . . . . . . . . . . . 49
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54
■CHAPTER 4 ASP.NET AJAX Client Libraries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55
JavaScript Type Extensions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55
Array and Boolean Extensions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55
Date Extensions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58
Error Extensions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59
Number Extension . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61
Object Extension . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63
String Extension . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64
The Sys Namespace . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66
Sys.Application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67
Sys.Component and Client Component Model . . . . . . . . . . . . . . . . . . . . . . . 70
Sys.UI . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71
Sys.UI.DomElement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72
Sys.UI.DomEvent . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75
Global Shortcuts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77
Other Commonly Used Classes in the Sys Namespace . . . . . . . . . . . . . . . 78
Sys.Browser . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78
Sys.StringBuilder . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80
This Book is Available on YakiBooki.com