Your SlideShare is downloading. ×
0ANTONE                             #-9+                 BUILD YOUR OWN                             REY SCALE    ASP.NET 3...
Thank you for downloading the sample chapters of Build Your Own ASP.NET 3.5Web Site Using C# & VB published by SitePoint.T...
Summary of Contents of this ExcerptPreface...................................................................................
BUILD YOUR OWN    ASP.NET 3.5 WEBSITE USING C# & VB           BY CRISTIAN DARIE           & WYATT BARNETT                 ...
iv     Build Your Own ASP.NET 3.5 Web Site Using C# & VB     by Cristian Darie and Wyatt Barnett                          ...
vAbout the AuthorsCristian Darie is a software engineer with experience in a wide range of modern technologies,and the aut...
To my family and friends.                   —Cristian DarieTo my Father, whose guidance got          me this far.         ...
Table of ContentsPreface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ....
x            Directives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29  ...
xi        Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .   81 ...
xii               Web.config . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 168...
xiii   Validation Groups . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 248   Upd...
xiv               Selecting Unique Data with DISTINCT . . . . . . . . . . . . . . . . . . . . . 310               Row Filt...
xv        Defining the Database Connection . . . . . . . . . . . . . . . . . . . . . . . . . 347        Preparing the Comm...
xvi               Styling the GridView with Templates, Skins, and CSS . . . . . . . . . . 450               Selecting Grid...
xvii   Securing ASP.NET Applications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 548       Working w...
xviii            Using Triggers to Update an UpdatePanel . . . . . . . . . . . . . . . . . . . . . . . . 639            Th...
xix          PlaceHolder . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 665          ...
xx     Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ....
PrefaceWeb development is very exciting. There’s nothing like the feeling you have afteryou place your first dynamic web s...
xxii       By the end of this book, you should be able to successfully download and install       Visual Web Developer 200...
xxiii    loops, arrays, functions, and more. Finally, we’ll see how the two languages    accommodate object oriented progr...
xxiv       Chapter 8: Speaking SQL          This chapter will teach you to speak the language of the database: Structured ...
xxv    also learn to implement features such as paging, filtering, and sorting, using    custom code.Chapter 13: Security ...
xxvi       The Code Archive       As you progress through this book, you’ll note a number of references to the code       ...
xxviiThe SitePoint NewslettersIn addition to books like this one, SitePoint publishes free email newsletters includ­ing Th...
xxviii                                                                                    example.css          .footer {  ...
xxix       Ahem, Excuse Me …     Notes are useful asides that are related—but not critical—to the topic at hand.     Think...
Build Your Own ASP.NET 3.5 Web Site Using C# & VB (www.sitepoint.com)
1                                                 ChapterIntroducing ASP.NET andthe .NET PlatformASP.NET is one of the mos...
2   Build Your Own ASP.NET 3.5 Web Site Using C# & VB    What is ASP.NET?    ASP.NET is a sophisticated and powerful web d...
Introducing ASP.NET and the .NET Platform    3                       Figure 1.1. A user interacting with a web application...
4   Build Your Own ASP.NET 3.5 Web Site Using C# & VB    to a specific technology and language, ASP.NET lets you write web...
Introducing ASP.NET and the .NET Platform         5■	 ASP.NET makes it easy to reuse common User Interface elements in man...
6   Build Your Own ASP.NET 3.5 Web Site Using C# & VB    Microsoft SQL Server 2005 Express Edition       This is the free,...
Introducing ASP.NET and the .NET Platform    7                     Figure 1.2. Installing Visual Web Developer 2008 Expres...
8   Build Your Own ASP.NET 3.5 Web Site Using C# & VB                     Figure 1.3. The Start Page of Visual Web Develop...
Introducing ASP.NET and the .NET Platform      92. Download the file. After the download completes, execute the file and f...
10   Build Your Own ASP.NET 3.5 Web Site Using C# & VB                                 Figure 1.5. Managing your database ...
Introducing ASP.NET and the .NET Platform    11That’s it. Your machine is now ready to build ASP.NET web projects and SQLS...
12   Build Your Own ASP.NET 3.5 Web Site Using C# & VB       C:LearningASPVB. You can choose any location you like. Figure...
Introducing ASP.NET and the .NET Platform        13  of them in detail a bit later on, but right now, let’s dive in and cr...
14   Build Your Own ASP.NET 3.5 Web Site Using C# & VB           The Label control is one of the simplest controls in .NET...
Introducing ASP.NET and the .NET Platform        15After all the notifications are out of the way, you should get a page l...
16   Build Your Own ASP.NET 3.5 Web Site Using C# & VB     arate files—named code-behind files—for storing this code. The ...
Introducing ASP.NET and the .NET Platform    17                        Figure 1.14. Default.aspx.vb in Visual Web Develope...
18   Build Your Own ASP.NET 3.5 Web Site Using C# & VB     If you’re using VB.NET, you’ll need to generate the Page_Load m...
Introducing ASP.NET and the .NET Platform            19         Figure 1.16. Loading Default.aspx with a Label element wit...
20   Build Your Own ASP.NET 3.5 Web Site Using C# & VB      C#                                            LearningASPCSDef...
Introducing ASP.NET and the .NET Platform           21The DateTime class has a property called Now, which returns the curr...
22   Build Your Own ASP.NET 3.5 Web Site Using C# & VB                   value="…" />             </div>             <div>...
Introducing ASP.NET and the .NET Platform         23SummaryIn this chapter, you learned about .NET. You also learned of th...
Build Your Own ASP.NET 3.5 Web Site Using C# & VB (www.sitepoint.com)
2                                                   ChapterASP.NET BasicsSo far, you’ve learned what ASP.NET is, and what ...
Aspnet3 sample-090505115252-phpapp01
Aspnet3 sample-090505115252-phpapp01
Aspnet3 sample-090505115252-phpapp01
Aspnet3 sample-090505115252-phpapp01
Aspnet3 sample-090505115252-phpapp01
Aspnet3 sample-090505115252-phpapp01
Aspnet3 sample-090505115252-phpapp01
Aspnet3 sample-090505115252-phpapp01
Aspnet3 sample-090505115252-phpapp01
Aspnet3 sample-090505115252-phpapp01
Aspnet3 sample-090505115252-phpapp01
Aspnet3 sample-090505115252-phpapp01
Aspnet3 sample-090505115252-phpapp01
Aspnet3 sample-090505115252-phpapp01
Aspnet3 sample-090505115252-phpapp01
Aspnet3 sample-090505115252-phpapp01
Aspnet3 sample-090505115252-phpapp01
Aspnet3 sample-090505115252-phpapp01
Aspnet3 sample-090505115252-phpapp01
Aspnet3 sample-090505115252-phpapp01
Aspnet3 sample-090505115252-phpapp01
Aspnet3 sample-090505115252-phpapp01
Aspnet3 sample-090505115252-phpapp01
Aspnet3 sample-090505115252-phpapp01
Aspnet3 sample-090505115252-phpapp01
Aspnet3 sample-090505115252-phpapp01
Aspnet3 sample-090505115252-phpapp01
Aspnet3 sample-090505115252-phpapp01
Aspnet3 sample-090505115252-phpapp01
Aspnet3 sample-090505115252-phpapp01
Aspnet3 sample-090505115252-phpapp01
Aspnet3 sample-090505115252-phpapp01
Aspnet3 sample-090505115252-phpapp01
Aspnet3 sample-090505115252-phpapp01
Aspnet3 sample-090505115252-phpapp01
Aspnet3 sample-090505115252-phpapp01
Aspnet3 sample-090505115252-phpapp01
Aspnet3 sample-090505115252-phpapp01
Aspnet3 sample-090505115252-phpapp01
Aspnet3 sample-090505115252-phpapp01
Aspnet3 sample-090505115252-phpapp01
Aspnet3 sample-090505115252-phpapp01
Aspnet3 sample-090505115252-phpapp01
Aspnet3 sample-090505115252-phpapp01
Aspnet3 sample-090505115252-phpapp01
Aspnet3 sample-090505115252-phpapp01
Aspnet3 sample-090505115252-phpapp01
Aspnet3 sample-090505115252-phpapp01
Aspnet3 sample-090505115252-phpapp01
Aspnet3 sample-090505115252-phpapp01
Aspnet3 sample-090505115252-phpapp01
Aspnet3 sample-090505115252-phpapp01
Aspnet3 sample-090505115252-phpapp01
Aspnet3 sample-090505115252-phpapp01
Aspnet3 sample-090505115252-phpapp01
Aspnet3 sample-090505115252-phpapp01
Aspnet3 sample-090505115252-phpapp01
Aspnet3 sample-090505115252-phpapp01
Aspnet3 sample-090505115252-phpapp01
Aspnet3 sample-090505115252-phpapp01
Aspnet3 sample-090505115252-phpapp01
Aspnet3 sample-090505115252-phpapp01
Aspnet3 sample-090505115252-phpapp01
Aspnet3 sample-090505115252-phpapp01
Aspnet3 sample-090505115252-phpapp01
Aspnet3 sample-090505115252-phpapp01
Aspnet3 sample-090505115252-phpapp01
Aspnet3 sample-090505115252-phpapp01
Aspnet3 sample-090505115252-phpapp01
Aspnet3 sample-090505115252-phpapp01
Aspnet3 sample-090505115252-phpapp01
Aspnet3 sample-090505115252-phpapp01
Aspnet3 sample-090505115252-phpapp01
Aspnet3 sample-090505115252-phpapp01
Aspnet3 sample-090505115252-phpapp01
Aspnet3 sample-090505115252-phpapp01
Aspnet3 sample-090505115252-phpapp01
Aspnet3 sample-090505115252-phpapp01
Aspnet3 sample-090505115252-phpapp01
Aspnet3 sample-090505115252-phpapp01
Aspnet3 sample-090505115252-phpapp01
Aspnet3 sample-090505115252-phpapp01
Aspnet3 sample-090505115252-phpapp01
Aspnet3 sample-090505115252-phpapp01
Aspnet3 sample-090505115252-phpapp01
Aspnet3 sample-090505115252-phpapp01
Aspnet3 sample-090505115252-phpapp01
Aspnet3 sample-090505115252-phpapp01
Aspnet3 sample-090505115252-phpapp01
Aspnet3 sample-090505115252-phpapp01
Aspnet3 sample-090505115252-phpapp01
Aspnet3 sample-090505115252-phpapp01
Aspnet3 sample-090505115252-phpapp01
Aspnet3 sample-090505115252-phpapp01
Aspnet3 sample-090505115252-phpapp01
Aspnet3 sample-090505115252-phpapp01
Aspnet3 sample-090505115252-phpapp01
Aspnet3 sample-090505115252-phpapp01
Aspnet3 sample-090505115252-phpapp01
Aspnet3 sample-090505115252-phpapp01
Aspnet3 sample-090505115252-phpapp01
Aspnet3 sample-090505115252-phpapp01
Aspnet3 sample-090505115252-phpapp01
Aspnet3 sample-090505115252-phpapp01
Aspnet3 sample-090505115252-phpapp01
Aspnet3 sample-090505115252-phpapp01
Aspnet3 sample-090505115252-phpapp01
Aspnet3 sample-090505115252-phpapp01
Aspnet3 sample-090505115252-phpapp01
Aspnet3 sample-090505115252-phpapp01
Aspnet3 sample-090505115252-phpapp01
Aspnet3 sample-090505115252-phpapp01
Aspnet3 sample-090505115252-phpapp01
Aspnet3 sample-090505115252-phpapp01
Aspnet3 sample-090505115252-phpapp01
Aspnet3 sample-090505115252-phpapp01
Aspnet3 sample-090505115252-phpapp01
Aspnet3 sample-090505115252-phpapp01
Aspnet3 sample-090505115252-phpapp01
Aspnet3 sample-090505115252-phpapp01
Aspnet3 sample-090505115252-phpapp01
Aspnet3 sample-090505115252-phpapp01
Aspnet3 sample-090505115252-phpapp01
Aspnet3 sample-090505115252-phpapp01
Aspnet3 sample-090505115252-phpapp01
Aspnet3 sample-090505115252-phpapp01
Aspnet3 sample-090505115252-phpapp01
Aspnet3 sample-090505115252-phpapp01
Aspnet3 sample-090505115252-phpapp01
Aspnet3 sample-090505115252-phpapp01
Aspnet3 sample-090505115252-phpapp01
Aspnet3 sample-090505115252-phpapp01
Aspnet3 sample-090505115252-phpapp01
Aspnet3 sample-090505115252-phpapp01
Aspnet3 sample-090505115252-phpapp01
Aspnet3 sample-090505115252-phpapp01
Aspnet3 sample-090505115252-phpapp01
Aspnet3 sample-090505115252-phpapp01
Aspnet3 sample-090505115252-phpapp01
Aspnet3 sample-090505115252-phpapp01
Aspnet3 sample-090505115252-phpapp01
Aspnet3 sample-090505115252-phpapp01
Aspnet3 sample-090505115252-phpapp01
Aspnet3 sample-090505115252-phpapp01
Aspnet3 sample-090505115252-phpapp01
Aspnet3 sample-090505115252-phpapp01
Aspnet3 sample-090505115252-phpapp01
Aspnet3 sample-090505115252-phpapp01
Aspnet3 sample-090505115252-phpapp01
Aspnet3 sample-090505115252-phpapp01
Aspnet3 sample-090505115252-phpapp01
Aspnet3 sample-090505115252-phpapp01
Aspnet3 sample-090505115252-phpapp01
Aspnet3 sample-090505115252-phpapp01
Aspnet3 sample-090505115252-phpapp01
Aspnet3 sample-090505115252-phpapp01
Aspnet3 sample-090505115252-phpapp01
Aspnet3 sample-090505115252-phpapp01
Aspnet3 sample-090505115252-phpapp01
Aspnet3 sample-090505115252-phpapp01
Aspnet3 sample-090505115252-phpapp01
Aspnet3 sample-090505115252-phpapp01
Aspnet3 sample-090505115252-phpapp01
Aspnet3 sample-090505115252-phpapp01
Aspnet3 sample-090505115252-phpapp01
Upcoming SlideShare
Loading in...5
×

Aspnet3 sample-090505115252-phpapp01

8,724

Published on

Published in: Career
2 Comments
0 Likes
Statistics
Notes
  • Be the first to like this

No Downloads
Views
Total Views
8,724
On Slideshare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
86
Comments
2
Likes
0
Embeds 0
No embeds

No notes for slide

Transcript of "Aspnet3 sample-090505115252-phpapp01"

  1. 1. 0ANTONE #-9+ BUILD YOUR OWN REY SCALE ASP.NET 3.5 WEB SITE PANTONE Orange 021 C PANTONE 2955 C CMYK O, 53, 100, 0 CMYK 100, 45, 0, 37 Black 50% Black 100% USING C# & VB BY CRISTIAN DARIE & WYATT BARNETT 3RD EDITIONTHE ULTIMATE ASP.NET BEGINNER’S GUIDE
  2. 2. Thank you for downloading the sample chapters of Build Your Own ASP.NET 3.5Web Site Using C# & VB published by SitePoint.This excerpt includes the Summary of Contents, Information about the Author,Editors and SitePoint, Table of Contents, Preface, four sample chapters from the book,and the index.We hope you find this information useful in evaluating this book. For more information or to order, visit sitepoint.com
  3. 3. Summary of Contents of this ExcerptPreface..................................................................................................xxiI. Introducing ASP.NET and the .NET Platform.....................................12. ASP.NET Basics................................................................................253. VB and C# Programming Basics.......................................................454. Constructing ASP.NET Web Pages....................................................93Index...................................................................................................701Summary of Additional Book Contents5. Building Web Applications............................................................1536. Using the Validation Controls........................................................2237. Database Design and Development................................................2598. Speaking SQL..................................................................................3039. ADO.NET........................................................................................34310. Displaying Content Using Data Lists............................................41311. Managing Content Using Grid View and Details View................44112. Advanced Data Access..................................................................48313. Security and User Authentication................................................54514. Working with Files and Email......................................................59115. ASP.NET AJAX.............................................................................631A. Web Control Reference..................................................................651B. Deploying ASP.NET Web Sites......................................................691
  4. 4. BUILD YOUR OWN ASP.NET 3.5 WEBSITE USING C# & VB BY CRISTIAN DARIE & WYATT BARNETT 3RD EDITION
  5. 5. iv Build Your Own ASP.NET 3.5 Web Site Using C# & VB by Cristian Darie and Wyatt Barnett Copyright © 2008 SitePoint Pty. Ltd. Expert Reviewer: Wyatt Barnett Editor: Georgina Laidlaw Managing Editor: Chris Wyness Index Editor: Russell Brooks Technical Editor: Andrew Tetlaw Cover Design: Alex Walker Cover Image: Lucas Chan Printing History: First Edition: April 2004 Second Edition: October 2006 Third Edition: September 2008 Notice of Rights All rights reserved. No part of this book may be reproduced, stored in a retrieval system or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embodied in critical articles or reviews. Notice of Liability The author and publisher have made every effort to ensure the accuracy of the information herein. However, the information contained in this book is sold without warranty, either express or implied. Neither the authors and SitePoint Pty. Ltd., nor its dealers or distributors, will be held liable for any damages to be caused either directly or indirectly by the instructions contained in this book, or by the software or hardware products described herein. Trademark Notice Rather than indicating every occurrence of a trademarked name as such, this book uses the names only in an editorial fashion and to the benefit of the trademark owner with no intention of infringement of the trademark. Published by SitePoint Pty. Ltd. 48 Cambridge Street Collingwood VIC Australia 3066. Web: www.sitepoint.com Email: business@sitepoint.com ISBN 978-0-9804552-1-2 Printed and bound in the United States of America
  6. 6. vAbout the AuthorsCristian Darie is a software engineer with experience in a wide range of modern technologies,and the author of numerous technical books, including the popular Beginning E-Commerceseries. Having worked with computers since he was old enough to use a keyboard, he initiallytasted programming success with a prize in his first programming contest at the age of 12.From there, Cristian moved on to many other similar achievements, and is now studyingdistributed application architectures for his PhD.He always loves hearing feedback about his books, so dont hesitate to drop him a “hello”message when you have a spare moment. Cristian can be contacted through his personal website at http://www.cristiandarie.ro.Wyatt Barnett leads the in-house development team for a major industry trade associationin Washington DC. When not slinging obscene amounts of C# and SQL at a few exceedinglylarge monitors, he is most often spotted staring at HDTV and other forms of entertainmentin local watering holes. He also writes for SitePoints .NET blog, The Daily Catch.1About the Technical EditorAndrew Tetlaw has been tinkering with web sites as a web developer since 1997 and hasalso worked as a high school English teacher, an English teacher in Japan, a window cleaner,a car washer, a kitchen hand, and a furniture salesman. At SitePoint he is dedicated to makingthe world a better place through the technical editing of SitePoint books, kits, and articles.He is also a busy father of five, enjoys coffee, and often neglects his blog at http://tetlaw.id.au/.About the Technical DirectorAs Technical Director for SitePoint, Kevin Yank oversees all of its technical publica­tions—books, articles, newsletters, and blogs. He has written over 50 articles for SitePoint,but is best known for his book, Build Your Own Database Driven Website Using PHP &MySQL. Kevin lives in Melbourne, Australia, and enjoys performing improvised comedytheater and flying light aircraft.About SitePointSitePoint specializes in publishing fun, practical, and easy-to-understand content for webprofessionals. Visit http://www.sitepoint.com/ to access our books, newsletters, articles, andcommunity forums.1 http://www.sitepoint.com/blogs/category/net/
  7. 7. To my family and friends. —Cristian DarieTo my Father, whose guidance got me this far. —Wyatt Barnett
  8. 8. Table of ContentsPreface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxi Who Should Read This Book? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxi What’s in This Book? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxii The Book’s Web Site . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxv The Code Archive . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxvi Updates and Errata . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxvi The SitePoint Forums . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxvi The SitePoint Newsletters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxvii Your Feedback . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxvii Conventions Used in This Book . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxvii Code Samples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxvii Tips, Notes, and Warnings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxviii Acknowledgments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxixChapter 1 Introducing ASP.NET and the .NET Platform . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 What is ASP.NET? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2 Installing the Required Software . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 Installing Visual Web Developer 2008 Express Edition . . . . . . . . . . . . 6 Installing SQL Server Management Studio Express . . . . . . . . . . . . . . 8 Writing Your First ASP.NET Page . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 Getting Help . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23Chapter 2 ASP.NET Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . 25 ASP.NET Page Structure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
  9. 9. x Directives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29 Code Declaration Blocks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30 Code Render Blocks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32 ASP.NET Server Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34 Server-side Comments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34 Literal Text and HTML Tags . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36 View State . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37 Working with Directives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41 ASP.NET Languages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42 Visual Basic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42 C# . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43 Chapter 3 VB and C# Programming Basics . . . . 45 Programming Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46 Control Events and Subroutines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46 Page Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51 Variables and Variable Declaration . . . . . . . . . . . . . . . . . . . . . . . . . . . 54 Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58 Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61 Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64 Conditional Logic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66 Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68 Object Oriented Programming Concepts . . . . . . . . . . . . . . . . . . . . . . . . . . 74 Objects and Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74 Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77 Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78 Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79 Constructors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79 Scope . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80 Build Your Own ASP.NET 3.5 Web Site Using C# & VB (www.sitepoint.com)
  10. 10. xi Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81 Understanding Inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81 Objects in .NET . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82 Namespaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84 Using Code-behind Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92Chapter 4 Constructing ASP.NET Web Pages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93 Web Forms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94 HTML Server Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95 Using the HTML Server Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 97 Web Server Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102 Standard Web Server Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 104 List Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113 Advanced Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115 Web User Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 131 Creating a Web User Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 132 Master Pages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 140 Using Cascading Style Sheets (CSS) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 145 Types of Styles and Style Sheets . . . . . . . . . . . . . . . . . . . . . . . . . . . 146 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 152Chapter 5 Building Web Applications . . . . . . . . . . 153 Introducing the Dorknozzle Project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 154 Using Visual Web Developer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 156 Meeting the Features . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 157 Executing Your Project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 165 Core Web Application Features . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 167 Order the print version of this book to get all 700+ pages!
  11. 11. xii Web.config . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 168 Global.asax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 172 Using Application State . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 174 Working with User Sessions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 182 Using the Cache Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 184 Using Cookies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 186 Starting the Dorknozzle Project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 189 Preparing the Sitemap . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 189 Using Themes, Skins, and Styles . . . . . . . . . . . . . . . . . . . . . . . . . . . . 191 Building the Master Page . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 196 Using the Master Page . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 200 Extending Dorknozzle . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 204 Debugging and Error Handling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 206 Debugging with Visual Web Developer . . . . . . . . . . . . . . . . . . . . . . 207 Other Kinds of Errors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 212 Custom Errors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 214 Handling Exceptions Locally . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 215 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 220 Chapter 6 Using the Validation Controls . . . . . . 223 Client-side Validation and Server-side Validation . . . . . . . . . . . . . . . . . 224 Introducing the ASP.NET Validation Controls . . . . . . . . . . . . . . . . . . . . . 224 Enforcing Validation on the Server . . . . . . . . . . . . . . . . . . . . . . . . . 228 Using Validation Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 234 RequiredFieldValidator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 235 CompareValidator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 236 RangeValidator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 238 ValidationSummary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 240 RegularExpressionValidator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 241 CustomValidator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 245 Build Your Own ASP.NET 3.5 Web Site Using C# & VB (www.sitepoint.com)
  12. 12. xiii Validation Groups . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 248 Updating Dorknozzle . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 252 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 257Chapter 7 Database Design and Development . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 259 What Is a Database? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 260 Creating Your First Database . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 262 Creating a New Database Using Visual Web Developer . . . . . . . . . 263 Creating a New Database Using SQL Server Management Studio . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 264 Creating Database Tables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 265 Data Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 271 Column Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 272 Primary Keys . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 274 Creating the Employees Table . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 276 Creating the Remaining Tables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 279 Populating the Data Tables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 282 Relational Database Design Concepts . . . . . . . . . . . . . . . . . . . . . . . . . . . 285 Foreign Keys . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 287 Using Database Diagrams . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 290 Implementing Relationships in the Dorknozzle Database . . . . . . . 293 Diagrams and Table Relationships . . . . . . . . . . . . . . . . . . . . . . . . . . 297 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 301Chapter 8 Speaking SQL . . . . . . . . . . . . . . . . . . . . . . . . . . . . 303 Reading Data from a Single Table . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 304 Using the SELECT Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 306 Selecting Certain Fields . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 309 Order the print version of this book to get all 700+ pages!
  13. 13. xiv Selecting Unique Data with DISTINCT . . . . . . . . . . . . . . . . . . . . . 310 Row Filtering with WHERE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 312 Selecting Ranges of Values with BETWEEN . . . . . . . . . . . . . . . . . . . 313 Matching Patterns with LIKE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 314 Using the IN Operator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 315 Sorting Results Using ORDER BY . . . . . . . . . . . . . . . . . . . . . . . . . . . 316 Limiting the Number of Results with TOP . . . . . . . . . . . . . . . . . . . 317 Reading Data from Multiple Tables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 318 Subqueries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 319 Table Joins . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 319 Expressions and Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 321 Transact-SQL Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 324 Arithmetic Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 325 String Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 326 Date and Time Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 328 Working with Groups of Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 329 The COUNT Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 330 Grouping Records Using GROUP BY . . . . . . . . . . . . . . . . . . . . . . . . 330 Filtering Groups Using HAVING . . . . . . . . . . . . . . . . . . . . . . . . . . . . 332 The SUM, AVG, MIN, and MAX Functions . . . . . . . . . . . . . . . . . . . . . . 333 Updating Existing Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 334 The INSERT Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 334 The UPDATE Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 335 The DELETE Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 336 Stored Procedures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 337 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 342 Chapter 9 ADO.NET . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 343 Introducing ADO.NET . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 344 Importing the SqlClient Namespace . . . . . . . . . . . . . . . . . . . . . 346 Build Your Own ASP.NET 3.5 Web Site Using C# & VB (www.sitepoint.com)
  14. 14. xv Defining the Database Connection . . . . . . . . . . . . . . . . . . . . . . . . . 347 Preparing the Command . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 348 Executing the Command . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 349 Setting Up Database Authentication . . . . . . . . . . . . . . . . . . . . . . . . 351 Reading the Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 355 Using Parameters with Queries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 356 Bulletproofing Data Access Code . . . . . . . . . . . . . . . . . . . . . . . . . . . 364 Using the Repeater Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 366 Creating the Dorknozzle Employee Directory . . . . . . . . . . . . . . . . . . . . . 372 More Data Binding . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 377 Inserting Records . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 384 Updating Records . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 390 Deleting Records . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 406 Using Stored Procedures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 409 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 411Chapter 10 Displaying Content Using Data Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 413 DataList Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 414 Handling DataList Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 418 Editing DataList Items and Using Templates . . . . . . . . . . . . . . . . . . . . 426 DataList and Visual Web Developer . . . . . . . . . . . . . . . . . . . . . . . . . . . 435 Styling the DataList . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 436 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 439Chapter 11 Managing Content Using Grid View and Details View . . . . . . . . . . . . . . . . 441 Using the GridView Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 442 Customizing the GridView Columns . . . . . . . . . . . . . . . . . . . . . . . 449 Order the print version of this book to get all 700+ pages!
  15. 15. xvi Styling the GridView with Templates, Skins, and CSS . . . . . . . . . . 450 Selecting Grid Records . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 454 Using the DetailsView Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 459 Styling the DetailsView . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 462 GridView and DetailsView Events . . . . . . . . . . . . . . . . . . . . . . . . . . 465 Entering Edit Mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 469 Using Templates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 472 Updating DetailsView Records . . . . . . . . . . . . . . . . . . . . . . . . . . 476 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 480 Chapter 12 Advanced Data Access . . . . . . . . . . . . . . . . 483 Using Data Source Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 484 Binding the GridView to a SqlDataSource . . . . . . . . . . . . . . . . . . 486 Binding the DetailsView to a SqlDataSource . . . . . . . . . . . . . . 495 Displaying Lists in DetailsView . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 507 More on SqlDataSource . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 510 Working with Data Sets and Data Tables . . . . . . . . . . . . . . . . . . . . . . . . . 511 What Is a Data Set Made From? . . . . . . . . . . . . . . . . . . . . . . . . . . . . 514 Binding DataSets to Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 515 Implementing Paging . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 521 Storing Data Sets in View State . . . . . . . . . . . . . . . . . . . . . . . . . . . . 523 Implementing Sorting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 526 Filtering Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 537 Updating a Database from a Modified DataSet . . . . . . . . . . . . . . . . . . . 538 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 542 Chapter 13 Security and User Authentication . . . . . . . . . . . . . . . . . . . . . . . . . . . 545 Basic Security Guidelines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 546 Build Your Own ASP.NET 3.5 Web Site Using C# & VB (www.sitepoint.com)
  16. 16. xvii Securing ASP.NET Applications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 548 Working with Forms Authentication . . . . . . . . . . . . . . . . . . . . . . . . 550 ASP.NET Memberships and Roles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 563 Creating the Membership Data Structures . . . . . . . . . . . . . . . . . . . 564 Using Your Database to Store Membership Data . . . . . . . . . . . . . . 566 Using the ASP.NET Web Site Configuration Tool . . . . . . . . . . . . . . 572 Creating Users and Roles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 575 Changing Password Strength Requirements . . . . . . . . . . . . . . . . . . 576 Securing Your Web Application . . . . . . . . . . . . . . . . . . . . . . . . . . . . 579 Using the ASP.NET Login Controls . . . . . . . . . . . . . . . . . . . . . . . . . . 581 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 589Chapter 14 Working with Files and Email . . . . . 591 Writing and Reading Text Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 592 Setting Up Permissions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 593 Writing Content to a Text File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 596 Reading Content from a Text File . . . . . . . . . . . . . . . . . . . . . . . . . . 600 Accessing Directories and Directory Information . . . . . . . . . . . . . . . . . . 604 Working with Directory and File Paths . . . . . . . . . . . . . . . . . . . . . . 608 Uploading Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 611 Sending Email with ASP.NET . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 615 Sending a Test Email . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 617 Creating the Company Newsletters Page . . . . . . . . . . . . . . . . . . . . 619 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 629Chapter 15 ASP.NET AJAX . . . . . . . . . . . . . . . . . . . . . . . . . . 631 What is Ajax? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 632 ASP.NET AJAX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 633 Using the UpdatePanel Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 634 Managing the ScriptManager Control . . . . . . . . . . . . . . . . . . . . 638 Order the print version of this book to get all 700+ pages!
  17. 17. xviii Using Triggers to Update an UpdatePanel . . . . . . . . . . . . . . . . . . . . . . . . 639 The ASP.NET AJAX Control Toolkit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 643 The ValidatorCalloutExtender Control Extender . . . . . . . . 645 Getting Started with Animation . . . . . . . . . . . . . . . . . . . . . . . . . . . 648 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 650 Appendix A Web Control Reference . . . . . . . . . . . . . . . 651 The WebControl Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 651 Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 651 Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 652 Standard Web Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 653 AdRotator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 653 BulletedList . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 653 Button . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 654 Calendar . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 655 CheckBox . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 657 CheckBoxList . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 657 DropDownList . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 659 FileUpload . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 659 HiddenField . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 660 HyperLink . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 660 Image . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 660 ImageButton . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 661 ImageMap . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 662 Label . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 662 LinkButton . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 662 ListBox . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 663 Literal . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 664 MultiView . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 664 Panel . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 665 Build Your Own ASP.NET 3.5 Web Site Using C# & VB (www.sitepoint.com)
  18. 18. xix PlaceHolder . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 665 RadioButton . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 665 RadioButtonList . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 666 TextBox . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 667 Wizard . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 668 Xml . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 672 Validation Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 672 CompareValidator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 673 CustomValidator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 674 RangeValidator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 675 RegularExpressionValidator . . . . . . . . . . . . . . . . . . . . . . . . 676 RequiredFieldValidator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 676 ValidationSummary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 677 Navigation Web Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 678 SiteMapPath . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 678 Menu . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 679 TreeView . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 684 Ajax Web Extensions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 688 ScriptManager . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 688 Timer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 689 UpdatePanel . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 689 UpdateProgress . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 690Appendix B Deploying ASP.NET Web Sites . . . . . 691 ASP.NET Hosting Considerations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 691 Using Visual Web Developer Express to Deploy ASP.NET Applications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 692 ASP.NET Deployment “Gotchas” . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 695 Using the SQL Server Hosting Toolkit . . . . . . . . . . . . . . . . . . . . . . . . . . . . 696 Dealing with SQL Security . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 698 Order the print version of this book to get all 700+ pages!
  19. 19. xx Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 701 Build Your Own ASP.NET 3.5 Web Site Using C# & VB (www.sitepoint.com)
  20. 20. PrefaceWeb development is very exciting. There’s nothing like the feeling you have afteryou place your first dynamic web site online, and see your little toy in action whileother people are actually using it!Web development with ASP.NET is particularly exciting. If you’ve never created adynamic web site before, I’m sure you’ll fall in love with this area of web develop­ment. If you’ve worked with other server-side technologies, I expect you’ll be a littleshocked by the differences.ASP.NET really is a unique technology, and it provides new and extremely efficientways to create web applications using the programming language with which youfeel most comfortable. Though it can take some time to learn, ASP.NET is simpleto use. Whether you want to create simple web forms, feature-rich shopping carts,or even complex enterprise applications, ASP.NET can help you do it. All the toolsyou’ll need to get up and running are immediately available and easy to install, andrequire very little initial configuration.This book will be your gentle introduction to the wonderful world of ASP.NET,teaching you the foundations step by step. First, you’ll learn the theory; then, you’llput it into practice as we work through practical exercises together. To demonstratesome of the more complex functionality, and to put the theory into a cohesive,realistic context, we’ll develop a project through the course of this book. The pro­ject—an intranet site for a company named Dorknozzle—will allow us to see themany components of .NET in action, and to understand through practice exactlyhow .NET works in the real world.We hope you’ll find reading this book an enjoyable experience that will significantlyhelp you with your future web development projects!Who Should Read This Book?This book is aimed at beginner, intermediate, and advanced web designers lookingto make the leap into server-side programming with ASP.NET. We expect that you’llalready feel comfortable with HTML and a little CSS, as very little explanation ofthese topics is provided here.
  21. 21. xxii By the end of this book, you should be able to successfully download and install Visual Web Developer 2008 Express Edition, and use it to create basic ASP.NET pages. You’ll also learn how to install and run Microsoft SQL Server 2005 Express Edition, create database tables, and work with advanced, dynamic ASP.NET pages that query, insert, update, and delete information within a database. All examples provided in the book are written in both Visual Basic and C#, the two most popular languages for creating ASP.NET web sites. The examples start at be­ ginners’ level and proceed to more advanced levels. As such, no prior knowledge of either language is required in order to read, understand, learn from, and apply the knowledge provided in this book. Experience with other programming or scripting languages (such as JavaScript) will certainly grease the wheels, though, and should enable you to grasp fundamental programming concepts more quickly. What’s in This Book? This book comprises the following chapters. Read them from beginning to end to gain a complete understanding of the subject, or skip around if you feel you need a refresher on a particular topic. Chapter 1: Introducing ASP.NET Before you can start building your database-driven web presence, you must ensure that you have the right tools for the job. In this first chapter, you’ll install Visual Web Developer 2008 Express Edition and Microsoft SQL Server 2005 Express Edition. Finally, we’ll create a simple ASP.NET page to make sure that everything’s running and properly configured. Chapter 2: ASP.NET Basics In this chapter, you’ll create your first useful ASP.NET page. We’ll explore all of the components that make up a typical ASP.NET page, including directives, controls, and code. Then, we’ll walk through the process of deployment, focusing specifically on allowing the user to view the processing of a simple ASP.NET page through a web browser. Chapter 3: VB and C# Programming Basics In this chapter, we’ll look at two of the programming languages that are used to create ASP.NET pages: VB and C#. You’ll learn about the syntax of the two languages as we explore the concepts of variables, data types, conditionals, Build Your Own ASP.NET 3.5 Web Site Using C# & VB (www.sitepoint.com)
  22. 22. xxiii loops, arrays, functions, and more. Finally, we’ll see how the two languages accommodate object oriented programming principles by allowing you to work with classes, methods, properties, inheritance, and so on.Chapter 4: Constructing ASP.NET Web Pages ASP.NET web pages are known as web forms, but, as we’ll see, the process of building ASP.NET web forms is a lot like composing a castle with Lego bricks! ASP.NET is bundled with hundreds of controls—including HTML controls, web controls, and so on—that are designed for easy deployment within your applications. This chapter will introduce you to these building blocks and show how to lock them together. You’ll also learn about master pages, which are a very exciting feature of ASP.NET.Chapter 5: Building Web Applications A web application is basically a group of web forms, controls, and other elements that work together to achieve complex functionality. So it’s no surprise that when we build web applications, we must consider more aspects than when we build individual web forms. This chapter touches on those aspects. We configure your web application; learn how to use the application state, user sessions, and cookies; explore the process for debugging errors in your project; and more.Chapter 6: Using the Validation Controls This chapter introduces validation controls. With validation controls, Microsoft basically eliminated the headache of fumbling through, and configuring, tired, reused client-side validation scripts. First, we’ll learn how to implement user input validation on both the client and server sides of your application using Microsoft’s ready-made validation controls. Then, we’ll learn how to perform more advanced validation using regular expressions and custom validators.Chapter 7: Database Design and Development Undoubtedly one of the most important chapters in the book, Chapter 7 will prepare you to work with databases in ASP.NET. We’ll cover the essentials you’ll need to know in order to create a database using SQL Server Express Edition. Also in this chapter, we’ll begin to build the database for the Dorknozzle intranet project. Order the print version of this book to get all 700+ pages!
  23. 23. xxiv Chapter 8: Speaking SQL This chapter will teach you to speak the language of the database: Structured Query Language, or SQL. After a gentle introduction to the basic concepts of SQL, which will teach you how to write SELECT, INSERT, UPDATE, and DELETE queries, we’ll move on to more advanced topics such as expressions, conditions, and joins. Finally, we’ll take a look at how we can reuse queries quickly and easily by writing stored procedures. Chapter 9: ADO.NET The next logical step in building database-driven web applications is to roll up our sleeves and dirty our hands with a little ADO.NET—the technology that facilitates communication between your web application and the database server. This chapter explores the essentials of the technology, and will have you reading database data directly from your web applications in just a few short steps. We’ll then help you begin the transition from working with static applications to those that are database driven. Chapter 10: Displaying Content Using Data Lists Taking ADO.NET further, this chapter shows you how to utilize the DataList control provided within the .NET Framework. DataLists play a crucial role in simplifying the presentation of information with ASP.NET. In learning how to present database data within your applications in a cleaner and more legible format, you’ll gain an understanding of the concepts of data binding at a high level. Chapter 11: Managing Content Using GridView and DetailsView This chapter explores two of the most powerful data presentation controls of ASP.NET: GridView and DetailsView. GridView is a very powerful control that automates almost all tasks that involve displaying grids of data. DetailsView completes the picture by offering us the functionality we need to display the details of a single grid item. Chapter 12: Advanced Data Access This chapter explores a few of the more advanced details involved in data access, retrieval, and manipulation. We’ll start by looking at direct data access using ADO.NET’s data source controls. We’ll then compare this approach with that of using data sets to access data in a disconnected fashion. In this section, you’ll Build Your Own ASP.NET 3.5 Web Site Using C# & VB (www.sitepoint.com)
  24. 24. xxv also learn to implement features such as paging, filtering, and sorting, using custom code.Chapter 13: Security and User Authentication This chapter will show you how to secure your web applications with ASP.NET. We’ll discuss the various security models available, including IIS, Forms, Windows, and Windows Live ID, and explore the roles that the Web.config and XML files can play. This chapter will also introduce you to the ASP.NET membership model and login controls.Chapter 14: Working with Files and Email In this chapter, we’ll look at the task of accessing your server’s file system, in­ cluding drives, files, and the network. Next, the chapter will show you how to work with file streams to create text files, write to text files, and read from text files stored on your web server. Finally, you’ll get first-hand experience in sending emails using ASP.NET.Chapter 15: ASP.NET AJAX In our final chapter, you’ll learn all about the Ajax features that are built into ASP.NET 3.5. We’ll spice up the Dorknozzle project with a few Ajax features that’ll show how simple ASP.NET AJAX is to use. We’ll also explore the ASP.NET AJAX Control Toolkit and see how it can enhance existing features.Appendix A: Web Control Reference Included in this book is a handy web control reference, which lists the most common properties and methods of the most frequently used controls in ASP.NET.Appendix B: Deploying ASP.NET Web Sites This appendix will show you, step by step, how to use Visual Web Developer and to move your web site from your development environment to a web hosting service and make it live on the Internet. It also covers tips for choosing a reliable web host, ASP.NET deployment gotchas, and hints for using the SQL Server Hosting Toolkit to migrate your database.The Book’s Web SiteLocated at http://www.sitepoint.com/books/aspnet3/, the web site that supportsthis book will give you access to the following facilities. Order the print version of this book to get all 700+ pages!
  25. 25. xxvi The Code Archive As you progress through this book, you’ll note a number of references to the code archive. This is a downloadable ZIP archive that contains each and every line of example source code that’s printed in this book. If you want to cheat (or save yourself from carpal tunnel syndrome), go ahead and download the archive.1 The archive contains one folder for each chapter of this book. Each folder may contain a LearningASP folder for the stand-alone examples in that chapter and a Dorknozzle folder for files associated with the Dorknozzle intranet application, the project that we’ll work on throughout the book. Each folder will contain CS and VB subfolders, which contain the C# and VB versions of all the code examples for that chapter. Incremental versions of each file are represented by a number in the file’s name. Finally, a complete version of the Dorknozzle project can be found in the Dorknozzle folder. Updates and Errata No book is perfect, and we expect that watchful readers will be able to spot at least one or two mistakes before the end of this one. The Errata page on the book’s web site will always have the latest information about known typographical and code errors, and necessary updates for new releases of ASP.NET and the various web standards that apply. The SitePoint Forums If you’d like to communicate with us or anyone else on the SitePoint publishing team about this book, you should join SitePoint’s online community.2 The .NET forum, in particular, can offer an abundance of information above and beyond the solutions in this book.3 In fact, you should join that community even if you don’t want to talk to us, because a lot of fun and experienced web designers and developers hang out there. It’s a good way to learn new stuff, get questions answered in a hurry, and just have fun. 1 http://www.sitepoint.com/books/aspnet3/code.php 2 http://www.sitepoint.com/forums/ 3 http://www.sitepoint.com/forums/forumdisplay.php?f=141 Build Your Own ASP.NET 3.5 Web Site Using C# & VB (www.sitepoint.com)
  26. 26. xxviiThe SitePoint NewslettersIn addition to books like this one, SitePoint publishes free email newsletters includ­ing The SitePoint Tribune and The SitePoint Tech Times. In them, you’ll read aboutthe latest news, product releases, trends, tips, and techniques for all aspects of webdevelopment. If nothing else, you’ll get useful ASP.NET articles and tips, but ifyou’re interested in learning other technologies, you’ll find them especially valuable.Sign up to one or more SitePoint newsletters athttp://www.sitepoint.com/newsletter/.Your FeedbackIf you can’t find an answer through the forums, or if you wish to contact us for anyother reason, the best place to write is books@sitepoint.com. We have a well-staffedemail support system set up to track your inquiries, and if our support team membersare unable to answer your question, they’ll send it straight to us. Suggestions forimprovements, as well as notices of any mistakes you may find, are especiallywelcome.Conventions Used in This BookYou’ll notice that we’ve used certain typographic and layout styles throughout thisbook to signify different types of information. Look out for the following items.Code SamplesCode in this book will be displayed using a fixed-width font, like so: <h1>A perfect summers day</h1> <p>It was a lovely day for a walk in the park. The birds were singing and the kids were all back at school.</p>If the code may be found in the book’s code archive, the name of the file will appearat the top of the program listing, like this: Order the print version of this book to get all 700+ pages!
  27. 27. xxviii example.css .footer { background-color: #CCC; border-top: 1px solid #333; } If only part of the file is displayed, this is indicated by the word excerpt: example.css (excerpt) border-top: 1px solid #333; If additional code is to be inserted into an existing example, the new code will be displayed in bold: function animate() { new_variable = "Hello"; } Also, where existing code is required for context, rather than repeat all the code, a vertical ellipsis will be displayed: function animate() { ⋮ return new_variable; } Some lines of code are intended to be entered on one line, but we’ve had to wrap them because of page constraints. A ➥ indicates a line break that exists for formatting purposes only, and should be ignored. URL.open("http://www.sitepoint.com/blogs/2007/05/28/user-style-she ➥ets-come-of-age/"); Tips, Notes, and Warnings Hey, You! Tips will give you helpful little pointers. Build Your Own ASP.NET 3.5 Web Site Using C# & VB (www.sitepoint.com)
  28. 28. xxix Ahem, Excuse Me … Notes are useful asides that are related—but not critical—to the topic at hand. Think of them as extra tidbits of information. Make Sure You Always … … pay attention to these important points. Watch Out! Warnings will highlight any gotchas that are likely to trip you up along the way.AcknowledgmentsId like to thank the SitePoint team, and especially Andrew Tetlaw, for being ex­tremely supportive during the process of writing this book.—Cristian Darie Order the print version of this book to get all 700+ pages!
  29. 29. Build Your Own ASP.NET 3.5 Web Site Using C# & VB (www.sitepoint.com)
  30. 30. 1 ChapterIntroducing ASP.NET andthe .NET PlatformASP.NET is one of the most exciting web development technologies on offer today.When Microsoft released the first version in 2002, many web developers thoughtall their dreams had come true. Here was a powerful platform with lots of built-infunctionality, astonishing performance levels, and one of the best IDEs (IntegratedDevelopment Environments) around: Visual Studio. What more could anyone want?Indeed, ASP.NET showed the way for the faster, easier, and more disciplined devel­opment of dynamic web sites, and the results were impressive.Time has passed, and ASP.NET has grown. ASP.NET 3.5 comes with extraordinarynew features as well as an expanded and more powerful underlying framework.Not only that, but the basic versions of all development tools, including Visual WebDeveloper 2008 Express Edition and SQL Server 2005 Express Edition, are still free!This book shows you how to use all these technologies together in order to producefantastic results. We’ll take you step by step through each task, showing you howto get the most out of each technology and tool. Let’s begin!
  31. 31. 2 Build Your Own ASP.NET 3.5 Web Site Using C# & VB What is ASP.NET? ASP.NET is a sophisticated and powerful web development framework. If you’ve never used ASP.NET before, it will likely take you some time and patience to grow accustomed to it. Development with ASP.NET requires not only an understanding of HTML and web design, but also a firm grasp of the concepts of object oriented programming and development. However, we believe you’ll find the benefits amply reward the learning effort! In the next few sections, we’ll introduce you to the basics of ASP.NET. We’ll walk through the process of installing it on your web server, and look at a simple example that demonstrates how ASP.NET pages are constructed. But first, let’s define what ASP.NET actually is. ASP.NET is a server-side technology for developing web applications based on the Microsoft .NET Framework. Okay, let’s break that jargon-filled sentence down. ASP.NET is a server-side technology. That is, it runs on the web server. Most web designers cut their teeth learning client-side technologies such as HTML, JavaScript, and Cascading Style Sheets (CSS). When a web browser requests a web page created with only client-side technologies, the web server simply grabs the files that the browser (or client) requests and sends them down the line. The client is entirely responsible for reading the markup in those files and interpreting that markup to display the page on the screen. Server-side technologies such as ASP.NET, however, are a different story. Instead of being interpreted by the client, server-side code (for example, the code in an ASP.NET page) is interpreted by the web server. In the case of ASP.NET, the code in the page is read by the server and used to generate the HTML, JavaScript, and CSS, which is then sent to the browser. Since the processing of the ASP.NET code occurs on the server, it’s called a server-side technology. As Figure 1.1 shows, the client only sees the HTML, JavaScript, and CSS. The server is entirely responsible for processing the server-side code. Build Your Own ASP.NET 3.5 Web Site Using C# & VB (www.sitepoint.com)
  32. 32. Introducing ASP.NET and the .NET Platform 3 Figure 1.1. A user interacting with a web applicationNote the three roles involved in such a transaction:user The transaction starts and ends with the user. The user operates the web client software and interprets the results.web client This is the software program that the person uses to interact with the web application. The client is usually a web browser, such as Internet Explorer or Firefox.web server This is the software program located on the server. It processes re­ quests made by the web client.ASP.NET is a technology for developing web applications. A web application is justa fancy name for a dynamic web site. Web applications usually (but not always)store information in a database, and allow visitors to the site to access and changethat information. Many different programming technologies and supported languageshave been developed to create web applications; PHP, JSP, Ruby on Rails, CGI, andColdFusion are just a few of the more popular ones. However, rather than tying you Order the print version of this book to get all 700+ pages!
  33. 33. 4 Build Your Own ASP.NET 3.5 Web Site Using C# & VB to a specific technology and language, ASP.NET lets you write web applications in a variety of familiar programming languages. ASP.NET uses the Microsoft .NET Framework. The .NET Framework collects all the technologies needed for building Windows desktop applications, web applica­ tions, web services, and so on, into a single package, and makes them available to more than 40 programming languages. Even with all the jargon explained, you’re probably still wondering what makes ASP.NET so good. The truth is that there are many server-side technologies around, each of which has its own strengths and weaknesses. Yet ASP.NET has a few unique features: ■ ASP.NET lets you write the server-side code using your favorite programming language—or at least one the one you prefer from the long list of supported lan­ guages. The .NET Framework currently supports over 40 languages, and many of these may be used to build ASP.NET web sites. The most popular choices are C# (pronounced “C sharp”) and Visual Basic (or VB), which are the ones we’ll cover in this book. ■ ASP.NET pages are compiled, not interpreted. In ASP.NET’s predecessor, ASP, pages were interpreted: every time a user requested a page, the server would read the page’s code into memory, figure out how to execute the code, and execute it. In ASP.NET, the server need only figure out how to execute the code once. The code is compiled into efficient binary files, which can be run very quickly, again and again, without the overhead involved in rereading the page each time. This allows a big jump in performance, compared to the old days of ASP. ■ ASP.NET has full access to the functionality of the .NET Framework. Support for XML, web services, database interaction, email, regular expressions, and many other technologies are built right into .NET, which saves you from having to reinvent the wheel. ■ ASP.NET allows you to separate the server-side code in your pages from the HTML layout. When you’re working with a team composed of programmers and design specialists, this separation is a great help, as it lets programmers modify the server-side code without stepping on the designers’ carefully crafted HTML—and vice versa. Build Your Own ASP.NET 3.5 Web Site Using C# & VB (www.sitepoint.com)
  34. 34. Introducing ASP.NET and the .NET Platform 5■ ASP.NET makes it easy to reuse common User Interface elements in many web forms, as it allows us to save those components as independent web user controls. During the course of this book, you’ll learn how to add powerful features to your web site, and to reuse them in many places with a minimum of effort.■ You can get excellent tools that assist in developing ASP.NET web applications. Visual Web Developer 2008 is a free, powerful visual editor that includes features such as code autocompletion, code formatting, database integration functionality, a visual HTML editor, debugging, and more. In the course of this book, you’ll learn how to use this tool to build the examples we discuss.■ The .NET Framework was first available only to the Microsoft Windows platform, but thanks to projects such as Mono,1 it’s since been ported to other operating systems.Still with us? Great! It’s time to gather our tools and start building!Installing the Required SoftwareIf you’re going to learn ASP.NET, you first need to make sure you have all the ne­cessary software components installed and working on your system. Let’s take careof this before we move on.Visual Web Developer 2008 Express Edition Visual Web Developer 2008 is a free, powerful web development environment for ASP.NET 3.5. It includes features such as a powerful code, HTML and CSS editor, project debugging, IntelliSense (Microsoft’s code autocompletion tech­ nology), database integration with the ability to design databases and data structures visually, and much more. You’re in for a lot of Visual Web Developer fun during the course of this book..NET Framework 3.5 and the .NET Framework Software Development Kit (SDK) As we’ve already discussed, the .NET Framework drives ASP.NET. You’re likely to have the .NET Framework already, as installs automatically through the Windows Update service. Otherwise, it’ll be installed together with Visual Web Developer.1 http://www.mono-project.com/ Order the print version of this book to get all 700+ pages!
  35. 35. 6 Build Your Own ASP.NET 3.5 Web Site Using C# & VB Microsoft SQL Server 2005 Express Edition This is the free, but still fully functional, version of SQL Server 2005. This software is a Relational Database Management System whose purpose is to store, manage, and retrieve data as quickly and reliably as possible. You’ll learn how to use SQL Server to store and manipulate the data for the DorkNozzle applica­ tion you’ll build in this book. SQL Server Management Studio Express Because the Express Edition of SQL Server doesn’t ship with any visual man­ agement tools, you can use this free tool, also developed by Microsoft, to access your SQL Server 2005 database. Installing Visual Web Developer 2008 Express Edition Install Visual Web Developer 2008 Express Edition by following these simple steps: 1. Browse to http://www.microsoft.com/express/vwd/. 2. Click the Download Now! link. 3. Under the Web Install section, click the Download link for Visual Web Developer 2008 Express Edition. 4. Execute the downloaded file, vnssetup.exe. 5. After you accept the terms and conditions, you’re offered two additional optional products: MSDN Express Library, which contains the product and framework documentation, and SQL Server 2005 Express Edition. Make sure to tick both options. This is very important, as we’ll be needing the SQL Server Express Edi­ tion database server to complete projects later in the book. Click Next. Build Your Own ASP.NET 3.5 Web Site Using C# & VB (www.sitepoint.com)
  36. 36. Introducing ASP.NET and the .NET Platform 7 Figure 1.2. Installing Visual Web Developer 2008 Express Edition6. In the next setup screen you’ll be informed of the products and components you’re about to install. Click Next, and wait for the installer to download and install the software. You can see what the setup window looks like in Figure 1.2.7. Start Visual Web Developer to ensure it has installed correctly for you. Its welcome screen should look like Figure 1.3. Order the print version of this book to get all 700+ pages!
  37. 37. 8 Build Your Own ASP.NET 3.5 Web Site Using C# & VB Figure 1.3. The Start Page of Visual Web Developer 2008 Express Edition Installing SQL Server Management Studio Express You’ve just installed Visual Web Developer and SQL Server 2005 Express Editions. You won’t use SQL Server until later in the book when we discuss relational data­ bases, but we’ll install all the required software here so that when the time comes you’ll have the complete environment properly set up. In order to use your SQL Server 2005 instance effectively, you’ll need an adminis­ tration tool to work with your databases. SQL Server Management Studio Express is a free tool provided by Microsoft that allows you to manage your instance of SQL Server 2005. To install it, follow these steps: 1. Navigate to http://www.microsoft.com/express/sql/download/ and click the Download link under the SQL Server Management Studio Express section. Build Your Own ASP.NET 3.5 Web Site Using C# & VB (www.sitepoint.com)
  38. 38. Introducing ASP.NET and the .NET Platform 92. Download the file. After the download completes, execute the file and follow the steps to install the product.Once it’s installed, SQL Server Manager Express can be accessed from Start > AllPrograms > Microsoft SQL Server 2005 > SQL Server Management Studio Express. Whenexecuted, it will first ask for your credentials, as Figure 1.4 illustrates. Figure 1.4. Connecting to SQL ServerBy default, when installed, SQL Server 2005 Express Edition will only accept con­nections that use Windows Authentication, which means that you’ll use yourWindows user account to log into the SQL Server. Since you’re the user that installedSQL Server 2005, you’ll already have full privileges to the SQL Server. Click Connectto connect to your SQL Server 2005 instance.After you’re authenticated, you’ll be shown the interface in Figure 1.5, which offersyou many ways to interact with, and manage, your SQL Server 2005 instance.SQL Server Management Studio lets you browse through the objects that reside onyour SQL Server, and even modify their settings. For example, you can change thesecurity settings of your server by right-clicking COMPUTERSQLEXPRESS (whereCOMPUTER is the name of your computer), choosing Properties, and selecting Securityfrom the panel, as shown in Figure 1.6. Here we’ve modified the Server authenticationmode to SQL Server and Windows Authentication mode. We’ll need this settinga bit later in the book, but you can set it now if you want, and then click OK. Order the print version of this book to get all 700+ pages!
  39. 39. 10 Build Your Own ASP.NET 3.5 Web Site Using C# & VB Figure 1.5. Managing your database server Figure 1.6. Changing server settings with SQL Server Management Studio Build Your Own ASP.NET 3.5 Web Site Using C# & VB (www.sitepoint.com)
  40. 40. Introducing ASP.NET and the .NET Platform 11That’s it. Your machine is now ready to build ASP.NET web projects and SQLServer databases. Now the fun starts—it’s time to create your very first ASP.NETpage!Writing Your First ASP.NET PageFor your first ASP.NET exercise, we’ll create the simple example shown in Figure 1.7.We’ll go though the process of creating this page step by step. Figure 1.7. An exciting preview of your first ASP.NET page!To create this page in Visual Web Developer, you’ll need to follow a few simplesteps:1. Start Visual Web Developer, and choose File > New Web Site (or hit the default keyboard shortcut, Shift+Alt+N).2. Choose ASP.NET Web Site for the template and File System for the location type. This location type tells Visual Web Developer to create the project in a physical folder on your disk, and execute that project using the integrated web server.3. Choose the language in which you prefer to code your pages. Although ASP.NET allows you to code different pages inside a project in different languages, for the sake of simplicity we’ll generally assume you work with a single language.4. If you chose C# for the language, type C:LearningASPCS for the folder location where you want to store the files for this exercise. If you prefer VB.NET, choose Order the print version of this book to get all 700+ pages!
  41. 41. 12 Build Your Own ASP.NET 3.5 Web Site Using C# & VB C:LearningASPVB. You can choose any location you like. Figure 1.8 shows the C# version of the selection. Figure 1.8. Starting a new ASP.NET Web Site project with Visual Web Developer Figure 1.9. Your new project in Visual Web Developer 5. After clicking OK, Visual Web Developer will create the project with a basic structure. Your project contains an empty App_Data folder, a basic Default.aspx file, and a basic configuration file, Web.config—see Figure 1.9. We’ll discuss each Build Your Own ASP.NET 3.5 Web Site Using C# & VB (www.sitepoint.com)
  42. 42. Introducing ASP.NET and the .NET Platform 13 of them in detail a bit later on, but right now, let’s dive in and create our first ASP.NET web page.The main panel in the Visual Web Developer interface is the page editor, in whichyou’ll see the HTML source of the Default.aspx web page. Edit the title of the pageto something more specific than Untitled Page, such as Welcome to Build YourOwn ASP.NET 3.5 Web Site!: <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title>Welcome to Build Your Own ASP.NET 3.5 Web Site! </title> </head>Loading the Default.aspx page in a web browser now would open an empty page;this makes sense, since we didn’t add any content to this page! Let’s modify thepage by adding the highlighted: <body> <form id="form1" runat="server"> <div> <p>Hello there!</p> <p> The time is now: <asp:Label ID="myTimeLabel" runat="server" /> </p> </div> </form> </body> </html>Although our little page isn’t yet finished (our work with the Label control isn’tover), let’s execute the page to ensure we’re on the right track. Hit F5 or go to Debugmenu. How a Web Server Control Works You’ve just added a Web Server Control to the page by adding an <asp:Label/> element to the page. You’ll learn all about Web Server Controls in Chapter 2, but for now you need to learn how this simple control works so that you can under­ stand the exercise. Order the print version of this book to get all 700+ pages!
  43. 43. 14 Build Your Own ASP.NET 3.5 Web Site Using C# & VB The Label control is one of the simplest controls in .NET, which lets you insert dynamic content into the page. The asp: part of the tag name identifies it as a built-in ASP.NET tag. ASP.NET comes with numerous built-in tags, and <asp:Label/> is probably one of the most frequently used. The runat="server" attribute value identifies the tag as something that needs to be handled on the server. In other words, the web browser will never see the <asp:Label/> tag; when the page is requested by the client, ASP.NET sees it and converts it to regular HTML tags before the page is sent to the browser. It’s up to us to write the code that will tell ASP.NET to replace this particular tag with something meaningful to the user loading the page. The first time you do this, Visual Web Developer will let you know that your project isn’t configured for debugging, and it’ll offer to make the necessary change to the configuration (Web.config) file for you—see Figure 1.10. Confirm the change by clicking OK. Figure 1.10. Enabling project debugging in Visual Web Developer If Script Debugging is not enabled in Internet Explorer, you’ll get the dialog shown in Figure 1.11. Check the Don’t show this dialog again checkbox, and click Yes. Figure 1.11. Enabling script debugging in Internet Explorer Build Your Own ASP.NET 3.5 Web Site Using C# & VB (www.sitepoint.com)
  44. 44. Introducing ASP.NET and the .NET Platform 15After all the notifications are out of the way, you should get a page like that in Fig­ure 1.12: Figure 1.12. Executing your first ASP.NET web pageYou can now close the Internet Explorer window. Visual Web Developer willautomatically detect this action and will cancel debugging mode, allowing you tostart editing the project again. Now let’s do something with that Label control. Set Your Default Browser to Internet Explorer When executing the project, the web site is loaded in your system’s default web browser. For the purposes of developing ASP.NET applications, we recommend configuring Visual Web Developer to use Internet Explorer, even if this is not your preferred web browser. We recommend Internet Explorer because it integrates better with Visual Web Developer’s .NET and JavaScript debugging features. For example, Visual Web Developer knows to automatically stop debugging the project when the Internet Explorer window is closed. To change the default browser to be used by Visual Web Developer, right-click the root node in Solution Explorer, choose Browse With, select a browser from the Browsers tab, and click Set as Default.For our first dynamic web page using ASP.NET, let’s write some code that willdisplay the current time inside the Label control. That mightn’t sound very exciting,but it’s only for the purposes of this simple demonstration; don’t worry, we’ll reachthe good stuff before too long. To programmatically manipulate the Label control,you’ll have to write some C# or VB.NET code, depending on the language you’vechosen when creating the project. As suggested earlier in this chapter, ASP.NETallows web forms (.aspx pages) to contain C# or VB.NET code, or they can use sep- Order the print version of this book to get all 700+ pages!
  45. 45. 16 Build Your Own ASP.NET 3.5 Web Site Using C# & VB arate files—named code-behind files—for storing this code. The Default.aspx file that was generated for you when creating the project was created with a code-behind file, and we want to edit that file now. There are many ways in which you can open that file. You can click the View Code icon at the top of the Solution Explorer window, you can right-click the Default.aspx file in Solution Explorer and choose View Code, or you can click the + symbol to expand the Default.aspx entry. No matter how you open this file, it should look like Figure 1.13 if you’re using C#, or like Figure 1.14 if you’re using VB.NET. Figure 1.13. Default.aspx.cs in Visual Web Developer C#, VB.NET, and Visual Web Developer You may be slightly alarmed, at first, by the fact that the code-behind file template that Visual Web Developer generates for the Default.aspx file in a new project when you’re using C# is completely different from the one it generates when you’re using VB.NET. They’re based on the same platform and use the same data types and features, so C# and VB.NET are fundamentally very similar. However, there are still large differences between the languages’ syntax. VB.NET is frequently preferred by beginners because its syntax is perceived to be easier to read and understand than C#. While the differences can be intimidating initially, after we discuss their details in Chapter 3, you’ll see that it can be relatively easy to understand both. Build Your Own ASP.NET 3.5 Web Site Using C# & VB (www.sitepoint.com)
  46. 46. Introducing ASP.NET and the .NET Platform 17 Figure 1.14. Default.aspx.vb in Visual Web DeveloperLooking at Figure 1.13 and Figure 1.14 you can see that the C# version contains adefinition for a method called Page_Load, while the VB.NET version doesn’t. Thisis the method that executes automatically when the project is executed, and wewant to use it to write the code that will display the current time inside the Labelcontrol. Figure 1.15. Default.aspx in Design view in Visual Web Developer Order the print version of this book to get all 700+ pages!
  47. 47. 18 Build Your Own ASP.NET 3.5 Web Site Using C# & VB If you’re using VB.NET, you’ll need to generate the Page_Load method first. The easiest way to have Visual Web Developer generate Page_Load for you is to open Default.aspx—not its code-behind file—and switch to Design view (as shown in Figure 1.15). If you double-click on an empty place on the form, an empty Page_Load method will be created in the code-behind file for Default.aspx. Now edit the Page_Load method so that it looks like this, selecting the version that applies to your chosen language: Visual Basic LearningASPVBDefault.aspx.vb (excerpt) Partial Class _Default Inherits System.Web.UI.Page Protected Sub Page_Load(ByVal sender As Object, ➥ ByVal e As System.EventArgs) Handles Me.Load myTimeLabel.Text = DateTime.Now.ToString() End Sub End Class C# LearningASPCSDefault.aspx.cs (excerpt) public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { myTimeLabel.Text = DateTime.Now.ToString(); } } C# is Case Sensitive C#, unlike VB, is case sensitive. If you type the case of a letter incorrectly, the page won’t load. If these languages look complicated, don’t worry: you’ll learn more about them in Chapter 3. If you’ve never done any server-side programming before, the code may look a little scary. But before we analyze it in detail, let’s load the page and test that it works for real. To see your dynamically generated web page content in all its glory, hit F5 to execute the project again, and see the current date and time, as depicted in Fig­ ure 1.16. Build Your Own ASP.NET 3.5 Web Site Using C# & VB (www.sitepoint.com)
  48. 48. Introducing ASP.NET and the .NET Platform 19 Figure 1.16. Loading Default.aspx with a Label element with dynamically generated contentBoth versions of the page achieve exactly the same thing. You can even save themboth, giving each a different filename, and test them separately. Alternatively, youcan create two Visual Web Developer projects—one for C# code, in C:Learnin­gASPCS, and one for VB.NET code, in C:LearningASPVB. No Time? If the time isn’t displayed in the page, chances are that you opened the file directly in your browser instead of loading it through your web server. Because ASP.NET is a server-side language, your web server needs to process the file before it’s sent to your browser for display. If it doesn’t gain access to the file, the ASP.NET code is never converted into HTML that your browser can understand, so make sure you load the page by executing it in Visual Web Developer. Loading the page in your browser directly from Windows Explorer will not execute the code, and consequently the time won’t display.So how does the code work? Let’s break down some of the elements that make upthe page. We’re defining a method called Page_Load, in both languages: Visual Basic LearningASPVBDefault.aspx.vb (excerpt) Protected Sub Page_Load(ByVal sender As Object, ➥ ByVal e As System.EventArgs) Handles Me.Load Order the print version of this book to get all 700+ pages!
  49. 49. 20 Build Your Own ASP.NET 3.5 Web Site Using C# & VB C# LearningASPCSDefault.aspx.cs (excerpt) protected void Page_Load(object sender, EventArgs e) { I won’t go into too much detail here. For now, all you need to know is that you can write script fragments that are run in response to different events, such as a button being clicked or an item being selected from a drop-down. What the first line of code basically says is, “execute the following script whenever the page is loaded.” Note that C# groups code into blocks with curly braces ({ and }), while Visual Basic uses statements such as End Sub to mark the end of a particular code sequence. So, the curly brace ({) in the C# code above marks the start of the script that will be executed when the page loads for the first time. Here’s the line that actually displays the time on the page: Visual Basic LearningASPVBDefault.aspx.vb (excerpt) myTimeLabel.Text = DateTime.Now.ToString() C# LearningASPCSDefault.aspx.cs (excerpt) myTimeLabel.Text = DateTime.Now.ToString(); As you can see, these .NET languages have much in common, because they’re both built on the .NET Framework. In fact, the only difference between the ways the two languages handle the above line is that C# ends lines of code with a semicolon (;). In plain English, here’s what this line says: Set the Text of myTimeLabel to the current date and time, expressed as text Note that myTimeLabel is the value we gave for the id attribute of the <asp:Label/> tag where we want to show the time. So, myTimeLabel.Text, or the Text property of myTimeLabel, refers to the text that will be displayed by the tag. DateTime is a class that’s built into the .NET Framework; it lets you perform all sorts of useful functions with dates and times. The .NET Framework has thousands of these classes, which do countless handy things. The classes are collectively known as the .NET Framework Class Library. Build Your Own ASP.NET 3.5 Web Site Using C# & VB (www.sitepoint.com)
  50. 50. Introducing ASP.NET and the .NET Platform 21The DateTime class has a property called Now, which returns the current date andtime. This Now property has a method called ToString, which expresses that dateand time as text (a segment of text is called a string in programming circles). Classes,properties, and methods: these are all important words in the vocabulary of anyprogrammer, and we’ll discuss them in more detail a little later in the book. Fornow, all you need to take away from this discussion is that DateTime.Now.To-String() will give you the current date and time as a text string, which you canthen tell your <asp:Label/> tag to display.The rest of the script block simply ties up loose ends. The End Sub in the VB code,and the } in the C# code, mark the end of the script that’s to be run when the pageis loaded: Visual Basic LearningASPVBDefault.aspx.vb (excerpt) End Sub C# LearningASPCSDefault.aspx.cs (excerpt) }One final thing that’s worth investigating is the code that ASP.NET generated foryou. It’s clear by now that your web browser receives only HTML (no server-sidecode!), so what kind of HTML was generated for that label? The answer is easy tofind! With the page displayed in your browser, you can use the browser’s View Sourcefeature to view the page’s HTML code. Here’s what you’ll see: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title> Welcome to Build Your Own ASP.NET 3.5 Web Site! </title> </head> <body> <form name="form1" method="post" action="Default.aspx" id="form1"> <div> <input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" Order the print version of this book to get all 700+ pages!
  51. 51. 22 Build Your Own ASP.NET 3.5 Web Site Using C# & VB value="…" /> </div> <div> <p>Hello there!</p> <p> The time is now: <span id="myTimeLabel">5/13/2008 3:10:38 PM</span> </p> </div> </form> </body> </html> Notice that all the ASP.NET code has gone? Even the <asp:Label/> tag has been replaced by a <span> tag (which has the same id attribute as the <asp:Label/> tag we used) that contains the date and time. There’s a mysterious hidden input element named __VIEWSTATE that is used by ASP.NET for certain purposes, but we’ll ignore it for now. (Don’t worry, we’ll discuss it a bit later in the book!) That’s how ASP.NET works. From the web browser’s point of view, there’s nothing special about an ASP.NET page; it’s just plain HTML like any other. All the ASP.NET code is run by your web server and converted to plain HTML that’s sent to the browser. So far, so good, but the example above was fairly simple. The next chapter will get a bit more challenging as we investigate some valuable programming con­ cepts. Getting Help As you develop ASP.NET web applications, you’ll undoubtedly have questions that need answers, and problems that need to be solved. Help is at hand—Microsoft has developed the ASP.NET support web site.2 This portal provides useful information for the ASP.NET community, such as news, downloads, articles, and discussion forums. You can also ask questions of the experienced community members in the SitePoint Forums.3 2 http://www.asp.net/ 3 http://www.sitepoint.com/forums/ Build Your Own ASP.NET 3.5 Web Site Using C# & VB (www.sitepoint.com)
  52. 52. Introducing ASP.NET and the .NET Platform 23SummaryIn this chapter, you learned about .NET. You also learned of the benefits of ASP.NET,and that it’s a part of the .NET Framework.First, you learned about the components of ASP.NET. Then we explored the softwarethat’s required not only to use this book, but also in order for you or your companyto progress with ASP.NET development.You’ve gained a solid foundation in the basics of ASP.NET! The next chapter willbuild on this knowledge as we begin to introduce you to ASP.NET in more detail,covering page structure, the languages that you can use, various programming con­cepts, and the finer points of form processing. Order the print version of this book to get all 700+ pages!
  53. 53. Build Your Own ASP.NET 3.5 Web Site Using C# & VB (www.sitepoint.com)
  54. 54. 2 ChapterASP.NET BasicsSo far, you’ve learned what ASP.NET is, and what it can do. You’ve installed thesoftware you need to get going, and you even know how to create a simple ASP.NETpage. Don’t worry if it all seems a little bewildering right now, because, as this bookprogresses, you’ll learn how easy it is to use ASP.NET at more advanced levels.As the next few chapters unfold, we ters/abs1

×