cover_01

Table of Contents

Cover

Title Page

Copyright

Dedication

About the Author

Credits

Acknowledgments

Foreword

Introduction

Who This Book Is For

What This Book Covers

How This Book Is Structured

What You Need to Use This Book

Conventions

Source Code

Errata

p2p.wrox.com

Chapter 1: Getting Started with ASP.NET 4

Microsoft Visual Web Developer

Creating Your First ASP.NET 4 Web Site

An Introduction to ASP.NET 4

A Tour of the IDE

Customizing the IDE

The Sample Application

Practical Tips on Visual Web Developer

Summary

EXERCISES

Chapter 2: Building an ASP.NET Web Site

Creating Web Sites with VWD 2010

Working with Files in Your Web Site

Working with Web Forms

Practical Tips on Working with Web Forms

Summary

EXERCISES

Chapter 3: Designing Your Web Pages

Why Do You Need CSS?

An Introduction to CSS

Working with CSS in Visual Web Developer

Practical Tips on Working with CSS

Summary

EXERCISES

Chapter 4: Working with ASP.NET Server Controls

Introduction to Server Controls

A Closer Look at ASP.NET Server Controls

Types of Controls

The ASP.NET State Engine

Practical Tips on Working with Controls

Summary

EXERCISES

Chapter 5: Programming Your ASP.NET Web Pages

Introduction to Programming

Data Types and Variables

Statements

Organizing Code

Object Orientation Basics

Practical Tips on Programming

Summary

EXERCISES

Chapter 6: Creating Consistent Looking Web Sites

Consistent Page Layout with Master Pages

Using a Centralized Base Page

Themes

Skins

Practical Tips on Creating Consistent Pages

Summary

EXERCISES

Chapter 7: Navigation

Different Ways to Move Around Your Site

Using the Navigation Controls

Programmatic Redirection

Practical Tips on Navigation

Summary

EXERCISES

Chapter 8: User Controls

Introduction to User Controls

Adding Logic to Your User Controls

Practical Tips on User Controls

Summary

EXERCISES

Chapter 9: Validating User Input

Gathering Data from the User

Processing Data at the Server

Practical Tips on Validating Data

Summary

EXERCISES

Chapter 10: ASP.NET AJAX

Introducing Ajax

Using ASP.NET AJAX in Your Projects

Using Web Services and Page Methods in Ajax Web Sites

Practical Ajax Tips

Summary

EXERCISES

Chapter 11: jQuery

An Introduction to jQuery

jQuery Syntax

Modifying the DOM with jQuery

Effects with jQuery

jQuery and Extensibility

Practical Tips on jQuery

Summary

EXERCISES

Chapter 12: Introducing Databases

What Is a Database?

Different Kinds of Relational Databases

Using SQL to Work with Database Data

Retrieving and Manipulating Data with SQL

Creating Your Own Tables

Practical Database Tips

Summary

EXERCISES

Chapter 13: Displaying and Updating Data

Data Controls

Data Source and Data-bound Controls Working Together

Customizing the Appearance of the Data Controls

Updating and Inserting Data

Practical Tips for Displaying and Updating Data

Summary

EXERCISES

Chapter 14: LINQ and the ADO.NET Entity Framework

Introducing LINQ

Introducing the ADO.NET Entity Framework

Mapping Your Data Model to an Object Model

Introducing Query Syntax

Using Server Controls with LINQ Queries

Practical LINQ and ADO.NET Entity Framework Tips

Summary

EXERCISES

Chapter 15: Working with Data — Advanced Topics

Formatting Your Controls Using Styles

Handling Events

Hand-Coding Data Access Code

Caching

Practical Data Tips

Summary

EXERCISES

Chapter 16: Security in Your ASP.NET 4 Web Site

Introducing Security

Introducing the Login Controls

The Role Manager

Practical Security Tips

Summary

EXERCISES

Chapter 17: Personalizing Web Sites

Understanding Profile

Other Ways of Dealing with Profile

Practical Personalization Tips

Summary

EXERCISES

Chapter 18: Exception Handling, Debugging, and Tracing

Exception Handling

The Basics of Debugging

Tools Support for Debugging

Debugging Client-Side Script

Tracing Your ASP.NET Web Pages

Practical Debugging Tips

Summary

EXERCISES

Chapter 19: Deploying Your Web Site

Preparing Your Web Site for Deployment

Copying Your Web Site

Running Your Site under IIS

Moving Data to a Remote Server

The Deployment Checklist

What’s Next

Summary

EXERCISES

Appendix A

Chapter 1

Chapter 2

Chapter 3

Chapter 4

Chapter 5

Chapter 6

Chapter 7

Chapter 8

Chapter 9

Chapter 10

Chapter 11

Chapter 12

Chapter 13

Chapter 14

Chapter 15

Chapter 16

Chapter 17

Chapter 18

Appendix B

Configuring SQL Server 2008

Configuring Application Services

Index

Take your library wherever you go.

Title Page

To my friends

About the Author

Imar Spaanjaars graduated in Leisure Management at the Leisure Management School in the Netherlands, but he quickly changed his career path into the Internet world.

After working in the Internet business at various web agencies for the past twelve years, he recently started up his own company called De Vier Koeden (www.devierkoeden.nl), a small Internet agency specializing in consultancy and development of Internet and intranet applications with Microsoft technologies such as ASP.NET 4.

Imar has written books on ASP.NET and Macromedia Dreamweaver, all published under the Wrox brand. He is also one of the top contributors to the Wrox Community Forum at p2p.wrox.com, where he shares his knowledge with fellow programmers.

In 2008 and 2009, Imar received Microsoft’s Most Valuable Professional (MVP) award for his contributions to the ASP.NET community.

Imar lives in Utrecht, the Netherlands, with his girlfriend, Fleur. You can contact him through his personal web site at http://imar.spaanjaars.com or by e-mail at imar@spaanjaars.com.

Credits

Acquisitions Editor

Paul Reese

Project Editor

Brian Herrmann

Technical Editor

Michael J. Apostol

Production Editor

Rebecca Anderson

Copy Editor

Kim Cofer

Editorial Director

Robyn B. Siesky

Editorial Manager

Mary Beth Wakefield

Associate Director of Marketing

David Mayhew

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

Compositor

Craig Johnson, Happenstance Type-O-Rama

Proofreader

Josh Chase, Word One New York

Indexer

Robert Swanson

Cover Designer

Michael E. Trent

Cover Image

© Nevin Giesbrecht/istockphoto

Acknowledgments

Just as with my prior books, I really enjoyed writing this one. The process from an empty Word document at the very beginning to a printed copy as the final result is a really interesting one. Along the way, I discovered, understood and used a lot of the new features that ASP.NET 4 and Visual Web Developer 2010 bring. Since this book is a major update on the previous edition — Beginning ASP.NET 3.5 in C# and VB — I have been able to incorporate a lot of reader feedback into this edition. While working on this book, I went through the more than 450 threads in the book’s forum, looking for feedback that could improve this edition. This allowed me to add clarification and more detailed instructions where needed. If you have the previous edition and posted a question in the Wrox forums: thanks for your valuable feedback; you’ve really helped to make this book better.

Besides my readers, I owe a lot to other people who helped me write this book.

First of all I’d like to thank Brian Herrmann for his editorial work. Just as before, it was a pleasure to work with you! I also want to thank Michael J. Apostol for his work as a technical editor, and the people from Wrox for their support and contributions to this book.

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

Finally, I would like to thank my good friends Joost and René and my lovely girlfriend Fleur for their support during this project.

Foreword

This is a very nice book written by one of ASP.NET’s very own Most Valuable Professionals, Imar Spaanjaars. It takes a lot to be a Microsoft MVP — not only do you need expertise in the subject matter, you also need to be a great teacher and a technology philanthropist. Through his blogs, articles, and books, Imar has given a considerable amount of his time to benefit the web development community.

Imar has been active in the ASP.NET community for a long time and has written several books on web development, including the previous version of this book, Beginning ASP.NET 3.5. What makes this book so special is that it does not assume the reader has any prior knowledge of web development and at the same time it is a great book for anyone hoping to upgrade to ASP.NET 4. The book uses the free Visual Studio edition, Visual Web Developer Express 2010, starting from a chapter covering how to get started and then slowly introducing more advanced concepts in a seamless fashion.

In the process of helping create Visual Studio 2010 for Web Developers, I often interacted with community leaders to collect feedback on how we could make the product better for every web developer. Imar has been using ASP.NET 4 and VWD 2010 Express since their first Beta versions, and has been pouring in feedback on which bugs are important to fix and which features to emphasize. His passion for the technology is reflected in this book and I am positive that the readers of this book will feel that same passion.

ASP.NET 4 and Visual Studio 2010 have feature improvements in many areas, including building standards-compliant web sites, JScript IntelliSense, jQuery integration, Ajax, CSS improvements, HTML and markup snippets, Web Deployment, and data integration. I believe ASP.NET 4 and Visual Studio 2010 are great technologies to build fine web sites, and this book will be an excellent companion to these products, helping users excel in the world of web development.

Vishal R. JoshiSenior Program Manager LeadMicrosoft Web Platform & Toolshttp://vishaljoshi.blogspot.com

Introduction

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

ASP.NET 4 builds on top of its popular predecessors ASP.NET 2.0 and ASP.NET 3.5. While maintaining backward compatibility with sites built using these older versions, ASP.NET 4 and Visual Web Developer 2010 introduce new, exciting features and bring many smaller, but much needed changes to the framework and development tools.

With each new release of Visual Studio (which includes Visual Web Developer) since Visual Studio 2003, I am surprised by the sheer amount of new functionality and changes Microsoft has been able to put in the product. Visual Studio 2010 is no exception. A major new feature in Visual Studio 2010 is the full integration of the ADO.NET Entity Framework 4 that lets you work with databases with very little code. Another great change in Visual Studio is the use of Windows Presentation Foundation (WPF) for the User Interface which brings a better user experience and new behavior in Visual Studio itself.

Although not a new feature by itself, the inclusion of jQuery in Visual Web Developer is an excellent decision that will help you write fancier web sites in less time. jQuery is a compelling client side, cross-browser JavaScript framework and is discussed in detail in Chapter 11.

If you’re familiar with earlier versions of ASP.NET, you’ll be happy to find many small gems in the new version of the framework that will make your life as a developer easier. I’ll mention and discuss these new features throughout this book where appropriate. For a complete list of all new features in ASP.NET, check out the following white paper at the official ASP.NET web site:

http://www.asp.net/learn/whitepapers/aspnet4/

Probably the best thing about Visual Web Developer 2010 is its price: it’s still available for free. Although the commercial versions of Visual Studio 2010 ship with Visual Web Developer, you can also download and install the free Express edition. This makes Visual Web Developer 2010 and ASP.NET 4 probably the most attractive and compelling web development technologies available today.

Who This Book Is For

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 commercial purposes.

Anyone new to web programming should be able to follow along because no prior background in web development is assumed, although it helps if you do have a basic understanding of HTML and the web in general. 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 technologies, 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 have some experience with prior versions of ASP.NET, you may gain a lot from this book. Although many concepts from previous versions are brought forward into ASP.NET 4, you’ll discover there’s a lot of new stuff to be found in this book, including an introduction to the ADO.NET Entity Framework, the inclusion of jQuery, ASP.NET AJAX, the many changes to the ASP.NET 4 Framework, and much more.

What This Book Covers

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

How This Book Is Structured

This book takes the time to explain concepts step by step using working examples and detailed explanations. 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.

Since this is a beginner’s book, I can’t go into great detail on a number of topics. For pretty much each chapter in this book, you’ll easily find numerous other books that exclusively deal with the topic discussed. Where appropriate, I have included references to these books so you can easily decide where to go to next if you want to deepen your knowledge on a specific subject.

What You Need to Use This Book

This book assumes you have a system that meets the following requirements:

Although you should be able to follow along with most of the exercises using another version of Windows such as Windows XP (as long as it’s supported by Visual Web Developer), the exercises in Chapter 19 on deployment require the use of Microsoft’s web server IIS 7 or later, which only ships with the Windows versions in the requirements list.

Chapter 1 shows you how to obtain and install Visual Web Developer 2010, which in turn installs the Microsoft .NET Framework version 4 and SQL Server 2008 Express edition; all you need is a good operating system and the drive to read this book!

Service Pack 1

Service Pack 1 for Visual Web Developer 2010 Express and Visual Studio 2010 introduces a change in the way the Visual Basic editor writes methods for you in the Code Behind of your pages. The editor no longer adds the ByVal keyword to new methods it creates. Since ByVal was already the default for method parameters, this change doesn’t affect your code. When you have VS 2010 Service Pack 1 installed and you see ByVal used in the book, you can choose to type that keyword as shown in the book, or you can leave out the ByVal keyword as VWD 2010 / VS 2010 do when writing new methods. For a detailed explanation of the issue, check out this forum post:http://p2p.wrox.com/book-beginning-asp-net-4-c-vb/83002-you-using-service-pack-1-visual-web-developer-2010-express-vs-2010-a.html

Conventions

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.

Try It Out Conventions

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

1. They usually consist of a set of steps.

2. Each step has a number.

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

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

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.

note.ai

NOTE Boxes like this one hold important, not-to-be forgotten information that is directly relevant to the surrounding text.

mistakes.ai

COMMON MISTAKES Mistakes that are easily made while following the exercises are presented in a box like this. Be sure to read these carefully when you get stuck in an exercise.

As for styles in the text:

<tr>

  <td style="white-space: nowrap;">

    ... 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.

Dim roles As New ArrayList()   

roles.Add("Administrators")    

roles.Add("ContentManagers")   

To put emphasis on a block of code surrounded by other code, I used a bolded font like this:

<appSettings>

  <add key="FromAddress" value="info@planetwrox.com"/>

</appSettings>

The surrounding code is used to make it easier to see where the bolded code should be placed.

<!DOCTYPE html PUBLIC"-//W3C//DTD XHTML 1.0 Transitional//EN" 

      "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

If you’re typing this code yourself, you can put it all on one line, or use the same line breaks if you prefer.

Source Code

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 available for download from the book’s own page on the Wrox web site at www.wrox.com/go/beginningaspnet4. If somehow this link no longer works, go to www.wrox.com and locate the book either by using the Search box or by using one of the title lists. 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-50221-1.

You can download the full source for this book as a single file for each programming language used in the book (C# or Visual Basic). You can decompress these files with your favorite decompression tool. If you extract the source, make sure you maintain the original folder structure 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 19 chapters of this book and the final version of the Planet Wrox web site. 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.

f0001.tif

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.

The Site folder contains the site as you’ll build it throughout this book, while the Release folder will contain 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 a folder such as C:\BegASPNET\Source\Chapter 12 directly rather than opening its parent folder C:\BegASPNET\Source.

f0002.tif

Figure I-2

If you want to follow along in both programming languages, create a second folder called C:\BegASPNETVB to hold the files for the Visual Basic version. This way, the two sites can coexist without any problems. If you create a folder specifically for the C# language, don’t include the hash symbol (#) as that’s an invalid character in the path name for a web site.

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.

Errata

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, such as 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.

To find the errata page for this book, go to www.wrox.com/go/beginningaspnet4 or go to www.wrox.com and locate the title using the Search box or one of the title lists. Then, on the book details page, click the 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 links to each book’s errata is also available at www.wrox.com/misc-pages/booklist.shtml.

If you don’t spot “your” error on the book’s Errata page, go to www.wrox.com/contact/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

For author and peer discussion, join the P2P forums at p2p.wrox.com. 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.

At p2p.wrox.com 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. Go to p2p.wrox.com and click the Register Now link.

2. Read the terms of use and click Agree.

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 complete the joining process.

You can read messages in the forums without joining P2P but in order to post your own messages, you must join (which is free).

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 ASP.NET 4 category that is available from the homepage. 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 questions 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.