ebook img

Programming ASP.NET AJAX PDF

461 Pages·2008·6.84 MB·English
by  
Save to my drive
Quick download
Download
Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.

Preview Programming ASP.NET AJAX

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 [email protected]. 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 Preface 1 The Wikipedia page for Ajax (http://en.wikipedia.org/wiki/Ajax) provides more than 40 meanings for the word, including the names of two characters in Homer’s Iliad (Ajax the Great and Ajax the Lesser), the name of an Amsterdam soccer team, a couple of automobiles, a horse, and—my personal favorite—a household cleaner made by Colgate. However, Ajax is also the term for a collection of technologies manysaycouldrevolutionizetheWeb.Ifvariousweblogsandonlineandprintcom- mentaries are to be believed, Ajax is the future of web development, the enabler of Web 2.0, and probably a cure for fatal diseases, as well. Many web developers want to provide their users with a far richer client experience butdon’twanttowriteaWindowsclientapplication(or,forpracticalreasons,can- not write one). Ajax could be just what they need. It allows web applications to behave almost like desktop applications, with features such as keyboard shortcuts and drag-and-drop placement. ASP.NET “Atlas” was the code name for a new set of technologies from Microsoft that provide Ajax-like functionality for the ASP.NET developer. It offered many of thesamebenefitsforAjaxdevelopmentthatASP.NETprovidesforserver-sidedevel- opment. In autumn of 2006, the final product name was announced: ASP.NET AJAX. (However, Atlasis much easier to pronounce.) I resisted writing about Ajax for quite some time. For years, I had used and written about the technologies that make up Ajax, but the term itself had to be coined in early2005beforethetechnologyreallytookoff.Inmyopinion,ClemensVasterssaid it best: “Web 2.0 yadda yadda AJAX yaddayadda Profit!(?)” (see http://vasters.com/ clemensv/PermaLink,guid,d88c1112-d8da-496e-9fd0-8cf03cf55c32.aspx). The hype reminds me of the buzz that accompanied XML and web services a few years back: everybody was talking about them, but few had ever read their specs. Once reality settled in, the hype vanished and actual real-world applications appeared that made effective use of both technologies. xi IamconvincedthatAjaxwillfollowasimilarpathbutwilltravelitmorequickly.A tour of the Web will prove that there are already loads of useful Ajax applications available today. But, back to my reluctance to write a book about Ajax. I kept saying that Ajax itself could be explained in 20 to 30 pages. Adding some background information and examples might produce 75 pages, maybe 100. But how could I fill the rest of the book? Many of the Ajax books currently on the mar- ket have to go through contortions to reach a reasonable page count. My thinking about all of this changed when in September 2005 I attended the Microsoft Professional Developers Conference conference in Los Angeles and saw Atlas for the first time. Microsoft was announcing a framework that provided Ajax functionality but added controls and other tools to make development of modern webapplicationseasier.Nowthiswassomethingtowriteabout,Ithought.Istarted workingonamanuscriptbasedontheearly,prereleaseversionofAtlas.Ithadtobe rewritten several times with every new prerelease drop of Atlas I could get my hands on. The lack of documentation for the preliminary releases required me to reverse-engineer the inner workings of Atlas. As a result, this book may describe a few unofficial ways to accomplish things. ProgrammingAtlaswaspublishedinSeptemberof2006.Asoneofthefirstbookson the topic, it provided detailed information on the yet still changing framework. At theendofJanuary2007,ASP.NETAJAXwasreleasedinitsfinal1.0version.Beyond thenamechange,theinternalchangesweresovastitactuallyrequiredanewedition of the book as each and every existing application had to be adapted. This book will teach you how to create professional, dynamic web pages using the MicrosoftASP.NETAJAXframework.AcertainamountofJavaScriptandASP.NET knowledge is required. For your convenience, some JavaScript basics are covered at the beginning of the book. Iamabigbelieverinthe“show,don’ttell”principle.Therefore,thisbookcontainsa largenumberofexamplesshowingyouthekeyaspectsoftheASP.NETAJAXframe- work.Iamalsoafanoffocusingontherelevantfacts.So,Ihavecreatedsmallexam- ples, each conveying one or two points; I deliberately avoided putting as many facts as possible into one very long listing. In my experience as an author and trainer, shorter examples produce better results and make learning easier. Also, note that the examples are always very generic. This allows you to add them directlytoyourownprojectsandmodifyandtweakthemtomeetyourneeds.Every example is self-contained, making it very easy to use and reuse. xii | Preface Who This Book Is For Thisbookwaswrittenfortwogroupsofwebdevelopers:thosewhoareusingASP.NET and would like to take their applications a step further through the Ajax technology, and those who are using another technology but are interested in the ASP.NET AJAX framework.ItisalsosuitableforJavaScriptprogrammerswhowouldliketoavoidsome oftheheadachescausedbythenecessityofwritingcross-browsercode.Thelanguages used in this book are C# and JavaScript. If you need background on these languages, O’Reilly has some solid introductions to both, including Learning C# 2005, by Jesse Liberty and Brian MacDonald, and Learning JavaScript, by Shelley Powers. How This Book Is Organized Chapter1,ASP.NETAJAX,Ajax,andASP.NET,givesabroadoverviewofAjaxand theASP.NETAJAXframeworkandthencoverstheinstallationofASP.NETAJAX,a review of its structure, and a first simple example. Chapter2, JavaScript, is a concise introduction to JavaScript. Although ASP.NET AJAXdoesitsbesttohidethefunctionaldetailsfromASP.NETprogrammers,acer- tain knowledge of JavaScript is required to really master ASP.NET AJAX. Chapter3, Ajax, explains the technologies beyond the hype. You learn what hap- pens in the background, how Ajax works, and what it really is all about, in fewer than 20 pages. Chapter4, Using ASP.NET AJAX JavaScriptExtensions, describes how ASP.NET AJAX enriches the functionality of client-side JavaScript by adding new OOP-like features and even reimplementing some classes of the .NET Framework so they can be used on the client side. Chapter5,WebServices,dealswithXMLwebservices.EventhoughASP.NETAJAX focuses on client-based development, it also adds features for server-side web ser- vices. This includes features for error management and session support. Chapter6, UpdatePanel: Refreshing Only PartsofaPage, introduces the UpdatePanel control that makes individual parts of a web page updateable independent from the rest of the page, without a page refresh. This is one of the most important elements of ASP.NET AJAX. Chapter7, Using the ASP.NET AJAX Profile Service, details how ASP.NET AJAX provides a JavaScript access to the ASP.NET 2.0 Profile API. Chapter8, Using the ASP.NET AJAX AuthenticationService, explains the JavaScript hook into the ASP.NET 2.0 Forms Authentication API. Preface | xiii

Description:
O'Reilly books may be purchased for educational, business, or sales .. has some solid introductions to both, including Learning C# 2005, by Jesse. Liberty and Brian MacDonald, and Learning JavaScript, by Shelley Powers.
See more

The list of books you might like

Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.