Tải bản đầy đủ

1821 beginning ajax with ASP NET

www.it-ebooks.info


Beginning

Ajax with ASP.NET
Wallace B. McClure, Scott Cate, Paul Glavich, Craig Shoemaker

www.it-ebooks.info


www.it-ebooks.info


Beginning

Ajax with ASP.NET

www.it-ebooks.info



www.it-ebooks.info


Beginning

Ajax with ASP.NET
Wallace B. McClure, Scott Cate, Paul Glavich, Craig Shoemaker

www.it-ebooks.info


Beginning Ajax with ASP.NET
Published by
Wiley Publishing, Inc.
10475 Crosspoint Boulevard
Indianapolis, IN 46256
www.wiley.com
Copyright © 2006 by Wiley Publishing, Inc., Indianapolis, Indiana
Published simultaneously in Canada
ISBN-13: 978-0-471-78544-6
ISBN-10: 0-471-78544-X
Manufactured in the United States of America
10 9 8 7 6 5 4 3 2 1
1B/QT/QY/QW/IN
Library of Congress Control Number: 2006016507
No part of this publication may be reproduced, stored in a retrieval system or transmitted in any form or by any
means, electronic, mechanical, photocopying, recording, scanning or otherwise, except as permitted under Sections 107 or 108 of the 1976 United States Copyright Act, without either the prior written permission of the Publisher, or authorization through payment of the appropriate per-copy fee to the Copyright Clearance Center, 222
Rosewood Drive, Danvers, MA 01923, (978) 750-8400, fax (978) 646-8600. Requests to the Publisher for permission should be addressed to the Legal Department, Wiley Publishing, Inc., 10475 Crosspoint Blvd., Indianapolis,
IN 46256, (317) 572-3447, fax (317) 572-4355, or online at http://www.wiley.com/go/permissions.
LIMIT OF LIABILITY/DISCLAIMER OF WARRANTY: THE PUBLISHER AND THE AUTHOR MAKE NO
REPRESENTATIONS OR WARRANTIES WITH RESPECT TO THE ACCURACY OR COMPLETENESS OF
THE CONTENTS OF THIS WORK AND SPECIFICALLY DISCLAIM ALL WARRANTIES, INCLUDING
WITHOUT LIMITATION WARRANTIES OF FITNESS FOR A PARTICULAR PURPOSE. NO WARRANTY
MAY BE CREATED OR EXTENDED BY SALES OR PROMOTIONAL MATERIALS. THE ADVICE AND
STRATEGIES CONTAINED HEREIN MAY NOT BE SUITABLE FOR EVERY SITUATION. THIS WORK IS
SOLD WITH THE UNDERSTANDING THAT THE PUBLISHER IS NOT ENGAGED IN RENDERING
LEGAL, ACCOUNTING, OR OTHER PROFESSIONAL SERVICES. IF PROFESSIONAL ASSISTANCE IS
REQUIRED, THE SERVICES OF A COMPETENT PROFESSIONAL PERSON SHOULD BE SOUGHT.
NEITHER THE PUBLISHER NOR THE AUTHOR SHALL BE LIABLE FOR DAMAGES ARISING HEREFROM. THE FACT THAT AN ORGANIZATION OR WEBSITE IS REFERRED TO IN THIS WORK AS A
CITATION AND/OR A POTENTIAL SOURCE OF FURTHER INFORMATION DOES NOT MEAN THAT


THE AUTHOR OR THE PUBLISHER ENDORSES THE INFORMATION THE ORGANIZATION OR
WEBSITE MAY PROVIDE OR RECOMMENDATIONS IT MAY MAKE. FURTHER, READERS SHOULD BE
AWARE THAT INTERNET WEBSITES LISTED IN THIS WORK MAY HAVE CHANGED OR DISAPPEARED BETWEEN WHEN THIS WORK WAS WRITTEN AND WHEN IT IS READ.
For general information on our other products and services please contact our Customer Care Department
within the United States at (800) 762-2974, outside the United States at (317) 572-3993 or fax (317) 572-4002.
Trademarks: Wiley, the Wiley logo, Wrox, the Wrox logo, Programmer to Programmer, and related trade
dress are trademarks or registered trademarks of John Wiley & Sons, Inc. and/or its affiliates, in the United
States and other countries, and may not be used without written permission. All other trademarks are the
property of their respective owners. Wiley Publishing, Inc., is not associated with any product or vendor
mentioned in this book.
Wiley also publishes its books in a variety of electronic formats. Some content that appears in print may not
be available in electronic books.

www.it-ebooks.info


Credits
Executive Editor

Vice President and Executive Publisher

Bob Elliott

Joseph B. Wikert

Senior Development Editor

Graphics and Production Specialists

Kevin Kent

Carrie A. Foster
Lauren Goddard
Joyce Haughey
Barbara Moore
Heather Ryan
Alicia B. South

Technical Editor
Steven A. Smith

Production Editor
Pamela Hanley

Quality Control Technicians
Foxxe Editorial Services

John Greenough
Brian Walls

Editorial Manager

Project Coordinator

Mary Beth Wakefield

Jennifer Theriot

Production Manager

Proofreading and Indexing

Tim Tate

Techbooks

Copy Editor

Vice President and Executive Group Publisher
Richard Swadley

www.it-ebooks.info


For my wife, Ronda, my two children, Kirsten and Bradley, and the rest of my family.
—Wallace B. McClure
My contribution to this project is dedicated to my newborn son, Cameron. I’d like to thank Michael
Schwarz both for authoring the Ajax.NET Professional library and for his research assistance, as well
as all of the myKB.com staff, who were very helpful and supportive of this project. Special thanks go
to Lorin Thwaits, who helped me with research and provided several great ideas for my chapters.
—Scott Cate
To my wonderful wife Michele for her enduring love and patience; my three children, Kristy, Marc, and
Elizabeth, for being so lovable and great people; my two grandchildren, Olivia and William, for just
being themselves; my loving parents for all their support; and everyone else I have met on the way to
getting where I am, good or bad, thank you for helping me get here.
—Paul Glavich
I dedicate this work first to God, then my Peachy, TyRy, Zachy-zoo, and baby Jacob—who started to
make his appearance as I write this text.
—Craig Shoemaker
For Michelle, for putting up with me longer than anybody should have to do so.
—Steven A. Smith

www.it-ebooks.info


About the Authors
Wallace B. “Wally” McClure graduated from the Georgia Institute of Technology in 1990 with a Bachelor
of Science degree in electrical engineering. He continued his education there, receiving a master’s degree
in the same field in 1991. Since that time, he has done consulting and development for such organizations
as The United States Department of Education, Coca-Cola, Bechtel National, Magnatron, and Lucent
Technologies, among others. Products and services have included work with ASP, ADO, XML, and SQL
Server, as well as numerous applications in the Microsoft .NET Framework. Wally has been working with
the .NET Framework since the summer of 2000. Wally McClure specializes in building applications that
have large numbers of users and large amounts of data. He is a Microsoft MVP and an ASPInsider, and a
partner in Scalable Development, Inc. You can read Wally’s blog at http://weblogs.asp.net/wallym.
Wally and coauthor Paul Glavich also co-host the ASP.NET Podcast. You can listen to it at www.aspnet
podcast.com. In addition, Wally travels around the southeast United States doing user group talks and
sessions at various CodeCamps.
When not working or playing with technology, Wally tries to spend time with his wife Ronda and their
two children, Kirsten and Bradley. Occasionally, Wally plays golf and on July 30, 2005, broke par on a
real golf course for the first time in his life. If he hadn’t been there, he would not have believed it.
Scott Cate is the President of myKB.com, Inc., in Scottsdale, Arizona. myKB.com, Inc., is a technology company specializing in commercial ASP.NET applications. His product line includes myKB.com (knowledge
base software), kbAlertz.com (Microsoft knowledge base notifications), and EasySearchASP.net (a pluggable search engine for ASP.NET sites). Scott also runs AZGroups.com (Arizona .NET user groups), one of
the largest and most active user group communities in the country, and is a member of ASPInsiders.com, a
group devoted to giving early feedback to the Microsoft ASP.NET team. In addition, Scott has coauthored
the novel Surveillance, which can be found at http://surveillance-the-novel.com.
Paul Glavich is currently an ASP.NET MVP and works as a senior technical consultant for Readify. He
has over 15 years of industry experience ranging from PICK, C, C++, Delphi, and Visual Basic 3/4/5/6
to his current specialty in .NET C++ with C#, COM+, and ASP.NET. Paul has been developing in .NET
technologies since .NET was first in beta and was technical architect for one of the world’s first Internet
banking solutions using .NET technology. Paul can be seen on various .NET related newsgroups, has
presented at the Sydney .NET user group (www.sdnug.org) and is also a board member of ASPInsiders
(www.aspinsiders.com). He has also written some technical articles that can be seen on community
sites, such as ASPAlliance.com (www.aspalliance.com).
On a more personal note, Paul is married with three children and two grandkids, and holds a third
degree black belt in budo-jitsu.
Craig Shoemaker can’t sit still. As the host of the Polymorphic Podcast (polymorphicpodcast.com),
Craig teaches on topics as timely as software architecture and as cutting edge as the latest Ajax technologies. Whether he’s writing for CoDe Magazine, ASPAlliance, or DotNetJunkies or speaking at local user
groups, Southern California Code Camp, or VSLive!, Craig loves to share his passion for the art and science for software development. Craig is also a full-time software engineer for Microsoft Certified Partner
PDSA, Inc. (pdsa.com) in Tustin, California.

www.it-ebooks.info


About the Technical Editor
Steven A. Smith is president of ASPAlliance.com and DevAdvice.com. He is a Microsoft regional developer, a Microsoft ASP.NET MVP, and an ASPInsiders board member. He is an International .NET
Association (INETA) Speaker Bureau member, and author of two books on ASP.NET. Steve is also an Army
engineer officer and veteran of Operation Iraqi Freedom, where he spent 6 months locating and neutralizing munitions in 2004. He lives in Kent, Ohio, with his wife and business partner, Michelle, and their
daughter, Ilyana. When he is not attached to a computer, Steve enjoys spending time with his family
hiking, biking, and playing games.

www.it-ebooks.info


Acknowledgments
It is truly interesting how writing projects get started and how ideas turn into books. I had seen the hidden frame trick for years (at least 1998), but I always felt that it was too hard to set up and keep running,
so I just dismissed it in general. I saw my first formal Ajax application in the early part of 2003, though it
wasn’t called Ajax then. The application ran in Internet Explorer only and used the MSXML component.
I remember sitting down with the person who had written it as we tried to debug the JavaScript that was
necessary to get everything to work. Though it was only Windows/IE and debugging was very hard,
I could see the potential of this type of development. I always kept it in the back of my mind. With the
release of Google Suggest, Google Maps, and several other web sites and with Jesse James Garrett’s
essay on Ajax, where the coined term became public, Ajax has really taken off as far as developer mindshare. As I watched March 2005 turn into April 2005 and then into June 2005, I wondered if I was missing a an opportunity for a writing project on a killer technology. I started working with the Ajax library
for ASP.NET in June 2005, and I wondered if the topic of Ajax on ASP.NET had enough meat to write
about. With Scott Guthrie’s announcement of Atlas near the end of June 2005, I knew that Ajax was
something that I wanted to be involved with. I spoke with Bob Elliott of Wiley on the Tuesday of Scott
Guthrie’s announcement and probably every day through July 2005 regarding Ajax and writing a book
on Ajax with ASP.NET. From there, things took off. I started talking a lot about Ajax in some online lists
at aspadvice.com. Paul, Scott, and I immediately began talking about Ajax. Paul and I already worked
together on the ASP.NET Podcast (at www.aspnetpodcast.com), and the three of us already knew each
other from the ASPInsider and Microsoft MVP groups to which we belong. Given Scott and Paul’s existing knowledge, it was not hard to convince them to come on board and work on this book.
Personally, I would like to thank Bob Elliott for keeping me focused on what was going on and working
with us to develop this book. Our thanks also go out to the editorial staff at Wiley. Their help keeping us
on track as “life happened” was appreciated. The work of our technical editor, Steven A. Smith, was
impressive, and his attention to detail was great. Many other people behind the scenes have worked
hard on the book. By pulling this group together, Wiley created a team that was dedicated to creating
the best possible book on Ajax on ASP.NET. For that, we are truly appreciative.
~Wallace B. McClure and the author team

www.it-ebooks.info


www.it-ebooks.info


Contents
Acknowledgments
Introduction

ix
xix

Chapter 1: Introduction to Ajax on ASP.NET
Development Trends
ASP.NET Development

1
1
2

Design Methodology
Problems ASP.NET Solves

2
2

So, What’s the Problem?
Improving the User Experience
What Is Ajax?

4
5
6

Advantages of Ajax
History of Ajax
Technologies That Make Up Ajax
Running Ajax Applications

6
7
8
8

Who’s Using Ajax?
Problems Ajax Won’t Solve
Summary

9
9
10

Chapter 2: Introduction to DHTML
What JavaScript Is, and What It Isn’t
General Rules of JavaScript
Writing Your First Block of JavaScript Code
document.write()
Using document.write() in Nested for() Loops
window.status and Events

Getting Input from the User

11
11
12
13
14
14
16

17

Security Concerns When Getting Text Input
Canceling the Normal Outcome of an Event
Causing an Event Not to “Bubble Up”

Working with Images

18
18
19

19

Working with the image src Attribute
Using Functions
Programmatically Populating the Options in