ebook img

ASP.NET Bible PDF

686 Pages·2002·7.29 MB·English
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 ASP.NET Bible

ASP.NET Bible by mridula Parihar and et al. ISBN: 0764548166 Hungry Minds © 2002 (855 pages) Learn the ins and outs of ASP.NET using Visual Basic and C# with this comprehesive reference tool. Table of Contents ASP.NET Bible Preface Part I - ASP.NET Basics Chapter 1 - Understanding the .NET Framework Chapter 2 - Getting Started with ASP.NET Chapter 3 - Building Forms with Web Controls Chapter 4 - Using Rich Web Controls Chapter 5 - Creating and Using Custom Controls Chapter 6 - Validating User Input Chapter 7 - Debugging ASP.NET Pages Part II - ASP.NET Database Programming Chapter 8 - Introducing ADO.NET Chapter 9 - Understanding Data Binding Chapter 10 - Working with Data Grids Chapter 11 - Using Templates Chapter 12 - Using SQL Server with ASP.NET Chapter 13 - Advanced Data Binding and XML Part III - Advanced ASP.NET Chapter 14 - ASP.NET Application Configuration Chapter 15 - Developing Business Objects Chapter 16 - Building HTTP Handlers Chapter 17 - Understanding Caching Chapter 18 - Building Wireless Applications with ASP.NET Mobile Controls Chapter 19 - ASP.NET Security Chapter 20 - Localizing ASP.NET Applications Chapter 21 - Deploying ASP.NET Applications Part IV - Building Web Services Chapter 22 - Introduction to Web Services Chapter 23 - Web Services Infrastructure Chapter 24 - Understanding SOAP Chapter 25 - Building a Web Service Chapter 26 - Deploying and Publishing Web Services Chapter 27 - Finding Web Services Chapter 28 - Consuming Web Services Part V - Building ASP.NET Applications Chapter 29 - ASP.NET Blackjack Chapter 30 - Chatty Discussion Forum Appendix A - Visual Basic Syntax Appendix B - Visual Basic Functions and Features Appendix C - Visual Basic Object-Oriented Programming Appendix D - C# Syntax Appendix E - C# Classes Appendix F - C# Components Index List of Figures List of Tables List of Sidebars ASP.NET Bible Mridula Parihar et al. Published by Hungry Minds, Inc. 909 Third Avenue New York, NY 10022 www.hungryminds.com Copyright © 2002 Hungry Minds, Inc. All rights reserved. No part of this book, including interior design, cover design, and icons, may be reproduced or transmitted in any form, by any means (electronic, photocopying, recording, or otherwise) without the prior written permission of the publisher. Library of Congress Control Number: 2001093388 ISBN: 0-7645-4816-6 10 9 8 7 6 5 4 3 2 1 1B/SQ/QS/QS/IN Distributed in the United States by Hungry Minds, Inc. Distributed by CDG Books Canada Inc. for Canada; by Transworld Publishers Limited in the United Kingdom; by IDG Norge Books for Norway; by IDG Sweden Books for Sweden; by IDG Books Australia Publishing Corporation Pty. Ltd. for Australia and New Zealand; by TransQuest Publishers Pte Ltd. for Singapore, Malaysia, Thailand, Indonesia, and Hong Kong; by Gotop Information Inc. for Taiwan; by ICG Muse, Inc. for Japan; by Intersoft for South Africa; by Eyrolles for France; by International Thomson Publishing for Germany, Austria, and Switzerland; by Distribuidora Cuspide for Argentina; by LR International for Brazil; by Galileo Libros for Chile; by Ediciones ZETA S.C.R. Ltda. for Peru; by WS Computer Publishing Corporation, Inc., for the Philippines; by Contemporanea de Ediciones for Venezuela; by Express Computer Distributors for the Caribbean and West Indies; by Micronesia Media Distributor, Inc. for Micronesia; by Chips Computadoras S.A. de C.V. for Mexico; by Editorial Norma de Panama S.A. for Panama; by American Bookshops for Finland. For general information on Hungry Minds' products and services please contact our Customer Care department within the U.S. at 800-762-2974, outside the U.S. at 317- 572-3993 or fax 317-572-4002. For sales inquiries and reseller information, including discounts, premium and bulk quantity sales, and foreign-language translations, please contact our Customer Care department at 800-434-3422, fax 317-572-4002 or write to Hungry Minds, Inc., Attn: Customer Care Department, 10475 Crosspoint Boulevard, Indianapolis, IN 46256. For information on licensing foreign or domestic rights, please contact our Sub-Rights Customer Care department at 212-884-5000. For information on using Hungry Minds' products and services in the classroom or for ordering examination copies, please contact our Educational Sales department at 800- 434-2086 or fax 317-572-4005. For press review copies, author interviews, or other publicity information, please contact our Public Relations department at 317-572-3168 or fax 317-572-4168. For authorization to photocopy items for corporate, personal, or educational use, please contact Copyright Clearance Center, 222 Rosewood Drive, Danvers, MA 01923, or fax 978-750-4470. LIMIT OF LIABILITY/DISCLAIMER OF WARRANTY: THE PUBLISHER AND AUTHOR HAVE USED THEIR BEST EFFORTS IN PREPARING THIS BOOK. THE PUBLISHER AND AUTHOR MAKE NO REPRESENTATIONS OR WARRANTIES WITH RESPECT TO THE ACCURACY OR COMPLETENESS OF THE CONTENTS OF THIS BOOK AND SPECIFICALLY DISCLAIM ANY IMPLIED WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. THERE ARE NO WARRANTIES WHICH EXTEND BEYOND THE DESCRIPTIONS CONTAINED IN THIS PARAGRAPH. NO WARRANTY MAY BE CREATED OR EXTENDED BY SALES REPRESENTATIVES OR WRITTEN SALES MATERIALS. THE ACCURACY AND COMPLETENESS OF THE INFORMATION PROVIDED HEREIN AND THE OPINIONS STATED HEREIN ARE NOT GUARANTEED OR WARRANTED TO PRODUCE ANY PARTICULAR RESULTS, AND THE ADVICE AND STRATEGIES CONTAINED HEREIN MAY NOT BE SUITABLE FOR EVERY INDIVIDUAL. NEITHER THE PUBLISHER NOR AUTHOR SHALL BE LIABLE FOR ANY LOSS OF PROFIT OR ANY OTHER COMMERCIAL DAMAGES, INCLUDING BUT NOT LIMITED TO SPECIAL, INCIDENTAL, CONSEQUENTIAL, OR OTHER DAMAGES. Trademarks: Hungry Minds and the Hungry Minds logo are trademarks or registered trademarks of Hungry Minds, Inc. All other trademarks are the property of their respective owners. Hungry Minds, Inc., is not associated with any product or vendor mentioned in this book. is a trade mark of Hungr y Minds, Inc. Credits Acquisitions Editor Sharon Cox Project Editor Sharon Nash Development Editor Nancy D. Warner Technical Editor Michael MacDonald Copy Editor Bill McManus Editorial Manager Mary Beth Wakefield Vice President and Executive Group Publisher Richard Swadley Vice President and Publisher Joseph B. Wikert Project Coordinator Bill Ramsey Graphics and Production Specialists Sean Decker Melanie DesJardins Joyce Haughey Barry Offringa Betty Schutte Quality Control Technician Laura Albert David Faust John Greenough Andy Hollandbeck Proofreading and Indexing TECHBOOKS Production Services About the Authors Mridula Parihar has a master's degree in Applied Operations Research from Delhi University. She is a Microsoft Certified Solution Developer (MCSD) and has worked approximately 2½ years with NIIT Ltd. Mridula spent her first year in the Career Education Group (CEG) of NIIT, in which she taught NIIT students and was involved in scheduling and managing resources. For the past 1½ years, she has been working in the Knowledge Solutions Business (KSB) group of NIIT, in which she has had the opportunity to work on varied technical projects. Mridula has been involved in design, development, testing, and implementation of instructor-led training courses. Her primary responsibilities include instructional review, technical review, and ensuring ISO compliance. For the past six months, Mridula has been involved in textbook writing on varied technical subjects such as TCP/IP and .NET. Essam Ahmed is an accomplished developer with more than 10 years of professional programming and design experience. Essam enjoys writing — his publications include JScript .NET Programming (published October 2001 by Hungry Minds), more than 50 book reviews at codeguru.com, and various articles at asptoday.com and thecodechannel.com. Essam's professional affiliations include the IEEE Computer Society (http://computer.org), the Association for Computing Machinery (http://acm.org), and the Worldwide Institute of Software Architects (www.wwisa.org), in which he is a practicing member. Essam was a speaker at VSLive 2001 in Sydney, Australia. Essam's Web site is www.designs2solutions.com. Jim Chandler is an independent consultant with extensive experience in architecting and developing custom, integrated software solutions for small to medium-sized businesses in the Midwest. Before focusing his career on the Windows platform, Jim was a Graphics Partner at Digital Equipment Corporation, evangelizing X11 and Motif. Jim is also a co-author of the Visual Basic.NET Bible and an active member of the St. Louis .NET Users Group. He has delivered presentations on such topics as ASP.NET, XML, and Web services to the St. Louis developer community. His research interests include everything .NET as well as COM+ and developing mobile applications for the Pocket PC. Outside the daily challenges of developing applications and fulfilling his research interests, Jim shares his spare time with his wife, Rhonda, and their two sons, Sam and Thomas. Bill Hatfield is the best-selling author of several computer books, including ASP.NET For Dummies, two editions of Active Server Pages For Dummies (on Classic ASP), Visual InterDev For Dummies, and Creating Cool VBScript Web Pages (all from Hungry Minds). He is also the editor of three monthly publications from Pinnacle Publishing on .NET technologies: C# Developer, .NET Developer, and ActiveWeb Developer. He's an independent corporate trainer and maintains a Web site (www.edgequest.com) dedicated to helping developers solve problems. He works from his home in Indianapolis, Indiana, where he and his wife have celebrated the birth of their first child, Bryce Christopher. Of course, now that he has a real baby, he can stop dressing up the cat in little outfits. Richard Lassan is a Senior Consultant with GA Sullivan in Nashville, TN. He is currently involved in learning and writing about .NET. Richard can be reached at [email protected]. Peter B. MacIntyre lives and works in Charlottetown, Prince Edward Island, Canada (the home of the fictional Anne of Green Gables). Peter and his wife, Dawn, are trying to raise four kids: Daniel (14), Charity (12), Michael (12), and Simon (11). He has been in the software development industry for over 12 years and has seen many changes in technology in that time frame. Peter can be reached at [email protected] and is available for small to midsized consulting projects. Dave Wanta has been working in Web development since the mid-1990s. His Microsoft- centric Web technology has led him to develop a number of large e-commerce and B2B applications. His love for ASP.NET has led him to build the largest online ASP.NET directory, at www.123aspx.com. You can usually find Dave online discussing ASP.NET topics at the lists found at www.aspfriends.com. About the Series Editor Michael Lane Thomas is an active development-community and computer-industry analyst who presently spends a great deal of time spreading the gospel of Microsoft .NET in his current role as a .NET technology evangelist for Microsoft. In working with over a half-dozen publishing companies, Michael has written numerous technical articles and written or contributed to almost 20 books on numerous technical topics, including Visual Basic, Visual C++, and .NET technologies. He is a prolific supporter of the Microsoft certification programs, having earned his MCSD, MCSE+I, MCT, MCP+SB, and MCDBA. In addition to technical writing, Michael can also be heard over the airwaves from time to time, including two weekly radio programs on Entercom (www.entercom.com/) stations, most often in Kansas City on News Radio 980KMBZ (www.kmbz.com/). He can also occasionally be caught on the Internet doing an MSDN Webcast (www.microsoft.com/usa/webcasts/) discussing .NET, the next generation of Web application technologies. Michael started his journey through the technical ranks back in college at the University of Kansas, where he earned his stripes and a couple of degrees. After a brief stint as a technical and business consultant to Tokyo-based Global Online Japan, he returned to the States to climb the corporate ladder. He has held assorted roles, including those of IT manager, field engineer, trainer, independent consultant, and even a brief stint as Interim CTO of a successful dot-com, although he believes his current role as .NET evangelist for Microsoft is the best of the lot. He can be reached via e-mail at [email protected]. First and foremost, I want to dedicate this book to my Supreme Guide who has always guided me in the right direction. Then, I would like to dedicate this book to my Mom and Dad, without whose support I could have never completed this book. My Mom and Dad have always been a constant source of energy and encouragement for me. Also, I must thank my brothers, Amit and Abhay, who have always given their constant support to me. — Mridula Parihar For my sons, Vikranth and Siddharth — Essam Ahmed I'd like to dedicate this book to my family, whose love and support mean everything to me. I love you all. And, finally, the answer to the question posed by my sons, Sam and Thomas, who asked: "Dad, will our name be in the book, too?" You bet! — Jim Chandler To my lovely wife Melanie for the patience, kindness, and love she showsto our child every hour of every day. Bryce couldn't have picked abetter mom! — Bill Hatfield To my parents, William and Eleanor, for always being there. — Rick Lassan I would like to dedicate the chapters that I wrote to my lovely wife, Dawn Etta Riley. Without your patience and understanding, I would not have been able to pull this off! I know it has been hard sometimes, but reward comes with hard work. I love you very much and appreciate all that you do for me and all that you are to me. — Peter MacIntyre To my Parents, Roy and Terry, and my Brother Bob for being the coolest brother someone could have. — Dave Wanta Preface Introduction of the Microsoft .NET platform has begun a new era in the field of application development that will drive the next-generation Internet. ASP.NET, a part of the Microsoft .NET platform, is a revolutionary programming framework that enables the rapid development of enterprise-class Web applications. It provides the easiest and most scalable way to develop, deploy, and run Web applications that can target any browser or device. In fact, these features are only a few of several that make ASP.NET developers' first choice for creating Web applications. ASP.NET makes application development much easier than it was with classic ASP, and hence it has dramatically improved developers' productivity. ASP.NET enables you to provide richer functionality to your Web applications by writing the least amount of code. The best part is that ASP.NET enables you to select any of the available .NET programming languages. So, unlike classic ASP, in which you had to depend on interpreted VBScript and JScript languages, ASP.NET provides a wide choice of programming languages. In addition to the usual Web applications, ASP.NET allows you to create other types, which enables you to extend your applications' reach to new customers and business partners. For example, XML Web services enable sharing of data across the Internet regardless of the operating system and the programming language — this certainly widens the reach of your applications. Also, ASP.NET provides you with mobile controls that enable your applications to target a large number of mobile Web devices. Deploying server applications has always been a pain for developers. With ASP.NET, however, developers do not need to worry on this front at all, because ASP.NET has dramatically simplified the deployment process. Deploying a Web application simply requires you to copy it to the Web server. In addition to providing a rich developer experience, ASP.NET provide a rich user experience. ASP.NET Web applications are faster than the classic ASP applications. The reason is quite obvious. Unlike classic ASP, in which you relied only on interpreted languages, ASP.NET enables you to use compiled programming languages. Thus, ASP.NET applications do not require an explicit compilation step and, in turn, execute faster. Additionally, ASP.NET has rich caching and state-management features that add to the improved performance of ASP.NET Web applications. In addition to providing high performance and scalability, ASP.NET applications are highly reliable and secure. The greater reliability and security features enable your application users to build more confidence in using them. This book seeks to provide to all Web-application developers a greater understanding of ASP.NET. This book covers ASP.NET from its basic features to its advanced features, such as application configuration, caching, security, localization, XML Web services, and deployment. Icons Used in This Book Each icon used in this book signifies a special meaning. Here's what each icon means: Note Note icons provide supplemental information about the subject at hand but generally something that isn't quite the main idea. Notes are often used to elaborate on a detailed technical point. Tip Tips provide special information or advice. They indicate a more efficient way of doing something or a technique that may not be obvious. Caution Caution icons warn you of a potential problem or error. Cross- Cross-Reference icons direct you to related information Reference in another section or chapter. How This Book Is Organized The basic aim of this book is to provide Web-application developers with insight into ASP.NET features and to enable them to develop rich, secure, and reliable Web applications. The book begins by introducing the basic features of ASP.NET and then moves on to explore the advanced ASP.NET features. This book is divided into five parts. Part I: ASP.NET Basics This part presents the salient features of the .NET Framework and introduces you to the relationship of the .NET Framework with ASP.NET. This part covers the basic features of ASP.NET, including the Web controls, Rich Web controls, and Validation controls. In addition, this part discusses how to create custom Web controls and to use various ASP.NET debugging techniques to debug Web applications. Part II: ASP.NET Database Programming This part provides an overview of the ADO.NET technology. It discusses how to use SQL Server with ASP.NET applications to access and manipulate server-side data. This part also covers data binding with ASP.NET server controls and discusses how to work with Data Grids. Also, it discusses working with XML. Finally, it presents you with Web-server control templates to customize the look and layout of server controls. Part III: Advanced ASP.NET This part covers the advanced features of ASP.NET, such as application configuration, caching, and security. This part discusses how to develop business objects (the .NET components), build HTTP handlers, and develop wireless applications using mobile controls. It also discusses how to develop localized ASP.NET applications. Finally, this part presents you with the deployment of ASP.NET applications. Part IV: Building Web Services This part gives you an overview of Web services, the Web services infrastructure, and the Simple Object Access Protocol (SOAP). It then discusses how to build and deploy Web services. Finally, this part discusses how to find and consume Web services. Part V: Building ASP.NET Applications The Web services section covers the foundational technologies of Web services (such as XML and SOAP) as well as detailed information on how to develop, debug, and consume ASP.NET Web services using the Microsoft .NET Framework and ASP.NET. Appendixes This section of the book provides an overview of Visual Basic .NET and C#. Use this section as a reference for these programming languages. In fact, this section provides an easy way for beginners to get started with ASP.NET by introducing the salient features of Visual Basic .NET and C#. Companion Web Site This book provides a companion Web site from which you can download the code from various chapters. All the code listings reside in a single WinZip file that you can download by going to www.hungryminds.com/extras and selecting the ASP.NET Bible link. After you download the file (ASPNETBible.zip), and if you have WinZip already on your system, you can open it and extract the contents by double-clicking. If you don't currently have WinZip, you can download an evaluation version from www.winzip.com. When extracting the files, use WinZip's default options (confirm that the Use Folder Names option is checked) and extract the ASPNETBible.zip file to a drive on your system that has about 3MB of available space. The extraction process creates a folder called ASPNETBible. As long as the Use Folder Names option is checked in the Extract dialog box, an entire folder structure is created within the ASPNETBible folder. You'll see folders arranged by chapter number, and some of those chapter folders will contain subfolders. If you'd rather download just the code you need from a particular chapter — when you need it — simply click the separate chapter link on the Web site instead of downloading the entire WinZip file. Acknowledgments It's a great feeling to acknowledge the efforts and contributions of each one of those who were involved in the development of this book. I would like to acknowledge the time and effort put in by the teams at both ends, NIIT and Hungry Minds. At NIIT, I would like to convey my special thanks to Ms. Anita Sastry, the Project Manager, and Sunil Kumar Pathak, the Graphics Designer. Without their valuable contributions, this book wouldn't have been possible. Also, I would like to thank Namrata, Rashim, Meeta, Ashok, Nitin, and Yesh for their timely help. Thank you once again for giving a helping hand when it was needed the most. At Hungry Minds, my special thanks go to the Acquisitions Editor, Sharon Cox, and the Project Editor, Sharon Nash, who gave me the opportunity to write this book. Also, I would like to acknowledge the Technical Editors, Ken Cox and Michael MacDonald, and the Copy Editor, Bill McManus, for their valuable input and constant support. Thank you all for your valuable contributions, without which this book wouldn't be possible. — Mridula Parihar Tom Archer — for helping me get my writing career off the ground. Sharon Cox — a great acquisitions editor to work with! Sharon Nash — thanks for your patience. My family — for their ongoing support in all of my endeavors. — Essam Ahmed I would like to thank Bill Evjen for giving me the opportunity to fulfill the lifelong goal of becoming a published author as well as his dedication to the St. Louis .NET Users group — it is an outstanding organization due in large part to his efforts. I would also like to thank Craig Smyth, whose support and encouragement have contributed much to the career success I enjoy today. I am also grateful to Bob Hundman at Novus International for allowing me to "disappear" when I needed to while writing for this book. And, of course, a special thank you to my wife, Rhonda, who was patient and supportive while I worked into the wee hours of many nights and during our family vacation in order to meet the book's deadlines. — Jim Chandler Thanks to Chris Webb, Sharon Cox, Sharon Nash, and everyone else at Hungry Minds who helped put this together. You guys are great to work with! — Bill Hatfield I would like to give thanks and praise to God above for giving me the talents and skills that it takes to be both a computer specialist and an author. Of course, the great people at Hungry Minds who gave me this opportunity need to be mentioned as well, namely Sharon Cox and Sharon Nash. Their attention to detail and occasional prodding are what helped make this project a success! — Peter MacIntyre To my Wife, my "Best Friend" (Karen), for all of her encouragement while I contributed to this book. — Rick Lassan ASP.NET Basics Part I: Chapter List Chapter 1: Understanding the .NET Framework Chapter 2: Getting Started with ASP.NET Chapter 3: Building Forms with Web Controls Chapter 4: Using Rich Web Controls Chapter 5: Creating and Using Custom Controls Chapter 6: Validating User Input Chapter 7: Debugging ASP.Net Pages Understanding the .NET Framework Chapter 1: Overview The Internet revolution of the late 1990s represented a dramatic shift in the way individuals and organizations communicate with each other. Traditional applications, such as word processors and accounting packages, are modeled as stand-alone applications: they offer users the capability to perform tasks using data stored on the system the application resides and executes on. Most new software, in contrast, is modeled based on a distributed computing model where applications collaborate to provide services and expose functionality to each other. As a result, the primary role of most new software is changing into supporting information exchange (through Web servers and browsers), collaboration (through e-mail and instant messaging), and individual expression (through Web logs, also known as Blogs, and e-zines — Web based magazines). Essentially, the basic role of software is changing from providing discrete functionality to providing services. The .NET Framework represents a unified, object-oriented set of services and libraries that embrace the changing role of new network-centric and network-aware software. In fact, the .NET Framework is the first platform designed from the ground up with the Internet in mind. This chapter introduces the .NET Framework in terms of the benefits it provides. I present some sample code in Visual C# .NET, Visual Basic .NET, Visual Basic 6.0, and Visual C++; don't worry if you're not familiar with these languages, since I describe in the discussion what each sample does. Benefits of the .NET Framework The .NET Framework offers a number of benefits to developers: § A consistent programming model § Direct support for security § Simplified development efforts § Easy application deployment and maintenance Consistent programming model Different programming languages offer different models for doing the same thing. For example, the following code demonstrates how to open a file and write a one-line message to it using Visual Basic 6.0: Public Sub testFileAccess() On Error GoTo handle_Error ' Use native method of opening an writing to a file... Dim outputFile As Long outputFile = FreeFile Open "c:\temp\test.txt" For Output As #outputFile Print #outputFile, "Hello World!" Close #outputFile ' Use the Microsoft Scripting Runtime to ' open and write to the file... Dim fso As Object Set fso = CreateObject("Scripting.FileSystemObject") Dim outputText As TextStream Set outputText = fso.CreateTextFile("c:\temp\test2.txt") outputText.WriteLine "Hello World!" outputText.Close Set fso = Nothing Exit Sub handle_Error: ' Handle or report error here End Sub This code demonstrates that more than one technique is available to create and write to a new file. The first method uses Visual Basic's built-in support; the second method uses the Microsoft Scripting Runtime. C++ also offers more than one way of performing the same task, as shown in the following code: #include <fstream> #include <iostream> #include <cstdlib> #include <stdio.h> using namespace std; int main(int argc, char* argv[]) { // Use the C Runtime Library (CRT)... FILE *testFile; if( (testFile = fopen( "c:\\temp\\test3.txt", "wt" )) == NULL ) { cout << "Could not open first test file!" << endl; return 1; } fprintf(testFile,"Hello World!\n");

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.