Table of Contents

Title Page

Copyright

About the Authors

Credits

Acknowledgments

Praise for Professional ASP.NET 3.5 SP1 Edition In C# and VB

Introduction

A Little Bit of History

The Goals of ASP.NET

Additional Features of ASP.NET 3.5 and 3.5 SP1

What You Need for ASP.NET 3.5 SP1

Who Should Read This Book?

What This Book Covers

Conventions

Source Code

Errata

p2p.wrox.com

Chapter 1: Application and Page Frameworks

Application Location Options

The ASP.NET Page Structure Options

ASP.NET 3.5 Page Directives

ASP.NET Page Events

Dealing with PostBacks

Cross-Page Posting

ASP.NET Application Folders

Compilation

Build Providers

Global.asax

Working with Classes Through VS2008

Summary

Chapter 2: ASP.NET Server Controls and Client-Side Scripts

ASP.NET Server Controls

Applying Styles to Server Controls

HTML Server Controls

Manipulating Pages and Server Controls with JavaScript

Client-Side Callback

Summary

Chapter 3: ASP.NET Web Server Controls

An Overview of Web Server Controls

The Label Server Control

The Literal Server Control

The TextBox Server Control

The Button Server Control

The LinkButton Server Control

The ImageButton Server Control

The HyperLink Server Control

The DropDownList Server Control

Visually Removing Items from a Collection

The ListBox Server Control

The CheckBox Server Control

The CheckBoxList Server Control

The RadioButton Server Control

The RadioButtonList Server Control

Image Server Control

Table Server Control

The Calendar Server Control

AdRotator Server Control

The Xml Server Control

Panel Server Control

The PlaceHolder Server Control

BulletedList Server Control

HiddenField Server Control

FileUpload Server Control

MultiView and View Server Controls

Wizard Server Control

ImageMap Server Control

Summary

Chapter 4: Validation Server Controls

Understanding Validation

Client-Side versus Server-Side Validation

ASP.NET Validation Server Controls

Turning Off Client-Side Validation

Using Images and Sounds for Error Notifications

Working with Validation Groups

Summary

Chapter 5: Working with Master Pages

Why Do You Need Master Pages?

The Basics of Master Pages

Coding a Master Page

Coding a Content Page

Specifying Default Content in the Master Page

Programmatically Assigning the Master Page

Nesting Master Pages

Container-Specific Master Pages

Event Ordering

Caching with Master Pages

ASP.NET AJAX and Master Pages

Summary

Chapter 6: Themes and Skins

Using ASP.NET Themes

Creating Your Own Themes

Defining Multiple Skin Options

Programmatically Working with Themes

Themes, Skins, and Custom Controls

Summary

Chapter 7: Data Binding in ASP.NET 3.5

Data Source Controls

Configuring Data Source Control Caching

Storing Connection Information

Using Bound List Controls with Data Source Controls

Other Databound Controls

Inline Data-Binding Syntax

Expressions and Expression Builders

Summary

Chapter 8: Data Management with ADO.NET

Basic ADO.NET Features

The DataList Server Control

The ListView Server Control

Using Visual Studio for ADO.NET Tasks

Asynchronous Command Execution

Summary

Chapter 9: Querying with LINQ

LINQ to Objects

LINQ to XML

LINQ to SQL

Summary

Chapter 10: Working with XML and LINQ to XML

The Basics of XML

XmlReader and XmlWriter

XmlDocument and XPathDocument

DataSets

The XmlDataSource Control

XSLT

Databases and XML

Summary

Chapter 11: IIS7

Modular Architecture of IIS7

Extensible Architecture of IIS7

IIS7 and ASP.NET Integrated Pipeline

Building a Customized Web Server

Internet Information Services (IIS) Manager

Moving an Application from IIS6 to IIS7

Summary

Chapter 12: Introduction to the Provider Model

Understanding the Provider

The Provider Model in ASP.NET 3.5

Configuring Providers

Summary

Chapter 13: Extending the Provider Model

Providers Are One Tier in a Larger Architecture

Modifying Through Attribute-Based Programming

Examining ProviderBase

Building Your Own Providers

Extending Pre-Existing Providers

Summary

Chapter 14: Site Navigation

XML-Based Site Maps

SiteMapPath Server Control

TreeView Server Control

Menu Server Control

SiteMap Data Provider

SiteMap API

URL Mapping

Sitemap Localization

Security Trimming

Nesting SiteMap Files

Summary

Chapter 15: Personalization

The Personalization Model

Creating Personalization Properties

Anonymous Personalization

Programmatic Access to Personalization

Personalization Providers

Managing Application Profiles

Summary

Chapter 16: Membership and Role Management

Authentication

Authorization

ASP.NET 3.5 Authentication

ASP.NET 3.5 Authorization

Using the Web Site Administration Tool

Public Methods of the Membership API

Public Methods of the Roles API

Summary

Chapter 17: Portal Frameworks and Web Parts

Introducing Web Parts

Building Dynamic and Modular Web Sites

Working with Classes in the Portal Framework

Creating Custom Web Parts

Connecting Web Parts

Summary

Chapter 18: HTML and CSS Design with ASP.NET

Caveats

HTML and CSS Overview

Summary

Chapter 19: ASP.NET AJAX

Understanding the Need for AJAX

ASP.NET AJAX and Visual Studio 2008

ASP.NET AJAX Applications

ASP.NET AJAX's Server-Side Controls

Using Multiple UpdatePanel Controls

Working with Page History

Script Combining

Summary

Chapter 20: ASP.NET AJAX Control Toolkit

Downloading and Installing

The ASP.NET AJAX Controls

ASP.NET AJAX Control Toolkit Server Controls

Summary

Chapter 21: Security

Authentication and Authorization

Applying Authentication Measures

Authenticating Specific Files and Folders

Programmatic Authorization

Identity and Impersonation

Securing Through IIS

Summary

Chapter 22: State Management

What Are Your Choices?

Understanding the Session Object in ASP.NET

The Application Object

QueryStrings

Cookies

PostBacks and Cross-Page PostBacks

Hidden Fields, ViewState, and ControlState

Using HttpContext.Current.Items for Very Short-Term Storage

Summary

Chapter 23: Caching

Caching

Caching Programmatically

Using the SQL Server Cache Dependency

Configuring Your ASP.NET Application

Testing SQL Server Cache Invalidation

Summary

Chapter 24: Debugging and Error Handling

Design-Time Support

Tracing

Debugging

Exception and Error Handling

Summary

Chapter 25: File I/O and Streams

Working with Drives, Directories, and Files

Reading and Writing Files

Working with Serial Ports

Network Communications

Summary

Chapter 26: User and Server Controls

User Controls

Server Controls

Summary

Chapter 27: Modules and Handlers

Processing HTTP Requests

HttpModules

Summary

Chapter 28: Using Business Objects

Using Business Objects in ASP.NET 3.5

COM Interop: Using COM Within .NET

Using .NET from Unmanaged Code

Summary

Chapter 29: ADO.NET Entity Framework

Can We Speak the Same Language?

Creating Your First Entity Data Model

Understanding Relationships

Inheritance Within the EDM

Using Stored Procedures

The EntityDataSource Control

Summary

Chapter 30: ASP.NET Dynamic Data

Creating Your Base Application with Visual Studio 2008

Working with Dynamic Data Routes

Controlling Display Aspects

Summary

Chapter 31: Building and Consuming Services

Communication Between Disparate Systems

Building a Simple XML Web Service

Consuming a Simple XML Web Service

Transport Protocols for Web Services

Overloading WebMethods

Caching Web Service Responses

SOAP Headers

Consuming Web Services Asynchronously

Windows Communication Foundation

Building the WCF Consumer

Summary

Chapter 32: ADO.NET Data Services

Creating Your First Service

Querying the Interface

Consuming ADO.NET Data Services in ASP.NET

Summary

Chapter 33: Localization

Cultures and Regions

ASP.NET 3.5 Resource Files

Looking at the Resource Editor

Summary

Chapter 34: Configuration

Configuration Overview

Common Configuration Settings

Creating Custom Sections

Summary

Chapter 35: Instrumentation

Working with the Event Log

Using Performance Counters

Application Tracing

Understanding Health Monitoring

Summary

Chapter 36: Administration and Management

The ASP.NET Web Site Administration Tool

Configuring ASP.NET in IIS on Vista

Summary

Chapter 37: Packaging and Deploying ASP.NET Applications

Deployment Pieces

Steps to Take before Deploying

Methods of Deploying Web Applications

Looking More Closely at Installer Options

Summary

Appendix A: Migrating Older ASP.NET Projects

Migrating Is Not Difficult

When Mixing Versions—Forms Authentication

Upgrading—ASP.NET Reserved Folders

ASP.NET 3.5 Pages Come as XHTML

No Hard-Coded .js Files in ASP.NET 3.5

Converting ASP.NET 1.x Applications in Visual Studio 2008

Migrating from ASP.NET 2.0 to 3.5

Appendix B: ASP.NET Ultimate Tools

Debugging Made Easier

References

Tidying Up Your Code

Visual Studio Add-ins

Extending ASP.NET

General Purpose Developer Tools

Summary

Appendix C: Silverlight 2 and ASP.NET

Getting Started

Silverlight Controls

Silverlight and JavaScript

Summary

Appendix D: ASP.NET Is Greater Than ASP.NET MVC

Abstraction: What Web Forms Does Well

The Leak: Where Web Forms Doesn't Exactly Fit

Back to Basics: ASP.NET MVC Believes…

Caring About Testability

Common Reactions to ASP.NET MVC

Why “(ASP.NET > ASP.NET MVC) == True”

Summary

Appendix E: ASP.NET Online Resources

Author Blogs

ASP.NET Influential Blogs

Web Sites

Index

Wiley Publishing, Inc. End-User License Agreement

Advertisement

Professional ASP.NET 3.5 SP1 Edition: In C# and VB

Published by

Wiley Publishing, Inc.

10475 Crosspoint Boulevard

Indianapolis, IN 46256

www.wiley.com

Copyright © 2008 by Wiley Publishing, Inc., Indianapolis, Indiana

Published simultaneously in Canada

ISBN: 978-0-470-47826-4

Library of Congress Cataloging-in-Publication Data

Evjen, Bill.

Professional ASP.NET 3.5 SP1 edition : in C# and VB / Bill Evjen, Scott Hanselman, Devin Rader.

p. cm.

Includes index.

ISBN 978-0-470-47826-4 (cloth/cd-rom)

1. Active server pages. 2. Microsoft .NET. 3. C# (Computer program language) 4. Microsoft Visual BASIC.

I. Hanselman, Scott. II. Rader, Devin, 1976- III. Title.

TK5105.8885.A26E96 2009

006.7’882—dc22

2009004158

No part of this publication may be reproduced, stored in a retrieval system or transmitted in any form or by any means, electronic, mechanical, photocopying, recording, scanning or otherwise, except as permitted under Sections 107 or 108 of the 1976 United States Copyright Act, without either the prior written permission of the Publisher, or authorization through payment of the appropriate per-copy fee to the Copyright Clearance Center, 222 Rosewood Drive, Danvers, MA 01923, (978) 750-8400, fax (978) 646-8600. Requests to the Publisher for permission should be addressed to the Permissions Department, John Wiley & Sons, Inc., 111 River Street, Hoboken, NJ 07030, (201) 748-6011, fax (201) 748-6008, or online at http://www.wiley.com/go/permissions.

Limit of Liability/Disclaimer of Warranty: The publisher and the author make no representations or warranties with respect to the accuracy or completeness of the contents of this work and specifically disclaim all warranties, including without limitation warranties of fitness for a particular purpose. No warranty may be created or extended by sales or promotional materials. The advice and strategies contained herein may not be suitable for every situation. This work is sold with the understanding that the publisher is not engaged in rendering legal, accounting, or other professional services. If professional assistance is required, the services of a competent professional person should be sought. Neither the publisher nor the author shall be liable for damages arising herefrom. The fact that an organization or Web site is referred to in this work as a citation and/or a potential source of further information does not mean that the author or the publisher endorses the information the organization or Web site may provide or recommendations it may make. Further, readers should be aware that Internet Web sites listed in this work may have changed or disappeared between when this work was written and when it is read.

For general information on our other products and services please contact our Customer Care Department within the United States at (877) 762-2974, outside the United States at (317) 572-3993 or fax (317) 572-4002.

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 countries, and may not be used without written permission. All other trademarks are the property of their respective owners. Wiley Publishing, Inc., is not associated with any product or vendor mentioned in this book.

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

About the Authors

Bill Evjen is an active proponent of .NET technologies and community-based learning initiatives for .NET. He has been actively involved with .NET since the first bits were released in 2000. In the same year, Bill founded the St. Louis .NET User Group (www.stlnet.org), one of the world's first such groups. Bill is also the founder and former executive director of the International .NET Association (www.ineta.org), which represents more than 500,000 members worldwide.

Based in St. Louis, Missouri, USA, Bill is an acclaimed author and speaker on ASP.NET and XML Web Services. He has authored or co-authored more than fifteen books including Professional C# 2008, Professional VB 2008, ASP.NET Professional Secrets, XML Web Services for ASP.NET, and Web Services Enhancements: Understanding the WSE for Enterprise Applications (all published by Wiley Publishing, Inc.). In addition to writing, Bill is a speaker at numerous conferences, including DevConnections, VSLive, and TechEd. Along with these items, Bill works closely with Microsoft as a Microsoft Regional Director and an MVP. Bill is the Global Head of Platform Architecture for Lipper (www.lipperweb.com), as part of Thomson Reuters, the international news and financial services company. He graduated from Western Washington University in Bellingham, Washington, with a Russian language degree. When he isn't tinkering on the computer, he can usually be found at his summer house in Toivakka, Finland. You can reach Bill at bill.evjen.public@gmail.com

Scott Hanselman works for Microsoft as a Senior Program Manager in the Developer Division, aiming to spread the good word about developing software, most often on the Microsoft stack. Before this he worked in eFinance for 6+ years and before that he was a Principal Consultant at a Microsoft Partner for nearly 7 years. He was also involved in a few things like the MVP and RD programs and will speak about computers (and other passions) whenever someone will listen to him. He blogs at http://www.hanselman.com and podcasts at http://www.hanselminutes.com and contributes to http://www.asp.net, http://www.windowsclient.net, and http://www.silverlight.net.

Devin Rader is a Product Manager on the Infragistics Web Client team, responsible for leading the creation of Infragistics ASP.NET and Silverlight products. Devin is also an active proponent and member of the .NET developer community, being a co-founder of the St. Louis .NET User Group, an active member of the New Jersey .NET User Group, a former board member of the International .NET Association (INETA), and a regular speaker at user groups. He is also a contributing author on the Wrox title Silverlight 1.0 and a technical editor for several other Wrox publications and has written columns for ASP.NET Pro magazine, as well as .NET technology articles for MSDN Online. You can find more of Devin's musings at www.geekswithblogs.com/devin.

Credits

Development Editor

Lori Cerreto

Technical Editor

Alexei Gorkov

Production Editor

Daniel Scribner

Copy Editor

Nancy Rapoport

Editorial Manager

Mary Beth Wakefield

Production Manager

Tim Tate

Vice President and Executive Group Publisher

Richard Swadley

Vice President and Executive Publisher

Barry Pruett

Associate Publisher

Jim Minatel

Project Coordinator, Cover

Lynsey Stanford

Proofreader

Publication Services, Inc.

Indexer

J & J Indexing

Acknowledgments

I have said it before and I'll say it again: Writing a book may seem like the greatest of solo endeavors, but it requires a large team of people working together to get technical books out the door—and this book is no exception. First and foremost, I would like to thank Jim Minatel of Wrox for giving me the opportunity to write the original ASP.NET book, which then led to this special edition. There is nothing better than getting the opportunity to write about your favorite topic for the world's best publisher!

Besides Jim, I worked with Adaobi Obi Tulton on the first edition and then Lori Cerreto as development editor and Nancy Rapoport as copy editor on this SP1 Edition. Without their efforts, this book would not have happened.

I worked closely with Scott Hanselman and Devin Rader on the original edition of this book, and these guys deserve a lot of thanks. I appreciate your help (and Devin, I appreciate the new appendix for this edition) and advice throughout the process. Thanks guys!

Finally, thanks to my entire family. Book writing is a devil in disguise as it is something that I love to do but at the same time, takes way too much time away from my family. Thanks to my family for putting up with this and for helping me get these books out the door. I love you all.

—Bill Evjen

Praise for Professional ASP.NET 3.5 In C# and VB

“Bill, Scott and Devin are long-time ASP.NET experts, and the authors of several best selling ASP.NET and .NET books. This latest book is outstanding and provides an excellent end-to-end resource for almost all things ASP.NET related … The book is very well organized, with a nice balance of text, code samples, and screen-shots … . All in all, a great book and a fantastic addition to any ASP.NET developer's library.”

Scott Guthrie,

Microsoft Corporation,

Vice President--.NET Developer Division

“If there's one book to own on ASP.NET 3.5 this is it!”

Rob Zelt,

President of INETA,

Microsoft ASP.NET MVP

“I've got the ASP.NET 2.0 version of this book (both the original and special edition versions) and all of the strengths still hold: It still walks you through all of the common (and some of the uncommon) usage for ASP.NET and provides great examples and code snippets to illustrate points. I'm not an ASP.NET newbie and I still find myself referring to the book from time to time… ”

Travis Illig

“These 3 authors and their editors delivered a book the ASP.net developer will find invaluable, and help them write better code, solve tougher problems, and elegantly understand and apply quite a bit of the richness the Framework offers in real world ASP.net web site building and maintenance. Bravo, Bravisimo!”

Charles M. Carroll

What's on the CD

Adobe PDF e-book version of this complete book

7 PDF Wrox Blox that normally sell for $6.99 (US) each:

Internet Explorer 8 and Its Impact on Your ASP.NET Web Sites by Robert Boedigheimer

Jumping from ASP.NET to Silverlight 2 by Daniel Crenna

Leverage LINQ in ASP.NET 3.5 Projects by Roger Jennings

Dynamic Control Survivability in ASP.NET by Jerome DiMarzio

ASP.NET AJAX Control Development with Visual Studio 2008 and .NET 3.5 Framework by Damien White

Leveraging httpModules for Better ASP.NET Applications by Chris Love

Leveraging httpHandlers to Stream Custom Content in ASP.NET by Chris Love

Introduction

Simply put, ASP.NET 3.5 is an amazing technology to use to build your Web solutions! When ASP.NET 1.0 was introduced in 2000, many considered it a revolutionary leap forward in the area of Web application development. ASP.NET 2.0 was just as exciting and revolutionary, and ASP.NET 3.5 Service Pack 1 (SP1) is continuing a forward march in providing the best framework today in building applications for the Web. ASP.NET 3.5 SP1 continues to build on the foundation laid by the release of ASP.NET 1.0 by focusing on the area of developer productivity.

This book covers the whole of ASP.NET. It not only introduces new topics, it also shows you examples of these new technologies in action. So sit back, pull up that keyboard, and enjoy!

A Little Bit of History

Before organizations were even thinking about developing applications for the Internet, much of the application development focused on thick desktop applications. These thick-client applications were used for everything from home computing and gaming to office productivity and more. No end was in sight for the popularity of this application model.

During that time, Microsoft developers developed thick-client applications using mainly Visual Basic (VB).

Visual Basic was not only a programming language—it was tied to an IDE that allowed for easy thick-client application development. In the Visual Basic model, developers could drop controls onto a form, set properties for these controls, and provide code behind them to manipulate the events of the control. For example, when an end user clicked a button on one of the Visual Basic forms, the code behind the form handled the event.

Then, in the mid-1990s, the Internet arrived on the scene. Microsoft was unable to move the Visual Basic model to the development of Internet-based applications. The Internet definitely had a lot of power, and right away, the problems facing the thick-client application model were revealed. Internet-based applications created a single instance of the application that everyone could access. Having one instance of an application meant that when the application was upgraded or patched, the changes made to this single instance were immediately available to each and every user visiting the application through a browser.

To participate in the Web application world, Microsoft developed Active Server Pages (ASP). ASP was a quick and easy way to develop Web pages. ASP pages consisted of a single page that contained a mix of markup and languages. The power of ASP was that you could include VBScript or JScript code instructions in the page executed on the Web server before the page was sent to the end user's Web browser. This was an easy way to create dynamic Web pages customized based on instructions dictated by the developer.

ASP used script between brackets and percentage signs—<% %>—to control server-side behaviors. A developer could then build an ASP page by starting with a set of static HTML. Any dynamic element needed by the page was defined using a scripting language (such as VBScript or JScript). When a user requested the page from the server by using a browser, the asp.dll (an ISAPI application that provided a bridge between the scripting language and the Web server) would take hold of the page and define all the dynamic aspects of the page on-the-fly based on the programming logic specified in the script. After all the dynamic aspects of the page were defined, the result was an HTML page output to the browser of the requesting client.

Lesen Sie weiter in der vollständigen Ausgabe!

Lesen Sie weiter in der vollständigen Ausgabe!

Lesen Sie weiter in der vollständigen Ausgabe!

Lesen Sie weiter in der vollständigen Ausgabe!

Lesen Sie weiter in der vollständigen Ausgabe!

Lesen Sie weiter in der vollständigen Ausgabe!

Lesen Sie weiter in der vollständigen Ausgabe!

Lesen Sie weiter in der vollständigen Ausgabe!

Lesen Sie weiter in der vollständigen Ausgabe!

Lesen Sie weiter in der vollständigen Ausgabe!

Lesen Sie weiter in der vollständigen Ausgabe!

Lesen Sie weiter in der vollständigen Ausgabe!

Lesen Sie weiter in der vollständigen Ausgabe!

Lesen Sie weiter in der vollständigen Ausgabe!

Lesen Sie weiter in der vollständigen Ausgabe!

Lesen Sie weiter in der vollständigen Ausgabe!

Lesen Sie weiter in der vollständigen Ausgabe!

Lesen Sie weiter in der vollständigen Ausgabe!

Lesen Sie weiter in der vollständigen Ausgabe!

Lesen Sie weiter in der vollständigen Ausgabe!

Lesen Sie weiter in der vollständigen Ausgabe!

Lesen Sie weiter in der vollständigen Ausgabe!

Lesen Sie weiter in der vollständigen Ausgabe!

Lesen Sie weiter in der vollständigen Ausgabe!

Lesen Sie weiter in der vollständigen Ausgabe!

Lesen Sie weiter in der vollständigen Ausgabe!

Lesen Sie weiter in der vollständigen Ausgabe!

Lesen Sie weiter in der vollständigen Ausgabe!

Lesen Sie weiter in der vollständigen Ausgabe!

Lesen Sie weiter in der vollständigen Ausgabe!

Lesen Sie weiter in der vollständigen Ausgabe!

Lesen Sie weiter in der vollständigen Ausgabe!

Lesen Sie weiter in der vollständigen Ausgabe!

Lesen Sie weiter in der vollständigen Ausgabe!

Lesen Sie weiter in der vollständigen Ausgabe!

Lesen Sie weiter in der vollständigen Ausgabe!

Lesen Sie weiter in der vollständigen Ausgabe!

Lesen Sie weiter in der vollständigen Ausgabe!

Lesen Sie weiter in der vollständigen Ausgabe!

Lesen Sie weiter in der vollständigen Ausgabe!

Lesen Sie weiter in der vollständigen Ausgabe!

Lesen Sie weiter in der vollständigen Ausgabe!

Lesen Sie weiter in der vollständigen Ausgabe!

Lesen Sie weiter in der vollständigen Ausgabe!

Lesen Sie weiter in der vollständigen Ausgabe!