Advanced ASP.NET AJAX Server Controls, For .NET Framework 3.5 (2009)

Advanced ASP.NET AJAX Server Controls, For .NET Framework 3.5 (2009)

(Parte 1 de 5)

Adam Calderon Joel Rumerman

Advanced

Server Controls

For .NET Framework 3.5

Upper Saddle River, NJ •Boston•Indianapolis•San Francisco New York •Toronto •Montreal •London•Munich•Paris Madrid•Cape Town •Sydney•Tokyo •Singapore •Mexico City

Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in this book, and the publisher was aware of a trademark claim, the designations have been printed with initial capital letters or in all capitals.

The .NET logo is either a registered trademark or trademark of Microsoft Corporation in the United States and/or other countries and is used under license from Microsoft.

The authors and publisher have taken care in the preparation of this book, but make no expressed or implied warranty of any kind and assume no responsibility for errors or omissions. No liability is assumed for incidental or consequential damages in connection with or arising out of the use of the information or programs contained herein.

The publisher offers excellent discounts on this book when ordered in quantity for bulk purchases or special sales, which may include electronic versions and/or custom covers and content particular to your business, training goals, marketing focus, and branding interests. For more information, please contact:

U.S. Corporate and Government Sales (800) 382-3419 corpsales@pearsontechgroup.com

For sales outside the United States please contact:

International Sales international@pearson.com

Visit us on the web: w.informit.com/aw Library of Congress Cataloging-in-Publication Data:

Calderon, Adam, 1964- Advanced ASP.Net Ajax server controls for .Net 3.5 / Adam Calderon, Joel Rumerman. p. cm.

ISBN 0-321-51444-0 (pbk. : alk. paper) 1. Internet programming. 2. Active server pages. 3. Microsoft .NET. 4. Ajax (Web site development technology) 5. Web servers. I. Rumerman, Joel, 1980- I. Title.

QA76.625.C34 2008 006.7’882—dc22 2008013462

Copyright © 2009 Pearson Education, Inc.

All rights reserved. Printed in the United States of America. This publication is protected by copyright, and permission must be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval system, or transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. For information regarding permissions, write to:

Pearson Education, Inc. Rights and Contracts Department 501 Boylston Street, Suite 900 Boston, MA02116 Fax (617) 671 3447

ISBN-13: 978-0-321-51444-8 ISBN-10: 0-321-51444-0 Text printed in the United States on recycled paper at R Donnelly in Crawfordsville, Indiana. First printing: July 2008

Editor-in-Chief Karen Gettman

Acquisitions Editor Joan Murray

Development Editors Sheri Cain Chris Zahn

Managing Editor Kristy Hart

Project Editor Jovana San Nicolas-Shirley

Copy Editor Keith Cline

Indexer WordWise Publishing Services

Proofreader Geneil Breeze

Publishing Coordinator Olivia Basegio

Cover Designer Chuti Prasertsith

Compositor Bronkella Publishing

To my wife, Gayle, and my son, Derek: You were already accustomed to my hard work ethic, but I think writing this book took things to the next level. Your patience and understanding during this long journey provided the foundation I needed to get through the rough times. I am truly blessed to have the both of you in my life.

—Adam

To my wife, Stacey: You make even the hard days easy. And to my parents: I truly am 50 percent of each of you.

—Joel

Contents

Foreword xxv Preface xxvii Acknowledgments xxxv About the Authorsxxxix

IClient Code

1Programming with JavaScript3

Generally JavaScript 4

JavaScript Introduction 4 Language Attributes 4 Primitive Data Types6

Strings 8

Objects 9 Variables and Function Arguments16 Error Handling25 Delayed Code Execution Using Timeouts and Intervals30

Object-Oriented JavaScript Programming 39

Abstract Data Types40 Inheritance 46

2Microsoft AJAX Library Programming51

Extending the Built-In JavaScript Types52

Booleans 52 Dates and Numbers53

Cont ent sviii

Strings 54 Arrays 5

Extending the Microsoft AJAX Library60

Classes 60 Interfaces 67 Enumerations 72 Inheritance and Interface Implementation78

Important New Types88

Sys.EventHandlerList 8 Sys.StringBuilder 94 Sys.Debug 96 Sys.UI.DomElement 101 Sys.UI.DomEvent 107

I Controls

Components Defined 121

Components, Controls, and Behaviors122

Sys.Component 125

Defining New Components129 Creating Components 134 Wrapping Up Components151

Controls 151

New Concepts 153 Defining a New Control155 Creating a Control157 Wrapping Up Controls159

Behaviors 159

Defining a Behavior161 Creating a Behavior162 Wrapping Up Behaviors167

Cont ent s ix

Background Information 169

Creating Sys.Application 170 Type Information172 Method Information 172

Component Manager 176

Adding a Component177 Finding a Component179 Removing a Component183 Getting Components 184

Initialization Routine 185

Component Creation Process189 Load Event198

The Unload Routine203 Sys.Application.dispose 203

5Adding Client Capabilities to Server Controls207

Script-Generation Architecture 208

Behavior and Control Script Generation208 Script Resources214 ScriptManager 218

Adding Client-Side Behavior Using the ExtenderControl220

ExtenderControl Overview 221 Creating an Extender Control221

Adding Client-Side Functionality Using the ScriptControl233

ScriptControl Overview 234 Creating a Script Control237

Adding Client-Side Functionality to Composite Controls Using the IScriptControl Interface247

Composite Control Overview247 IScriptControl Interface 249 Creating the Composite Control249

Cont ent sx

6ASP.NETAJAX Localization 255

Localization in ASP.NET255

Determining What Needs to Be Localized257 Running under a Particular Culture261 Localizing Displayed Values267

Localization in ASP.NET AJAX277

JavaScript Localization Capabilities277 ASP.NET AJAX Localization Capabilities280

7Control Development in a Partial Postback Environment317

UpdatePanel Behavior 318 The Effects of a Partial Postback on Client Components 324

Automatic Disposal of Behaviors and Controls 329 Automatic Disposal of Components337 Manual Disposal of a Component, Control, or Behavior340

Loading of JavaScript Statements and Files355

ScriptManager Registration Methods355 Sys.Application.notifyScriptLoaded() 362

Sys.Application Events 364

The init Event364 The load Event365

I Communication

8 ASP.NET AJAX Communication Architecture 371

New Communication Paradigm372 ASP.NET AJAX 2.0 Extensions Communication Architecture374

Microsoft AJAX Library Communication Architecture401

Service Proxies401 Serialization 415

Cont ent s xi

WebRequest 417 Web Request Core421

9 Application Services 425

ASP.NET 2.0 Membership, Role, and User Profile Services425

Forms Authentication 426 ASP.NET 2.0 Provider Model429 Web Site Administration Tool431 Membership 433 Roles 439 Profiles 4

ASP.NET AJAX Application Services448

Authentication Service 448 Role Service453 Profile Service456

Custom Application Services460

HTTP Handler Factory and Supporting Classes462 Service Proxy473 Configuration 477

IVAJAX Control Toolkit

10ASP.NETAJAX Control Toolkit Architecture481

Overview of the Toolkit482

Reliance on Attributes to Simplify Development482 Rich Set of .NET Classes482 Rich Set of JavaScript Classes483 Support for Animations483

Composition of the Toolkit483

Installation 484 Layout of the Solution484

Server-Based Architecture 486

Attributes 486 Base Classes for Extenders and ScriptControls491 Designer Classes495

Cont ent sxii

Client-Based Architecture 499

BehaviorBase Class 500 ControlBase Class 501

Animations 501

Animation Structure and Types502 Client Architecture 503 Server Architecture 509

11Adding Client Capabilities to Server Controls Using the

ASP.NETAJAX Control Toolkit513 Adding Client-Side Behavior Using the ExtenderControlBase513

Visual Studio 2008 Extender Control Library Template514 Inheriting from the ExtenderControlBase Class518 Creating the AjaxControlToolkit.BehaviorBase Class521 Attaching the Extender to a Control523 Final Thoughts523

Adding Design-Time Support to Your Extender Control524

Default Design-Time Experience525 Adding Designers and Editors to Properties526

Adding Animations to Your Extender Control532

Animations Using the JavaScript API533 Animations Using the Declarative Method537

V Appendixes

AJavaScript in Visual Studio 2008547

IntelliSense 547

Referencing Libraries and Web Services548 XMLComments 551

BValidating Method Parameters555

Cont ent s xiii

CASP.NETHandlers and Modules559

ASP.NET Application Lifecycle559 HTTP Handlers 560

Overview of HTTP Handlers561 Overview of HTTP Handler Factory562

HTTP Modules 565 Overview of HTTP Modules565

DClient Error Handling Code569

ErrorHandler Client Class569 ErrorEventArgs Client Class571 ErrorHandler Server Control571 StackTrace Client Class572 ErrorDataService Web Service574 Test Error Page574

Index 577

Cont ent sxiv

Figures

Figure 1.1Output of caught error alert28 Figure 1.2Output of unhandled error alert29 Figure 1.3Output of alert registered with timeout32 Figure 1.4Output of alert after we modified variable b33 Figure 1.5Output of the abc div tag after 7 seconds38

Figure 2.1IntelliSense in Visual Studio 2008 of an implicit namespace62 Figure 2.2IntelliSense in Visual Studio 2008 of a declared namespace62 Figure 2.3Output of sorted books list71 Figure 2.4Output of sorted newspaper list72 Figure 2.5Availability of base class methods80 Figure 2.6Output of the overridden base class method81 Figure 2.7Output of the editorAddedHandler method after adding Tim92 Figure 2.8Output of the editorAddedHandler method after adding Mark92 Figure 2.9Debug test page in action99 Figure 2.10Web Development Helper displaying the trace messages100 Figure 2.11Visual Studio’s output window displaying the trace messages100 Figure 2.12Firebug’s console window displaying the trace messages100 Figure 2.13Safari’s JavaScript console displaying the trace messages100 Figure 2.14Opera’s error console displaying the trace messages101 Figure 2.15The div and span with initial x,y coordinates; after moving x,y coordinates; and the div’s final width and height106

Figure 2.16The span with the highlight removed107 Figure 2.17The undefined error message113 Figure 2.18The object’s _name member114 Figure 2.19Output of _name and context116

Figure 3.1Class hierarchy between Sys.Component, Sys.UI.Behavior, and

Sys.UI.Control 123 Figure 3.2Decision process between component, control, and behavior125

Figure 4.1The Operation Aborted error in Internet Explorer192

Figure 5.1Script-generation class structure209 Figure 5.2Selecting the Embedded Resource option of the build action215 Figure 5.3ExtenderControl class hierarchy222 Figure 5.4ExtenderControl project template223 Figure 5.5ExtenderControl project template structure224 Figure 5.6Extender Control Wizard232 Figure 5.7ImageRotator sample233 Figure 5.8ScriptControl class hierarchy235 Figure 5.9ScriptControl project template238 Figure 5.10ScriptControl project template structure239 Figure 5.1 ImageURL Editor 242 Figure 5.12TextBoxInfo control246 Figure 5.13TextBoxInfo control help alert246 Figure 5.14CompositeControl class hierarchy248

Figure 6.1The Transactions page with what needs to be localized circled257 Figure 6.2Fiddler view of a request’s Accept-Language header262 Figure 6.3Fiddler’s view of a request’s Accept-Language header with multiple languages specified 263

Figure 6.4Transactions page with culture selector264 Figure 6.5Accessing Visual Studio 2008’s Generate Local Resource tool268 Figure 6.6Newly Created Transactions.aspx.resx file in App_LocalResources269 Figure 6.7Resources in Transactions.aspx.resx272

Cont ent sxvi

Figure 6.8BoundFieldResource1 in Transactions.aspx.resx272 Figure 6.9Transactions page using Transactions.aspx.resx273 Figure 6.10The three local resource files274 Figure 6.11The Spanish (Mexico) local resource file275 Figure 6.12The French local resource file275 Figure 6.13The Transactions page under the Spanish (Mexico) culture276 Figure 6.14The Transactions page under the French culture276 Figure 6.15The toLocaleString output of a number when the operating system is running in French279

Figure 6.16The toLocaleString output of a date when the operating system is running in French280

Figure 6.17The localeFormat output of a number when the operating system is running in French284

Figure 6.18The format output of a number when the operating system is running in French284

Figure 6.19The localeFormat output of a date when the operating system is running in French 287

Figure 6.20The format output of a date when the operating system is running in

French 288

Figure 6.21The Transactions page with the amount filter294 Figure 6.22An English error message stating the amount values are invalid294 Figure 6.23AFrench error message stating the amount values are invalid295 Figure 6.24Afocused and unformatted CurrencyTextBox295 Figure 6.25An unfocused and formatted CurrencyTextBox295 Figure 6.26An English error message displayed after entering an invalid number 296

Figure 6.27AFrench error message displayed after entering an invalid number 296

Figure 6.28Our project’s layout in Visual Studio296 Figure 6.29The CurrencyTextBox resource file303 Figure 6.30The InvalidNumberMessage resource304 Figure 6.31Visual Studio with the three resource files304 Figure 6.32The CurrencyTextBox.es-mx.resx resource file304 Figure 6.33The CurrencyTextBox.fr.resx resource file304 Figure 6.34ScriptResource attribute in AssemblyInfo.cs305 Figure 6.35Our new Filter.js file310 Figure 6.36Transactions page with “Invalid From Amount” error message313

Cont ent s xvii

Figure 6.37Localized Filter.js files313 Figure 6.38Script Documents showing Filter.es-mx.js file use314 Figure 6.39Our Spanish error message315

Figure 7.1High-level steps during partial page rendering319 Figure 7.2Sample UpdatePanel page browser display321 Figure 7.3Web Development Helper view of a partial postback322 Figure 7.4Web Development Helper view of a partial postback in text form323 Figure 7.5The initialization message in the Visual Studio Debug window327 Figure 7.6The dispose and second initialization messages in the Visual Studio

Debug window327

Figure 7.7Re-creating Comp 1 during a partial postback328 Figure 7.8UpdatePanel UP1’s content being replaced332 Figure 7.9ImageRotator extender in an UpdatePanel335 Figure 7.10The disposal and initialization of each component339 Figure 7.11The dispose script for the partial postback component340 Figure 7.12AHoverCard’s visual output343 Figure 7.13The HoverCard’s initial location344 Figure 7.14The HoverCard after we’ve dragged it a bit345 Figure 7.15Initial position of the HoverCard in the div tag346 Figure 7.16The HoverCard’s position after we’ve clicked the header346 Figure 7.17The initial view of the page wrapped in an UpdatePanel348 Figure 7.18The JavaScript error after we clicked the Update button349 Figure 7.19The second JavaScript error after we clicked the first error’s Continue button 349

(Parte 1 de 5)

Comentários