Joydip Kanjilal and Sriram Putrevu SamsTeachYourself ASP.NET Ajax 24 in Hours 800 East 96th Street,Indianapolis,Indiana,46240 USA Sams Teach Yourself ASP.NET Ajax in 24 Hours Editor-in-Chief Copyright © 2009 by Pearson Education,Inc. Karen Gettman All rights reserved. No part of this book shall be reproduced,stored in a retrieval system,or Executive Editor transmitted by any means,electronic,mechanical,photocopying,recording,or otherwise,without written permission from the publisher. No patent liability is assumed with respect to the use of Neil Rowe the information contained herein. Although every precaution has been taken in the preparation of Development this book,the publisher and authors assume no responsibility for errors or omissions. Nor is any Editor liability assumed for damages resulting from the use of the information contained herein. Mark Renfrow ISBN-13: 978-0-672-32967-8 ISBN-10: 0-672-32967-0 Managing Editor The Library of Congress Cataloging-in-Publication Data is on file. Kristy Hart Printed in the United States of America Project Editor First Printing July 2008 Betsy Harris Trademarks All terms mentioned in this book that are known to be trademarks or service marks have been Copy Editor appropriately capitalized. Sams Publishing cannot attest to the accuracy of this information. Use Water Crest of a term in this book should not be regarded as affecting the validity of any trademark or service Publishing,Inc. mark. Indexer Warning and Disclaimer Lisa Stumpf Every effort has been made to make this book as complete and accurate as possible,but no war- ranty or fitness is implied. The information provided is on an “as is” basis. The authors and the Proofreaders publisher shall have neither liability nor responsibility to any person or entity with respect to any San Dee Phillips loss or damages arising from the information contained in this book. Language Logistics Bulk Sales Sams Publishing offers excellent discounts on this book when ordered in quantity for bulk pur- Technical Editor chases or special sales. For more information,please contact J.Boyd Nolan U.S. Corporate and Government Sales Publishing 1-800-382-3419 Coordinator [email protected] Cindy Teeters For sales outside the U.S.,please contact International Sales Book Designer [email protected] Gary Adair Compositor Bronkella Publishing This Book Is Safari Enabled The Safari®Enabled icon on the cover of your favorite technology book means the book is available through Safari Bookshelf. When you buy this book,you get free access to the online edition for 45 days. Safari Bookshelf is an electronic reference library that lets you easily search thousands of technical books,find code samples,download chapters,and access technical information whenever and wherever you need it. To gain 45-day Safari Enabled access to this book: . Go to http://www.informit.com/onlineedition. . Complete the brief registration form. . Enter the coupon code RDMF-KFKM-MCJK-C9LD-99E1. If you have difficulty registering on Safari Bookshelf or accessing the online edition,please email customer- [email protected]. Contents at a Glance Part I: Getting Started with Ajax HOUR 1 Getting Started with ASP.NET Ajax.................................................3 2 Understanding the ASP.NET Ajax Architecture................................23 3 Working with the XMLHttpRequestObject.......................................37 4 Understanding Client-Side Scripting..............................................53 5 Data Communication with the Server...........................................69 6 Working with the Microsoft Ajax Library.......................................85 Part II: Working with Ajax HOUR 7 Using ASP.NET Ajax Server Extensions.........................................103 8 Working with the UpdatePanelandUpdateProgressControls..........117 9 Working with the ScriptManagerandTimerControls.....................135 10 Working with Ajax Control Toolkit—Part I....................................147 11 Working with Ajax Control Toolkit—Part II...................................161 12 ASP.NET Ajax and Web Parts......................................................175 13 ASP.NET Ajax Client Life Cycle Events..........................................195 Part III: Advanced Concepts HOUR 14 Working with Web Services Using Ajax........................................209 15 Working with Authentication Service Using Ajax...........................221 16 Working with Profile Service Using Ajax.......................................235 17 Extending the Ajax Library........................................................249 18 Implementing Localization and Globalization Using ASP.NET Ajax..259 19 Debugging and Tracing in ASP.NET Ajax.....................................273 20 The ASP.NET Ajax Futures CTP...................................................285 iv Sams Teach Yourself ASP.NET Ajax in 24 Hours Part IV: Using ASP.NET Ajax to Build a Sample E-Commerce Application HOUR 21 Introducing e-Commerce and Designing the Application................299 22 Setting Up the Application.........................................................317 23 Searching and Shopping for the Products.....................................343 24 Generating and Managing Orders...............................................375 Index.....................................................................................393 Table of Contents Part I: Getting Started with Ajax HOUR 1: Getting Started with ASP.NET Ajax 3 Things You Should Know........................................................................4 Ajax—A Paradigm Shift ........................................................................4 Technologies That Make Up Ajax ............................................................5 The Pros and Cons of Using Ajax ............................................................6 The Downsides of Using Ajax..............................................................6 Looking Back in Time............................................................................7 What Is ASP.NET Ajax?..........................................................................7 Other Ajax Frameworks..........................................................................8 Goals of ASP.NET Ajax ..........................................................................9 Installing Ajax......................................................................................9 Setting Up Your Environment ............................................................10 Installing ASP.NET Ajax....................................................................10 Creating Your First Ajax Application ......................................................13 Creating a Generic Function for Instantiating the XMLHttpRequestObject......14 How Does It Work? ..........................................................................15 What Does the Previous Code Do?......................................................17 Summary ..........................................................................................19 Workshop ..........................................................................................20 Quiz..............................................................................................20 Answers ........................................................................................20 HOUR 2: Understanding the ASP.NET Ajax Architecture 23 Introducing ASP.NET............................................................................23 A Quick Look at the ASP.NET Architecture ..............................................24 The Application Life Cycle Events ......................................................24 The Page Life Cycle Events ................................................................25 vi Sams Teach Yourself ASP.NET Ajax in 24 Hours A Bird’s-Eye View of the ASP.NET Ajax Architecture ..................................28 What’s Inside the ASP.NET Ajax Server Framework? ..............................30 What’s Inside the ASP.NET Ajax Client Framework? ..............................32 Summary ..........................................................................................33 Workshop ..........................................................................................33 Quiz..............................................................................................33 Answers ........................................................................................34 HOUR 3: Working with the XMLHttpRequest Object 37 An Overview of the XMLHttpRequestObject..............................................37 Looking Back in Time ......................................................................38 Creating the XMLHttpRequestObject ......................................................38 Synchronous and Asynchronous Data Retrieval Using the XmlHttpRequest Object................................................................................................40 Synchronous Data Retrieval ..............................................................40 Asynchronous Data Retrieval ............................................................42 Working with the XMLHttpRequestObject ................................................44 Simulating Ajax Behavior Without Using the XMLHttpRequestObject ..........50 Summary ..........................................................................................51 Workshop ..........................................................................................51 Quiz..............................................................................................51 Answers ........................................................................................51 HOUR 4: Understanding Client-Side Scripting 53 Introducing DHTML ............................................................................53 What Is CSS? ..................................................................................54 Event Handling with JavaScript..........................................................58 JavaScript and the Document Object Model ............................................60 The Document Object ......................................................................62 The Element Object..........................................................................63 Implementing Client-Side Scripting ........................................................63 Summary ..........................................................................................66 vii Contents Workshop ..........................................................................................67 Quiz..............................................................................................67 Answers ........................................................................................67 HOUR 5: Data Communication with the Server 69 The Request and Response Cycle............................................................69 Understanding the Data Interchange Formats..........................................71 HTML Content ................................................................................71 The Most Common Plain Text or String Format ....................................71 XML—The Language of the Web for Data Exchange..............................72 Introducing JSON ................................................................................73 Using Arrays to Store Multiple Ordered Items ......................................74 Using Object Literals to Store Name/Value Pairs....................................75 Understanding JSON........................................................................76 Parsing JSON ..................................................................................77 Using JSON with Ajax ......................................................................78 Summary ..........................................................................................82 Workshop ..........................................................................................82 Quiz..............................................................................................82 Answers ........................................................................................82 HOUR 6: Working with the Microsoft Ajax Client Library 85 Introducing the Microsoft Ajax Client Library ..........................................85 Goals of the Microsoft Ajax Client Library ..........................................86 Inside the Microsoft Ajax Client Library Namespaces ................................87 TheGlobalNamespace—Extending JavaScript ....................................87 TheSysNamespace—The Root of All Namespaces................................88 TheSys.NetNamespace ..................................................................89 TheSys.SerializationNamespace....................................................89 TheSys.ServicesNamespace ..........................................................89 TheSys.UINamespace ....................................................................89 TheSys.WebFormsNamespace—Enabling Partial Rendering ..................89 viii Sams Teach Yourself ASP.NET Ajax in 24 Hours The Building Blocks of the Microsoft Ajax Client Library............................90 What’s Inside the Core Framework?....................................................91 What’s Inside the User Interface Framework?........................................93 Extending the JavaScript Library with the Microsoft Ajax JavaScript Extensions Framework......................................................................95 Summary ..........................................................................................97 Workshop ..........................................................................................98 Quiz..............................................................................................98 Answers ........................................................................................98 Part II: Working with Ajax HOUR 7: Using ASP.NET Ajax Server Extensions 103 The ASP.NET Ajax Server Extensions Framework ....................................103 Looking at the Components of the ASP.NET Ajax Server Extensions Framework....................................................................................106 What Are the ASP.NET Ajax Server Controls?......................................106 A Quick Look at the Microsoft Ajax Server Reference Library ....................110 TheSystem.Web.UINamespace........................................................110 TheSystem.Web.UI.DesignNamespace ............................................111 TheSystem.Web.ConfigurationNamespace ......................................112 TheSystem.Web.HandlersNamespace ..............................................112 TheSystem.Web.ScriptNamespace..................................................113 TheSystem.Web.Script.ServicesNamespace....................................113 Summary..........................................................................................113 Workshop ........................................................................................114 Quiz ............................................................................................114 Answers........................................................................................114 HOUR 8: Working with the UpdatePanel and UpdateProgress Controls 117 What Is Partial-Page Rendering? ..........................................................117 Why Use Partial-Page Rendering? ....................................................118 Looking Back in Time ....................................................................118 ix Contents TheUpdatePanelServer Control—Your Companion for Implementing Partial Updates..................................................................................119 What Is a ContentTemplate?............................................................122 What Are Triggers? ............................................................................127 Looking at the UpdateProgressControl ................................................129 Summary..........................................................................................132 Workshop ........................................................................................132 Quiz ............................................................................................132 Answers........................................................................................132 HOUR 9: Working with the ScriptManager and Timer Controls 135 An Overview of the ScriptManagerControl............................................135 Error Handling Using Ajax..................................................................137 Working with the TimerControl ..........................................................140 Implementing Partial Page Updates Using the UpdatePaneland the TimerControls ..................................................................................142 Summary..........................................................................................145 Workshop ........................................................................................145 Quiz ............................................................................................145 Answers........................................................................................146 HOUR 10: Working with the Ajax Control Toolkit—Part I 147 Introducing the Control Toolkit............................................................147 What Are Ajax-Enabled Controls? ....................................................148 Extender ......................................................................................150 Script Control................................................................................151 Working with the Control Toolkit..........................................................152 The AutoComplete Extender ............................................................153 Summary..........................................................................................158 Workshop ........................................................................................158 Quiz ............................................................................................158 Answers........................................................................................159