Table Of ContentProgramming ASP.NET AJAX
Other Microsoft Windows resources from O’Reilly
Related titles Essential SharePoint 2007 SharePoint 2007:
Learning ASP.NET 2.0 with The Definitive Guide
AJAX Windows Vista:
Learning C# 2008 The Definitive Guide
Programming WPF Windows Vista in a Nutshell
Windows Books windows.oreilly.com is a complete catalog of O’Reilly’s Win-
Resource Center dows and Office books, including sample chapters and code
examples.
oreillynet.comistheessentialportalfordevelopersinterestedin
openandemergingtechnologies,includingnewplatforms,pro-
gramming languages, and operating systems.
Conferences O’Reillybringsdiverseinnovatorstogethertonurturetheideas
thatsparkrevolutionaryindustries.Wespecializeindocument-
ing the latest tools and systems, translating the innovator’s
knowledge into useful skills for those in the trenches. Visit
conferences.oreilly.com for our upcoming events.
Safari Bookshelf (safari.oreilly.com) is the premier online refer-
ence library for programmers and IT professionals. Conduct
searchesacrossmorethan1,000books.Subscriberscanzeroin
on answers to time-critical questions in a matter of seconds.
Read the books on your Bookshelf from cover to cover or sim-
ply flip to the page you need. Try it today for free.
Programming ASP.NET AJAX
Christian Wenz
Beijing • Cambridge • Farnham • Köln • Paris • Sebastopol • Taipei • Tokyo
Programming ASP.NET AJAX
by Christian Wenz
Copyright © 2007 Christian Wenz. All rights reserved.
Printed in the United States of America.
Published by O’Reilly Media, Inc., 1005 Gravenstein Highway North, Sebastopol, CA 95472.
O’Reilly books may be purchased for educational, business, or sales promotional use. Online editions
are also available for most titles (safari.oreilly.com). For more information, contact our
corporate/institutional sales department: (800) 998-9938 orcorporate@oreilly.com.
Editor: John Osborn Cover Designer: Karen Montgomery
Technical Editor: Mike Pope Interior Designer: David Futato
Production Editor: Rachel Monaghan Illustrators: Robert Romano and
Production Services: Octal Publishing, Inc. Jessamyn Read
Printing History:
September 2007: First Edition.
Nutshell Handbook, the Nutshell Handbook logo, and the O’Reilly logo are registered trademarks of
O’Reilly Media, Inc.Programming ASP.NET AJAX, the image of a murex, and related trade dress are
trademarks of O’Reilly Media, Inc.
Manyofthedesignationsusedbymanufacturersandsellerstodistinguishtheirproductsareclaimedas
trademarks. Where those designations appear in this book, and O’Reilly Media, Inc. was aware of a
trademark claim, the designations have been printed in caps or initial caps.
Whileeveryprecautionhasbeentakeninthepreparationofthisbook,thepublisherandauthorassume
no responsibility for errors or omissions, or for damages resulting from the use of the information
contained herein.
This book uses RepKover™, a durable and flexible lay-flat binding.
ISBN-10: 0-596-51424-7
ISBN-13: 978-0-596-51424-2
[M]
Table of Contents
Preface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xi
Part I. Basics
1. ASP.NET AJAX, Ajax, and ASP.NET. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
ASP.NET AJAX and Ajax 3
ASP.NET AJAX and ASP.NET 5
ASP.NET AJAX Prerequisites and Installation 6
ASP.NET AJAX Structure and Architecture 12
A First ASP.NET AJAX Example: Hello User 14
The ScriptManager Control 19
Summary 21
For Further Reading 21
2. JavaScript . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
The JavaScript Language 24
Object-Oriented Programming (OOP) 34
Accessing Page Elements 38
DOM Methods 42
Summary 43
For Further Reading 43
3. Ajax. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44
The XMLHttpRequest Object 45
The XMLDocument Object 55
JSON 61
Summary 64
For Further Reading 64
v
Part II. ASP.NET AJAX Extensions
4. Using ASP.NET AJAX JavaScriptExtensions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67
ASP.NET AJAX Shortcuts and Helper Functions 67
Extensions to Existing JavaScript Objects 70
ASP.NET AJAX OOP Features for JavaScript 71
Client Versions of .NET Classes 83
Summary 87
For Further Reading 87
5. Web Services . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88
Error Handling 88
Page Methods 93
Maintaining Session State 95
Exchanging Complex Data with the Server 100
Consuming Web Services with JavaScript 105
Summary 114
For Further Reading 115
6. UpdatePanel: Refreshing Only PartsofaPage . . . . . . . . . . . . . . . . . . . . . . . . 116
Making a Page Region Updatable 116
Summary 131
For Further Reading 132
7. Using the ASP.NET AJAX Profile Service . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 133
Preparing the Web Site 133
Accessing Profile Data 135
Accessing Profile Group Data 139
Summary 144
For Further Reading 144
8. Using the ASP.NET AJAX AuthenticationService. . . . . . . . . . . . . . . . . . . . . . . 145
Preparing the Application 145
Login and Logout 148
Summary 154
For Further Reading 154
vi | Table of Contents
9. Localizing and Globalizing Applications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 155
Localization 156
Globalization and Internationalization 169
Summary 173
For Further Reading 173
Part III. ASP.NET AJAX Control Toolkit
10. Using the Control Toolkit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 177
Installing the Control Toolkit 177
Using the Control Toolkit 181
Summary 184
For Further Reading 184
11. Adding Animation to a Web Page . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 185
Animation Framework 185
Drag-and-Drop 193
Summary 195
For Further Reading 196
12. Autocompleting User Input, FightingSpam, and More . . . . . . . . . . . . . . . . 197
Creating an Accordion Pane 197
Maintaining the Relative Position of an Element 199
Adding Autocomplete Behavior to a TextBox Control 201
Attaching a Calendar to a Text Field 208
Dynamically Collapsing a Single Panel 210
Displaying a Pop Up Over a Page 211
Fighting Spam in Blogs and in Other Entry Forms 214
Creating a Tabbed Interface 216
Summary 218
For Further Reading 218
13. Writing Custom Controls and Contributing to the Community . . . . . . . . . . 219
Writing Custom ASP.NET AJAX Controls 219
Contributing to the Control Toolkit 228
Summary 237
For Further Reading 237
Table of Contents | vii
Part IV. ASP.NET AJAX Futures
14. Client Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 241
Introducing ASP.NET AJAX Client Controls 241
Using ASP.NET AJAX Controls 242
Handling Control Events 259
Summary 263
For Further Reading 263
15. Binding and Validating Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 264
Data Binding 264
Data Validation 280
Summary 296
For Further Reading 296
16. Using Behaviors and Components . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 297
Using Behaviors 297
Using Components 310
Summary 313
For Further Reading 313
17. Using Server Data. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 314
Using a ListView Control 314
Creating a Custom Data Source 330
Summary 336
For Further Reading 336
18. Using Remote Web Services . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 337
Using the Google Web Service 338
Using the Amazon Web Service 345
Transforming a Web Service Result with XSLT 350
Using the Yahoo! Web Service (and REST and XPath) 358
Summary 367
For Further Reading 367
19. Using Animations. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 368
Using Animations 368
Using an Animation to Create a Fade Effect 370
viii | Table of Contents
Summary 380
For Further Reading 380
20. Fixing Bookmarks and Back/Forward Buttons . . . . . . . . . . . . . . . . . . . . . . . . 381
Fixing with Code 382
Fixing Bookmarks and Back/Forward Buttons with Controls Using
UpdateHistory 384
Fixing Bookmarks and Back/Forward Buttons with Controls Using the
ASP.NET AJAX Futures 388
Summary 395
For Further Reading 395
21. Web Parts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 396
Using ASP.NET AJAX with ASP.NET Web Parts 396
Summary 401
For Further Reading 401
Part V. Microsoft AJAX Library
22. Using ASP.NET AJAX with OtherServerTechnologies. . . . . . . . . . . . . . . . . . . 405
Using ASP.NET AJAX with PHP 406
Summary 409
For Further Reading 410
Part VI. Appendixes
A. Debugging ASP.NET AJAX Applications. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 413
B. XMLHttpRequest Reference . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 426
C. DOM Reference . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 428
D. ASP.NET AJAX Reference . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 432
E. ScriptManager, UpdatePanel, UpdateProgress, and Timer
Declarative Reference . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 435
Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 439
Table of Contents | ix
Description:Related titles Essential SharePoint 2007. Learning ASP.NET 2.0 with. AJAX. Learning C# 2008. Programming WPF. SharePoint 2007: The Definitive