1748 Beginning ASP.NET 3.5 ebook Free download

  TM Wrox Programmer to Programmer

  Beginning

ASP.NET 3.5

  In C# and VB Imar Spaanjaars

  Updates, source code, and Wrox technical support at www.wrox.com

  

Beginning

ASP.NET 3.5

  

Chapter 1: Getting Started with ASP.NET 3.5 . . . . . . . . . . . . . . . . . . . . . . . . . 1

Chapter 2: Building an ASP.NET Web Site . . . . . . . . . . . . . . . . . . . . . . . . . . . 33 Chapter 3: Designing Your Web Pages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63 Chapter 4: Working with ASP.NET Controls . . . . . . . . . . . . . . . . . . . . . . . . . 103 Chapter 5: Programming Your ASP.NET Web Pages . . . . . . . . . . . . . . . . . . . 135 Chapter 6: Creating Consistent Looking Web Sites . . . . . . . . . . . . . . . . . . . 193 Chapter 7: Navigation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 235 Chapter 8: User Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 267 Chapter 9: Validating User Input. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 291 Chapter 10: ASP.NET AJAX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 321 Chapter 11: Introduction to Databases. . . . . . . . . . . . . . . . . . . . . . . . . . . . 353 Chapter 12: Displaying and Updating Data . . . . . . . . . . . . . . . . . . . . . . . . . 383 Chapter 13: LINQ. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 425 Chapter 14: Presenting Data — Advanced Topics . . . . . . . . . . . . . . . . . . . . 475 Chapter 15: Security in Your ASP.NET 3.5 Web Site . . . . . . . . . . . . . . . . . . 517 Chapter 16: Personalizing Web Sites . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 557 Chapter 17: Exception Handling, Debugging, and Tracing . . . . . . . . . . . . . . 591 Chapter 18: Deploying Your Web Site . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 631 Appendix A: Exercise Answers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 665

Appendix B: Configuring SQL Server 2005 . . . . . . . . . . . . . . . . . . . . . . . . . 687

Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 707

  

Beginning

ASP.NET 3.5

  

In C# and VB

Imar Spaanjaars Beginning ASP.NET 3.5: In C# and VB Published by Wiley Publishing, Inc.

  10475 Crosspoint Boulevard Indianapolis, IN 46256 Copyright © 2008 by Wiley Publishing, Inc., Indianapolis, Indiana Published simultaneously in Canada

  ISBN: 978-0-470-18759-3 Manufactured in the United States of America 10 9 8 7 6 5 4 3 2 1 Library of Congress Cataloging-in-Publication Data Spaanjaars, Imar.

  Beginning ASP.NET 3.5 / Imar Spaanjaars. p. cm. Includes index.

  ISBN 978-0-470-18759-3 (pbk. : web) 1. Active server pages. 2. Web sites—Design. 3. Microsoft .NET. I. Title. TK5105.8885.A26S6815 2006 005.2'76—dc22

  2007052406

No part of this publication may be reproduced, stored in a retrieval system or transmitted in any form or by any means,

electronic, mechanical, photocopying, recording, scanning or otherwise, except as permitted under Sections 107 or 108

of the 1976 United States Copyright Act, without either the prior written permission of the Publisher, or authorization

through payment of the appropriate per-copy fee to the Copyright Clearance Center, 222 Rosewood Drive, Danvers,

MA 01923, (978) 750-8400, fax (978) 646-8600. Requests to the Publisher for permission should be addressed to the Legal

Department, Wiley Publishing, Inc., 10475 Crosspoint Blvd., Indianapolis, IN 46256, (317) 572-3447, fax (317) 572-4355, or

online at http://www.wiley.com/go/permissions.

  

Limit of Liability/Disclaimer of Warranty: The publisher and the author make no representations or warranties with

respect to the accuracy or completeness of the contents of this work and specifically disclaim all warranties, including

without limitation warranties of fitness for a particular purpose. No warranty may be created or extended by sales or

promotional materials. The advice and strategies contained herein may not be suitable for every situation. This work is

sold with the understanding that the publisher is not engaged in rendering legal, accounting, or other professional serv-

ices. If professional assistance is required, the services of a competent professional person should be sought. Neither the

publisher nor the author shall be liable for damages arising herefrom. The fact that an organization or Website is referred

to in this work as a citation and/or a potential source of further information does not mean that the author or the pub-

lisher endorses the information the organization or Website may provide or recommendations it may make. Further,

readers should be aware that Internet Websites listed in this work may have changed or disappeared between when

this work was written and when it is read.

  

For general information on our other products and services please contact our Customer Care Department within the

United States at (800) 762-2974, outside the United States at (317) 572-3993 or fax (317) 572-4002.

  

Trademarks: Wiley, the Wiley logo, Wrox, the Wrox logo, Wrox Programmer to Programmer, and related trade dress are

trademarks or registered trademarks of John Wiley & Sons, Inc. and/or its affiliates, in the United States and other coun-

tries, and may not be used without written permission. All other trademarks are the property of their respective owners.

  Wiley Publishing, Inc., is not associated with any product or vendor mentioned in this book.

Wiley also publishes its books in a variety of electronic formats. Some content that appears in print may not be avail-

able in electronic books.

  

To my dad I know you’d be proud

  A b o u t t h e A u t h o r

Imar Spaanjaars graduated in Leisure Management at the Leisure Management School in the Netherlands,

but he quickly changed his career path into the Internet world.

  After working for a large corporation and doing some freelance work, he now works for Design IT

  www.designit.nl

  ( ), an IT company in the Netherlands specializing in Internet and intranet applica- tions built with Microsoft technologies like ASP.NET 3.5. As a technical director and software designer, he’s responsible for designing and building medium- to large-scaled e-commerce web sites and portals. He’s also the tech lead for Dynamicweb Nederland, the Dutch branch of the popular Danish Content

  www.dynamicweb.nl Management System Dynamicweb ( ).

  Imar has written books on ASP.NET 2.0 and Macromedia Dreamweaver, all published under the Wrox

  p2p.wrox.com

  brand. He is also one of the top contributors to the Wrox Community Forum at , where he shares his knowledge with fellow programmers. Imar lives in Utrecht, the Netherlands, with his girlfriend, Fleur. You can contact him through his personal

  http://imar.spaanjaars.com web site at .

  Credits

  Acquisitions Director Jim Minatel Development Editor Brian Herrmann Lead Technical Editor Peter Lanoie Technical Editors Alexei Gorkov John Dunagan Robert Searing Editorial Manager Mary Beth Wakefield Production Manager Tim Tate

  Vice President and Executive Group Publisher Richard Swadley Vice President and Executive Publisher Joseph B. Wikert Project Coordinator, Cover Lynsey Stanford Compositor Laurie Stewart, Happenstance Type-O-Rama Proofreaders Kathryn Duggan David Parise Rachel Gunn Indexer Melanie Belkin

  Acknowledgments Writing a book is probably one of the most exhausting but fun and rewarding things I have ever done.

  During writing you have to invest a lot of time and effort to put your ideas down into something that is worth reading by others. After the hard work is done and the book is written, the reward comes from readers like you who send me e-mails, contact me through my web site, or participate in the online dis- cussion forums at p2p.wrox.com to discuss the book.

  As Norman Mailer put it, writing a book is the closest that men ever get to childbearing. Although I think there is probably some truth in that statement, I also realize there is one big difference: writing a book is not something you have to do on your own. Although only my name is on the cover, I owe a lot to many people who helped me write this book.

  First of all I’d like to thank Jim Minatel from Wiley for asking me to pick up this project and having faith in my ability to bring it to a good end. I would also like to thank Brian Herrmann for his editorial work. I know it wasn’t always easy with the number of reviewers we had, but I think it turned out pretty well. I am very thankful for the work done by the technical editors on this book: Alexei, John, and Rob — thanks, guys, for all your hard work! I particularly want to thank the lead technical editor, Peter Lanoie, who has made a major contribution, both in shaping the direction of the book and in assuring its technical accuracy. Thank you, Peter! I am also very glad for the support I got from the people at Design IT. Thanks to all who have reviewed my work and participated in my discussions on the book’s direction.

  Another person I owe a lot to is Anne Ward from Blue Violet, a UK-based web and graphic design com- pany. Anne has done most of the designs used in this book, which I highly appreciate. Thanks, Anne! The concert pictures you see in this book come from her good friend Nigel D. Nudds, who kindly let me use pictures from his collection.

  Finally, I would like to thank my lovely girlfriend, Fleur. You may get tired of hearing it, but I really appreciate the support you have given me throughout this project. I couldn’t — and wouldn’t — have done it without you!

  Contents Acknowledgments xi Introduction xxiii

Chapter 1: Getting Started with ASP.NET 3.5

  2 Getting Visual Web Developer

  27 The Sample Application

  43

  39 Adding Existing Files

  39 The Many File Types of an ASP.NET 3.5 Web Site

  36 Working with Files in Your Web Site

  35 Creating and Opening a New Web Site

  34 Choosing the Right Web Site Template

  34 Different Project Types

  33 Creating Web Sites with VWD 2008

  31 Chapter 2: Building an ASP.NET Web Site

  30 Exercises

  30 Summary

  28 Practical Tips on Visual Web Developer

  26 Resetting Your Changes

  3 Installing Visual Web Developer Express Edition

  1 Microsoft Visual Web Developer

  25 Customizing Toolbars

  23 Customizing the Document Window

  22 Modifying the Toolbox

  22 Rearranging Windows

  21 Customizing the IDE

  15 Informational Windows

  15 The Main Development Area

  14 A Tour of the IDE

  10 A First Look at ASP.NET Markup

  9 Understanding HTML

  5 An Introduction to ASP.NET 3.5

  3 Creating Your First ASP.NET 3.5 Web Site

  25 Customizing Keyboard Shortcuts Contents Organizing Your Site

  44 Special File Types

  46 Working with Web Forms

  47 The Different Views on Web Forms

  47 Choosing between Code Behind and Pages with Inline Code

  48 Adding Markup to Your Page

  53 Connecting Pages

  59 Practical Tips on Working with Web Forms

  61 Summary

  61 Exercises

  62 Chapter 3: Designing Your Web Pages

  63 Why Do You Need CSS?

  63 Problems of HTML Formatting

  64 How CSS Fixes Formatting Problems

  65 An Introduction to CSS

  65 CSS — The Language

  69 The Style Sheet

  69 Adding CSS to Your Pages

  80 Working with CSS in Visual Web Developer

  82 Creating New Styles in External Style Sheets

  83 Creating Embedded and Inline Style Sheets

  88 Applying Styles

  94 Managing Styles

  96 Practical Tips on Working with CSS

  99 Summary 100 Exercises 100

Chapter 4: Working with ASP.NET Controls 103 Introduction to Server Controls

  103 A Closer Look at ASP.NET Server Controls 107 Defining Controls in Your Pages

  108 Common Properties for All Controls 108

  Types of Controls 110

  Standard Controls 111 HTML Controls 124 Data Controls 124 Validation Controls 125 Navigation Controls 125 Login Controls 125 xiv xv Contents

  Ajax Extensions 125 WebParts 125

  The ASP.NET State Engine 126

  What Is State and Why Is It Important? 126 How the State Engine Works 127 Not All Controls Rely on ViewState 131 A Note About ViewState and Performance 131

  Practical Tips on Working with Controls 132 Summary 132 Exercises 133

  

Chapter 5: Programming Your ASP.NET Web Pages 135

Introduction to Programming 136 Data Types and Variables 136

  Converting Data Types 140 Using Arrays and Collections 142

  Statements 146

  Operators 147 Making Decisions 154 Loops 161

  Organizing Code 164

  Methods: Functions and Subroutines 165 The App_Code Folder 167 Organizing Code with Namespaces 171 Writing Comments 173

  Object Orientation Basics 176

  Important OO Terminology 176 Events 188

  Practical Tips on Programming 189 Summary 190 Exercises 191

  

Chapter 6: Creating Consistent Looking Web Sites 193

Consistent Page Layout with Master Pages 194 Creating Master Pages 196 Creating Content Pages 198

  Using a Centralized Base Page 203

  An Introduction to the ASP.NET Page Life Cycle 204 Implementing the Base Page 206 Creating Reusable Page Templates 210 xvi Contents

  Themes 214

  Programmatic Redirection 259

  Adding Logic to Your User Controls 277

  Site-Wide Registration of User Controls 274 User Control Caveats 275

  Creating User Controls 268 Adding User Controls to a Content Page or Master Page 271

  Introduction to User Controls 267

  Chapter 8: User Controls 267

  Practical Tips on Navigation 264 Summary 264 Exercises 265

  Programmatically Redirecting the Client to a Different Page 259 Server-Side Redirects 261

  Architecture of the Navigation Controls 242 Examining the Web.sitemap File 242 Using the Menu Control 244 Using the TreeView Control 253 Using the SiteMapPath Control 257

  Different Types of Themes 215 Choosing Between Theme and StyleSheetTheme 215

  Using the Navigation Controls 241

  236 Understanding Default Documents 240

  Different Ways to Move around Your Site 236 Understanding Absolute and Relative URLs

  Chapter 7: Navigation 235

  Practical Tips on Creating Consistent Pages 232 Summary 233 Exercises 234

  Creating a Skin File 229 Named Skins 231 A Final Note on Skins 232

  Skins 228

  Applying Themes 215 Extending Themes 219 Dynamically Switching Themes 222

  Creating Your Own Data Types for Properties 277 xvii Contents

  Implementing ViewState Properties 283 ViewState Considerations 288

  Creating Flicker-Free Pages 323 Providing Feedback to Users 328 Using the Timer Control 332

  Creating Your Own Tables 371

  Retrieving and Manipulating Data 358

  What Is a Database? 354 Different Kinds of Relational Databases 355 Using SQL to Work with Database Data 355

  Chapter 11: Introduction to Databases 353

  Practical Ajax Tips 349 Summary 350 Exercises 351

  What Are Web Services? 337 Creating Web Services 338 Using Web Services in Your Ajax Web Site 342 This Is Just the Beginning 349

  Using Web Services in Ajax Web Sites 337

  Introducing Ajax 322 Using ASP.NET AJAX in Your Projects 323

  Practical Tips on User Controls 289 Summary 289 Exercises 290

  Chapter 10: ASP.NET AJAX 321

  Practical Tips on Validating Data 318 Summary 318 Exercises 319

  Sending E-mail from Your Web Site 307 Reading from Text Files 313

  Processing Data at the Server 307

  Validating User Input in Web Forms 293

  Gathering Data from the User 292

  Chapter 9: Validating User Input 291

  Data Types in SQL Server 371 xviii Contents

  Understanding Primary Keys and Identities 373 Creating Relationships Between Tables 377

  Chapter 13: LINQ 425

  New Controls Introduced in ASP.NET 3.5 443 A Few Notes about Performance 471

  Using Server Controls with LINQ Queries 443

  433 Shaping Data with Anonymous Types 437

  433 Standard Query Operators

  Mapping Your Data Model to an Object Model 428 Introducing Query Syntax

  Introducing LINQ to SQL 427

  Different Types of LINQ 427

  Introducing LINQ 426

  423 Exercises 423

  Practical Database Tips 380 Summary 381 Exercises 381

  Using DetailsView to Insert and Update Data 409 Practical Tips for Displaying and Updating Data 422 Summary

  Updating and Inserting Data 409

  Customizing the Appearance of the Data Controls 403 Configuring Columns or Fields of Data-Bound Controls 403

  Filtering Data 397

  386 Inserting Data with DetailsView 392 Storing Your Connection Strings in web.config 395

  Data Source and Data-Bound Controls Working Together 386 Displaying and Editing Data with GridView

  Data-Bound Controls 384 Data Source Controls 386

  

Chapter 12: Displaying and Updating Data 383

Data Controls 383

  Practical LINQ Tips 472 Summary 472 Exercises 473 xix Contents

  

Chapter 14: Presenting Data — Advanced Topics 475

Formatting Your Controls Using Styles 476

  520 The Login Controls

  Understanding Profile 558

  Chapter 16: Personalizing Web Sites 557

  Practical Security Tips 554 Summary 555 Exercises 555

  Programmatically Checking Roles 551

  Configuring the Role Manager 541 Managing Users with the WSAT 542 Configuring the Web Application to Work with Roles 546

  The Role Manager 541

  525 Configuring Your Web Application 537

  Authorization: What Are You Allowed to Do? 518 An Introduction to the ASP.NET Application Services 519 Introducing the Login Controls

  An Introduction to Styles 477 Combining Styles, Themes, and Skins 481

  Identity: Who Are You? 518 Authentication: How Can You Prove Who You Are? 518

  

Chapter 15: Security in Your ASP.NET 3.5 Web Site 517

Introducing Security 518

  513 Summary 514 Exercises 515

  503 Different Ways to Cache Data in ASP.NET Web Applications 505 Practical Data Tips

  502 Common Pitfalls with Caching Data

  The ASP.NET Page and Controls Life Cycles Revisited 485 Handling Errors that Occur in the Data Source Controls 498 Caching

  Handling Events 485

  Configuring the Profile 558 Using the Profile 565 xx Contents

  Other Ways of Dealing with Profile 583

  Practical Debugging Tips 628 Summary 629 Exercises 630

  Running Your Site under IIS 643

  Creating a Simple Copy of Your Web Site 639 Publishing Your Web Site 642

  Copying Your Web Site 639

  Avoiding Hardcoded Settings 632

  Preparing Your Web Site for Deployment 632

  Chapter 18: Deploying Your Web Site 631

  Using the Standard Tracing Capabilities 622 Adding Your Own Information to the Trace 626 Tracing and Performance 628 A Security Warning 628

  Anonymous Identification 583 Cleaning Up Old Anonymous Profiles 584 Looking at Other Users’ Profiles 585

  Debugging Client-Side Script 618 Tracing Your ASP.NET Web Pages 621

  Moving around in Debugged Code 611 Debugging Windows 612

  The Basics of Debugging 607 Tools Support for Debugging 611

  Different Types of Errors 592 Catching and Handling Exceptions 594 Global Error Handling and Custom Error Pages 601

  

Chapter 17: Exception Handling, Debugging, and Tracing 591

Exception Handling 592

  Practical Personalization Tips 589 Summary 589 Exercises 590

  Installing and Configuring the Web Server 643 Understanding Security in IIS 651 NTFS Settings for Planet Wrox 652 Troubleshooting Web Server Errors 656 xxi Contents

  Moving Data to a Remote Server 657

  Using the Database Publishing Wizard 658 Recreating the Database 660

  The Deployment Checklist 661 What’s Next 662 Summary 663 Exercises 664

  Appendix A: Exercise Answers 665

Appendix B: Configuring SQL Server 2005 687

  Configuring SQL Server 2005 687

  Terminology and Concepts 688 Using SQL Server Management Studio 689 Enabling Remote Connections in SQL Server 690

  Connecting Your Application to SQL Server 2005 693 Configuring Application Services

  701 Configuring Your Database for the Application Services 702 Overriding the LocalSqlServer Connection String 703 Overriding the Settings of the Application Services 704

  Index 707

  I n t r o d u c t i o n

  To build effective and attractive database-driven web sites, you need two things: a solid and fast frame- work to run your web pages on and a rich and extensive environment to create and program these web pages. With ASP.NET 3.5 and Visual Web Developer 2008 you get both. Together they form the platform to create dynamic and interactive web applications.

  ASP.NET 3.5 builds on top of its popular predecessor ASP.NET 2.0. While maintaining backward compati- bility with sites built using this older version, the Microsoft .NET Framework 3.5 in general and ASP.NET 3.5 in particular add a lot of new, compelling features to the mix. Continuing the path of “less code” that was entered with the 2.0 version of the .NET Framework, ASP.NET 3.5 lets you accomplish more with even less code. New features like LINQ that are added to the .NET Frame work allow you to access a database with little to no handwritten code. The integration of Microsoft ASP.NET Ajax into the ASP.NET Framework and Visual Web Developer means you can now create fast-responding and spiffy web interfaces simply by dragging a few controls onto your page and setting a few properties. This book gives you an in-depth look at both of these technologies. The support for Cascading Style Sheets (CSS), the language to lay out and format web pages, has under- gone a major overhaul in Visual Web Developer. The design-time support, that shows you how a page will eventually look in the browser, has been vastly improved. Additionally, Visual Web Developer now ships with a lot of tools that make writing CSS a breeze.

  However, drag-and-drop support and visual tools are not the only things you’ll learn from this book. ASP.NET 3.5 and Visual Web Developer 2008 come with a great and extensive set of tools to help you program your web applications. These tools range from the new LINQ syntax that allows you to query data and databases in your web applications, to the vastly improved debugging capabilities that allow you to debug your application from client-side JavaScript all the way up into your server-side code, all with the same familiar user interface, commands, and actions.

  Under the hood, ASP.NET 3.5 makes use of the same run time as version 2.0. This ensures a great back- ward compatibility with that version, which means that ASP.NET 2.0 applications continue to run under the new framework. But don’t be fooled by the fact that the run time hasn’t changed. Although the tech- nical underpinnings needed to execute your web application haven’t changed, the .NET 3.5 Framework and ASP.NET add a lot of new features, as you’ll discover in this book. Probably the best thing about Visual Web Developer 2008 is its price: it’s available for free. Although the commercial versions of Visual Studio 2008 ship with Visual Web Developer, you can also download and install the free Express Edition. This makes Visual Web Developer 2008 and ASP.NET 3.5 probably the most attractive and compelling web development technologies available today. Introduction xxiv W h o m T h i s B o o k I s Fo r

  This book is for anyone who wants to learn how to build rich and interactive web sites that run on the Microsoft platform. With the knowledge you gain from this book, you create a great foundation to build any type of web site, ranging from simple hobby-related web sites to sites you may be creating for com- mercial purposes.

  Anyone new to web programming should be able to follow along because no prior background in web development is assumed. The book starts at the very beginning of web development by showing you how to obtain and install Visual Web Developer. The chapters that follow gradually introduce you to new tech- nologies, building on top of the knowledge gained in the previous chapters.

  Do you have a strong preference for Visual Basic over C# or the other way around? Or do you think both languages are equally cool? Or maybe you haven’t made up your mind yet and want to learn both languages? Either way, you’ll like this book because all code examples are presented in both languages! Even if you’re already familiar with previous versions of ASP.NET, with the 1.x versions in particular, you may gain a lot from this book. Although many concepts from ASP.NET 2.0 are brought forward into ASP.NET 3.5, you’ll discover there’s a host of new stuff to be found in this book, including an introduc- tion to LINQ, the new CSS and JavaScript debugging tools, new ASP.NET controls, and integrated sup- port for ASP.NET Ajax.

  W h a t T h i s B o o k C ov e r s

  This book teaches you how to create a feature-rich, data-driven, and interactive web site. Although this is quite a mouthful, you’ll find that with Visual Web Developer 2008 this isn’t as hard as it seems. You’ll see the entire process of building a web site, from installing Visual Web Developer 2008 in Chapter 1 all the way up to putting your web application on a live server in Chapter 18. The book is divided into 18 chapters, each dealing with a specific subject.

  ❑

  Chapter 1, “Getting Started with ASP.NET 3.5.” In this chapter you’ll see how to obtain and

  install Visual Web Developer 2008. You’ll get instructions for downloading and installing the free edition of Visual Web Developer 2008, called the Express Edition. You are also introduced to HTML, the language behind every web page. The chapter closes with an overview of the customization options that Visual Web Developer gives you.

  ❑

  Chapter 2, “Building an ASP.NET Web Site.” This chapter shows you how to create a new

  web site and how to add new elements like pages to it. Besides learning how to create a well- structured site, you also see how to use the numerous tools in Visual Web Developer to create HTML and ASP.NET pages. ❑

  Chapter 3, “Designing Your Web Pages.” Visual Web Developer comes with a host of tools

  that allow you to create well-designed and attractive web pages. In this chapter, you see how to make good use of these tools. Additionally, you learn about CSS, the language that is used to format web pages. ❑

  Chapter 4, “Working with ASP.NET Controls.” ASP.NET Server Controls are one of the most

  important concepts in ASP.NET. They allow you to create complex and feature-rich web sites with very little code. This chapter introduces you to the large number of server controls that are available, explains what they are used for, and shows you how to use them.

  Introduction

  ❑

  Chapter 5, “Programming Your ASP.NET Web Pages.” Although the built-in CSS tools and the ASP.NET Server Controls can get you a long way in creating web pages, you are likely to use a

  programming language to enhance your pages. This chapter serves as an introduction to pro- gramming with a strong focus on programming web pages. Best of all: all the examples you see in this chapter (and the rest of the book) are in both Visual Basic and C#, so you can choose the language you like best.

  Consistency is important to give your web ❑

  Chapter 6, “Creating Consistent Looking Web Sites.”

  site an attractive and professional appeal. ASP.NET helps you create consistent-looking pages through the use of master pages, which allow you to define the global look and feel of a page. Skins and themes help you to centralize the looks of controls and other visual elements in your site. You also see how to create a base page that helps to centralize programming code that you need on all pages in your site. ❑

  Chapter 7, “Navigation.” To help your visitors find their way around your site, ASP.NET comes

  with a number of navigation controls. These controls are used to build the navigation structure of your site. They can be connected to your site’s central site map that defines the pages in your web site. You also learn how to programmatically send users from one page to another.

  User controls are reusable page fragments that can be used in multi- ❑

  Chapter 8, “User Controls.”

  ple web pages. As such, they are great for repeating content like menus, banners, and so on. In this chapter, you learn how to create and use user controls and enhance them with some programmatic intelligence. ❑

  

Chapter 9, “Validating User Input.” A large part of interactivity in your site is defined by the

  input of your users. This chapter shows you how to accept, validate, and process user input using ASP.NET Server Controls. Additionally, you see how to send e-mail from your ASP.NET web appli- cation and how to read from text files.

  Chapter 10, “ASP.NET Ajax. ” Microsoft ASP.NET Ajax allows you to create good-looking, flicker-

  ❑ free web pages that close the gap between traditional desktop applications and web applications.

  In this chapter you learn how to use the built-in Ajax features to enhance the presence of your web pages, resulting in a smoother interaction with the web site. ❑

  

Chapter 11, “Introduction to Databases.” Understanding how to use databases is critical to

  building modern web sites, as most modern web sites require the use of a database. You’ll learn the basics of SQL, the query language that allows you to access and alter data in a database. In addition, you are introduced to the database tools found in Visual Web Developer that help you create and manage your SQL Server databases.

  

Chapter 12, “Displaying and Updating Data.” Building on the knowledge you gained in the

  ❑ previous chapter, this chapter shows you how to use the ASP.NET data-bound and data source controls to create a rich interface that enables your users to interact with the data in the database that these controls target.

  ❑

  Chapter 13, “LINQ.” LINQ is Microsoft’s new solution for accessing objects, databases, XML, and

  more. In this chapter you’ll see how to use LINQ to SQL to access SQL Server databases. Instead of writing a lot of manual code, you create a bunch of LINQ objects that do the heavy work for you. This chapter shows you what LINQ is all about, how to use the visual LINQ designer built into Visual Web Developer, and how to write LINQ queries to get data in and out of your SQL Server database.

  Chapter 14, “Presenting Data: Advanced Topics.” While earlier chapters focused mostly on the

  ❑ technical foundations of working with data, this chapter looks at the same topic from a front- end perspective. You see how to change the visual appearance of your data through the use of

  xxv Introduction

  control styles. You also see how to interact with the data-bound controls and how to speed up your application by keeping a local copy of frequently accessed data.

  Although presented quite late in the ❑

  Chapter 15, “Security in Your ASP.NET 3.5 Web Site.”

  book, security is a first-class, important topic. This chapter shows you how to make use of the built-in ASP.NET features related to security. You learn about a number of application services that facilitate security. You also learn about how to let users sign up for an account on your web site, how to distinguish between anonymous and logged-on users, and how to manage the users in your system. ❑

  Chapter 16, “Personalizing Web Sites.” Building on the security features introduced in Chapter 15, this chapter shows you how to create personalized web pages with content tar-

  geted at individual users. You see how to configure and use the ASP.NET Profile that enables you to store personalized data for known and anonymous visitors.

  In order to understand, improve, ❑

  Chapter 17, “Exception Handling, Debugging, and Tracing.”

  and fix the code you write for your ASP.NET web pages you need good debugging tools. Visual Web Developer ships with great debugging support that enables you to diagnose the state of your application at run time, helping you to find and fix problems before your users do.

  ❑

  Chapter 18, “Deploying Your Web Site.” By the end of the book, you should have a web site that

  is ready to be shown to the world. But how exactly do you do that? What are the things you need to know and understand to put your web site out in the wild? This chapter gives the answers and provides you with a good look at configuring different production systems in order to run your final web site.

  H o w T h i s B o o k I s S t r u c t u r e d

  This book takes the time to explain concepts step-by-step using working examples and detailed explana- tions. Using the famous Wrox Try It Out and How It Works sections, you are guided through a task step by step, detailing important things as you progress through the task. Each Try It Out task is followed by a detailed How It Works section that explains the steps you performed in the exercise.

  At the end of each chapter, you find exercises that help you test the knowledge you gained in this chapter. You’ll find the answers to each question in Appendix A at the end of this book. Don’t worry if you don’t know all the answers to the questions. Later chapters do not assume you followed and carried out the tasks from the exercise sections of previous chapters.

  W h a t Yo u N e e d t o U s e T h i s B o o k

  This book assumes you have a system that meets the following requirements: ❑ Capable of running Visual Web Developer. For the exact system requirements, consult the readme file that comes with the software.

  Running Windows XP Professional Edition, Windows Vista (at least the Home Premium edition), ❑ or one of the server editions of Windows.

  xxvi

  Although you should be able to follow along with most exercises using Windows XP Home or Windows Vista Basic, some of the chapters in this book require the use of IIS, Microsoft’s web server, which only ships with the Windows versions in the requirements list.

  How It Works After each Try It Out, the actions you carried out and the code you’ve typed in will be explained in detail.

  <tr> <td style=”white-space: nowrap;”>

Boxes like this one hold important, not-to-be forgotten information that is directly

relevant to the surrounding text.

  ❑ Code or content irrelevant to the discussion is either left out completely or replaced with three subsequent dots, like this:

  ❑ Menu items that require you to click multiple submenus have a special symbol that looks like this: ➪ . For example: File ➪ New ➪ Folder.

  Request.QueryString.Get(“Id”)

  As for styles in the text: ❑ New terms and important words are italicized when they are introduced. ❑ URLs and code within the text are presented like this:

  Tips, hints, tricks, and asides to the current discussion are offset and placed in italics like this.

  4. Then read the How It Works section to find out what’s going on.

  The first chapter shows you how to obtain and install Visual Web Developer 2008, which in turn installs the Microsoft .NET Framework version 3.5 and SQL Server 2005 Express Edition; all you need is a good operating system and the drive to read this book!

  3. Follow the steps through with your copy of the code.

  2. Each step has a number.

  1. They usually consist of a set of steps.

  Try It Out Conventions The Try It Out is an exercise you should work through, following the text in the book.

  To help you get the most from the text and keep track of what’s happening, a number of conventions are used throughout the book.

  C o nv e n t i o n s

  Introduction xxvii Introduction ... Menu items go here; not shown </td>

  </tr>

  The three dots are used regardless of the programming language used in the example, so you’ll see it for C#, Visual Basic, HTML, CSS, and JavaScript. When you see it in code you’re instructed to type into the code editor, you can simply skip the three dots and anything that follows them on the same line.

  ❑ Code shown for the first time, or other relevant code, is in the following format:

  Dim albumOwner As String albumOwner = “Imar” lblOwner.Text = albumOwner

  By contrast, less important code, or code that you have seen before, looks like this:

  albumOwner = “Imar”

  ❑ Text that appears on screen often has Each Word Start With A Capital Letter, even though the original screen text uses a different capitalization. This is done to make the screen text stand out from the rest of the text.

  S o u r c e C o d e

  As you work through the examples in this book, you may choose either to type in all the code manually or to use the source code files that accompany the book. All of the source code used in this book is avail-

  www.wrox.com

  able for download at . Once at the site, locate the book’s title (either by using the Search box or by using one of the title lists) and click the Download Code link on the book’s detail page to obtain all the source code for the book. Because many books have similar titles, you may find it easiest to search by ISBN; for this book the ISBN is 978-0-470-18759-3.

  You can download the full source for this book as a single file that you can decompress with your favorite decompression tool. If you extract the source, make sure you maintain the original folder struc- ture that is part of the code download. The different decompression tools use different names for this feature, but look for a feature like Use Folder Names or Maintain Directory Structure. Once you have extracted the files from the code download, you should end up with a folder called Source and a folder called Resources. Then create a new folder in the root of your C drive, call it BegASPNET, and move the Source and Resources folders into this new folder so you end up with folders like C:\BegASPNET\Source and C:\BegASPNET\Resources. The source folder contains the source for each of the 18 chapters of this book and the final version of the PlanetWrox application that you’ll work on throughout this book. The Resources folder contains files you need during some of the exercises in this book. If everything turned out correctly, you should end up with the structure shown in Figure I-1.

  xxviii

  Introduction

  Figure I-1 Later chapters have you create folders called Site and Release inside the same C:\BegASPNET folder giving you a folder structure similar to that in Figure I-2.

  Figure I-2

  xxix Introduction

  The Site folder contains the site as you’ll build it throughout this book, while the Release folder will con- tain your final version at the end of this book. Whenever you’re stuck with some examples in this book, you can take a peek in the Source folder to see how things should have ended up. If you want to run the site for a specific chapter to see how it works, be sure to open the chapter’s folder in Visual Web Developer as a web site. So you should open C:\BegASPNET\Source\Chapter 13 rather than opening its parent folder C:\BegASPNET\Source. Sticking to this structure ensures a smooth execution of the Try It Out exercises in this book. Incorrectly mixing or nesting these folders make it harder to carry out the exercises and may even lead to unexpected situations and errors. Whenever you run into an issue or error that is not explained in this book, ensure that your site structure is still closely related to the one presented here.

  Er rata

  I have made every effort to ensure that there are no errors in the text or in the code. However, no one is perfect, and mistakes do occur. If you find an error in this book, like a spelling mistake or a faulty piece of code, I’d be very grateful for your feedback. By sending in errata you may save another reader hours of frustration and at the same time you will be helping me provide even higher quality information.

  

www.wrox.com

  To find the errata page for this book, go to and locate the title using the Search box or one of the title lists. Then, on the book details page, click the Book Errata link. On this page you can view all errata that has been submitted for this book and posted by Wrox editors. A complete book list including

  www.wrox.com/misc-pages/booklist.shtml links to each book’s errata is also available at . www.wrox.com/contact/

  If you don’t spot “your” error on the Book Errata page, go to

  techsupport.shtml

  and complete the form there to send us the error you have found. I’ll check the information and, if appropriate, post a message to the book’s errata page and fix the problem in subsequent editions of the book.

  p2p.wrox.com p2p.wrox.com

  For author and peer discussion, join the P2P forums at . The forums are a web-based system for you to post messages relating to Wrox books and related technologies and interact with other readers and technology users. The forums offer a subscription feature to e-mail you topics of interest of your choosing when new posts are made to the forums. I am a frequent visitor of the Wrox forums, and I’ll do my best to help you with any questions you may have about this book.

  p2p.wrox.com

  At you will find a number of different forums that will help you not only as you read this book, but also as you develop your own applications. To join the forums, just follow these steps:

1. Read the terms of use and click Agree.

  p2p.wrox.com Go to and click the Register Now link.

  2. xxx

  Introduction 3.

  Complete the required information to join as well as any optional information you wish to provide and click Submit.

4. You will receive an e-mail with information describing how to verify your account and com- plete the joining process.

  You can read messages in the forums without joining P2P but in order to post your own messages, you must join. After you join, you can post new messages and respond to messages other users post. You’ll find this book’s own forum under the Books category that is available from the homepage or by clicking View All Forums on the menu on the left. You can read messages at any time on the Web. If you would like to have new messages from a particular forum e-mailed to you, click the Subscribe to this Forum icon by the forum name in the forum listing. For more information about how to use the Wrox P2P, be sure to read the P2P FAQs for answers to ques- tions about how the forum software works as well as many common questions specific to P2P and Wrox books. To read the FAQs, click the FAQ link on any P2P page.

  xxxi