Microsoft AJAX Library Essentials Client-side ASP.NET AJAX 1.0 Explained A practical tutorial to using Microsoft AJAX Library to enhance the user experience of your ASP.NET Web Applications Bogdan Brinzarea Cristian Darie BIRMINGHAM - MUMBAI Microsoft AJAX Library Essentials Client-side ASP.NET AJAX 1.0 Explained Copyright © 2007 Packt Publishing All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews. Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the authors, Packt Publishing, nor its dealers or distributors will be held liable for any damages caused or alleged to be caused directly or indirectly by this book. Packt Publishing has endeavored to provide trademark information about all the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information. First published: July 2007 Production Reference: 1230707 Published by Packt Publishing Ltd. 32 Lincoln Road Olton Birmingham, B27 6PA, UK. ISBN 978-1-847190-98-7 www.packtpub.com Cover Image by www.visionwt.com Credits Authors Project Coordinator Bogdan Brinzarea Abhijeet Deobhakta Cristian Darie Indexer Reviewers Bhushan Pangaonkar Ruben CCoorrddoobbaa Cosmin Mihai Gheorghita Proofreaders Chris Smith Development Editor Martin Brooks Douglas Paterson Technical Editor Production Coordinator Rajlaxmi Nanda Manjiri Nadkarni Editorial Manager Cover Designer Dipali Chittar Manjiri Nadkarni Project Manager Patricia Weir About the Authors Bogdan Brinzarea-Iamandi has a strong background in Computer Science holding Master and Bachelor Degrees at the Automatic Control and Computers Faculty of the Politehnica University of Bucharest, Romania, and also an Auditor diploma at the Computer Science department at Ecole Polytechnique, Paris, France. His main interests cover a wide area from embedded programming, distributed and mobile computing, and new web technologies. Currently, he is employed as Supervisor within the Alternative Channels Sector team of the IT Division in Banca Romaneasca, a Member of National Bank of Greece. He is Project Manager for Internet Banking and he coordinates other projects related to new technologies and applications to be implemented in the banking area. This is Bogdan's second book on AJAX, following the popular AJAX and PHP: Building Responsive Web Applications, also published by Packt. Cristian Darie is a software engineer with experience in a wide range of modern technologies, and the author of numerous technical books. Cristian currently lives in Bucharest, Romania, studying distributed application architectures for his PhD. He's getting involved with various commercial and research projects, and when not planning to buy Google, he enjoys his bit of social life. If you want to say "hi" you can reach Cristian through his personal website at http://www.cristiandarie.ro. About the Reviewers Ruben Cordoba graduated from Merida University (Spain) and has a Bachelor Degree in Technical Engineering of Computer Systems. Programming and web development are his passions. He has loved computers from the moment his dad presented him his first computer, an AMSTRAD CPC464 with green screen, at the age of six. When he is not programming, Ruben likes walking his dog along the beach, getting inspired with new ideas to code. He lives in the South of Spain and works as a web programmer. Cosmin Mihai Gheorghita is working as a computer programmer for a small and dynamic Romanian company. His skills range from low-level programming using microcontrollers to high-level web development. Although he likes to experiment with hot technologies such as Ruby On Rails, he has specialized in .NET technologies with an emphasis on C# and ASP.NET. Table of Contents Preface 1 Chapter 1: AJAX and ASP.NET 7 The Big Picture 8 AJAX and Web 2.0 10 Building Websites Since 1990 11 HTTP and HTML 11 ASP.NET and Other Server-Side Technologies 13 JavaScript and Other Client-Side Technologies 14 What's Missing? 15 The World of AJAX 15 What is AJAX Made Of? 17 Uses and Misuses of AJAX 18 Introducing ASP.NET AJAX 20 Resources and Tools 21 Setting Up Your Environment 22 Installing IIS 22 Installing Visual Web Developer 25 Creating a Folder for Your Project 25 Preparing the Atlas Application in Windows Vista 26 Preparing the Atlas Web Application in Windows XP 27 Hello World! 28 Time for Action—Quickstart AJAX 31 What Just Happened? 35 Summary 43 Chapter 2: AJAX Foundations 45 JavaScript and the Document Object Model 45 Time for Action—Playing with JavaScript and the DOM 48 What Just Happened 50 Table of Contents JavaScript Events and the DOM 51 Time for Action—Using JavaScript Events and the DOM 53 What Just Happened? 54 Even More DOM 56 Time for Action—Even More DOM 57 What Just Happened? 59 JavaScript, DOM, and CSS 61 Time for Action—Working with CSS and JavaScript 61 What Just Happened? 64 The XMLHttpRequest Object 65 Creating the XMLHttpRequest Object 65 Initiating Server Requests 69 Handling the Server Response 72 Time for Action—Making Asynchronous Calls with XMLHttpRequest 74 What Just Happened? 77 Summary 80 Chapter 3: Object-Oriented JavaScript 81 Concepts of Object-Oriented Programming 81 Objects and Classes 82 Encapsulation 83 Inheritance 83 Polymorphism 84 Object-Oriented JavaScript 84 JavaScript Functions 85 Functions as Variables 86 Anonymous Functions 88 Inner Functions and JavaScript Closures 89 JavaScript Classes 90 Class Diagrams 93 C# and JavaScript Classes 93 Referencing External Functions 96 Thinking of Objects as Associative Arrays 96 Creating Object Members on the Fly 98 Private Members 99 Prototypes 100 The JavaScript Execution Context 101 var x, this.x, and x 102 Using the Right Context 103 Inheritance using Closures and Prototypes 105 Inheritance Using Closures 105 Inheritance Using Prototyping 108 Introducing JSON 110 Summary 112 [ ii ] Table of Contents Chapter 4: Introducing the Microsoft AJAX Library 113 Microsoft AJAX Library Components 113 Asynchronous Communication 116 Client Asynchronous Communication 117 Server Asynchronous Communication 118 Working with WebRequest 119 Time for Action—WebRequest 120 What Just Happened? 125 More WebRequests 129 Time for Action—More WebRequest 130 What Just Happened? 132 Summary 133 Chapter 5: OOP with the Microsoft AJAX Library 135 The New Features 136 JavaScript Base Classes Extensions 136 Time for Action: Bubble Sort and Base Classes Extensions 137 What Just Happened? 140 Classes in Microsoft AJAX Library 142 The Type, Namespaces, and Events 143 Time for Action—Creating and Using the Person Class 144 What Just Happened? 148 Inheritance 154 Time for Action—Implementing Inheritance using Microsoft 156 What Just Happened? 161 AJAX Library Enumerations 165 Interfaces 166 Ti me for action—Inheritance and Interfaces 167 What Just Happened? 173 OOP Recommendations 175 Summary 175 Chapter 6: Creating Client Components 177 DOM Elements and Events 177 Components, Behaviors, and Controls 180 Creating Components 182 Disposing of Components 183 Sys.Application and Client Page Life-Cycle Events 184 The init Event 186 The load Event 186 The pageLoad() Method 187 The pageUnload() Method 188 The unload Event 188 [ iii ]
Description: