Beginning ASP.neT WeB PAgeS WiTh WeBMATrix® FOreWOrD . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxiii inTrODucTiOn . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxv chAPTer 1 Getting Started with WebMatrix . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 chAPTer 2 Designing Web Pages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21 chAPTer 3 Designing Websites . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53 chAPTer 4 Programming Your Site . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77 chAPTer 5 Adding User Interactivity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115 chAPTer 6 AJAX and jQuery . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .147 chAPTer 7 Packages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 179 chAPTer 8 Working with Files and Images . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 197 chAPTer 9 Debugging and Error Handling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 219 chAPTer 10 Introduction to Data and Databases . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 243 chAPTer 11 A Deeper Dive Into Data Access . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 271 chAPTer 12 Security . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 305 chAPTer 13 Optimizing Your Site . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 333 chAPTer 14 Deploying Your Site . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 357 APPenDix Exercise Answers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 375 inDex . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 385 Beginning ASP.neT Web Pages with WebMatrix® Beginning ASP.neT Web Pages with WebMatrix® Mike Brind Imar Spaanjaars Beginning ASP.neT Web Pages with WebMatrix® Published by John Wiley & Sons, Inc. 10475 Crosspoint Boulevard Indianapolis, IN 46256 www.wiley.com Copyright © 2011 by John Wiley & Sons, Inc., Indianapolis, Indiana Published simultaneously in Canada ISBN: 978-1-118-05048-4 ISBN: 978-1-118-20348-4 (ebk) ISBN: 978-1-118-20350-7 (ebk) ISBN: 978-1-118-20349-1 (ebk) Manufactured in the United States of America 10 9 8 7 6 5 4 3 2 1 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 Permissions Department, John Wiley & Sons, Inc., 111 River Street, Hoboken, NJ 07030, (201) 748-6011, fax (201) 748-6008, 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 pro- motional 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 pub- lisher nor the author shall be liable for damages arising herefrom. The fact that an organization or Web site 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 Web site may provide or recommendations it may make. Further, readers should be aware that Internet Web sites 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 (877) 762-2974, outside the United States at (317) 572-3993 or fax (317) 572-4002. Wiley also publishes its books in a variety of electronic formats and by print-on-demand. Not all content that is available in standard print versions of this book may appear or be packaged in all book formats. If you have purchased a version of this book that did not include media that is referenced by or accompanies a standard print version, you may request this media by visiting http://booksupport.wiley.com. For more information about Wiley products, visit us at www.wiley.com. Library of Congress Control Number: 2011936929 Trademarks: Wiley, the Wiley logo, Wrox, the Wrox logo, Wrox 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 coun- tries, and may not be used without written permission. WebMatrix is a registered trademark of Microsoft Corporation. All other trademarks are the property of their respective owners. John Wiley & Sons, Inc., is not associated with any product or vendor mentioned in this book. ABOuT The AuThOrS Mike BrinD spent the first 20 years of his working life in a series of successful sales and marketing roles, towards the end of which he was introduced to HTML and databases. A dormant inner geek took over and Mike became very much more interested in developing websites than selling advertis- ing space on them. As well as on books such as those in the Wrox Beginner series, Mike became reliant on the enormous amount of free help provided by online communities while he learned his new craft. Mike is now one of the all-time leading contributors to the official ASP.NET forums at http://forums.asp.net and is also a moderator there. As a result of his contributions to the ASP.NET community via the forums, and through his technical article site at www.mikesdotnetting.com, Mike received the Microsoft Most Valuable Professional (MVP) Award for ASP.NET in 2008, 2009, 2010, and 2011. Beginning ASP.NET Web Pages with WebMatrix is Mike’s first book. Mike is currently the Operations Director for ITEC Systems, a company that produces specialist software for recruitment agencies all around the world. Mike lives in Rochester in the UK with his wife of over 25 years, Anna. Together they have two daughters and a grandson. iMAr SPAAnjAArS graduated in Leisure Management at the Leisure Management School in the Netherlands, but he quickly changed his career path into the Internet world. After working in the Internet business at various web agencies for thirteen years, he started up his own company called De Vier Koeden (http://devierkoeden.com), a small Internet agency specializing in consultancy and development of Internet and intranet applications with Microsoft technologies such as ASP.NET 4. He’s also the technical director of Dynamicweb Netherlands and Dynamicweb North America, two branches of the popular Danish CMS and eCommerce system Dynamicweb. Imar has written books on ASP.NET and Macromedia Dreamweaver, all published under the Wrox brand. He is also one of the top contributors to the Wrox Community Forum at p2p.wrox.com, where he shares his knowledge with fellow programmers. Since 2008, Imar has been rewarded with Microsoft’s Most Valuable Professional (MVP) award for his contributions to the ASP.NET community. Imar lives in Utrecht, the Netherlands, with his girlfriend, Fleur. You can contact him through his personal web site at http://imar.spaanjaars.com or by email at [email protected].