1. What This Book Is About
This book is a collection of ASP.NET 2.0 recipes that aims to help you quickly and efficiently solve many of the day-to-day problems you face when developing web applications with the .NET platform. Our recipes run the gamut from simple coding techniques to more comprehensive development strategies that the most experienced ASP.NET programmers will savor. Revised and updated for ASP.NET 2.0, the ASP.NET 2.0 Cookbook is your ultimate ASP.NET 2.0 code sourcebook.
Tip
This book is tailored to ASP.NET 2.0. Thus, the vast majority of the code will not run in the ASP.NET 1.x environment. If youre working with ASP.NET 1.x, consider instead OReillys ASP.NET Cookbook (the first edition of this book).
More than a compilation of tips and tricks, the ASP.NET 2.0 Cookbook solves realworld programming problems and is rooted in our experience as professional programmers who have designed and built richly functional web-based projects for various corporate clients. We think we know the kinds of problems that you face, and we aim to help you solve them.
The ASP.NET 2.0 Cookbook contains dozens of code examples, ranging from relatively simple 10-liners to comprehensive, multipage solutions. Without solid and complete working examples, its difficult to make an informed decision about whether an approach is the right one or whether you should be looking elsewhere. We are convinced that reading good example code is the best path to understanding any development platform, so weve included lots of it and commented our listings to help you follow the logic.
The ASP.NET 2.0 Cookbook is written in classic OReilly Cookbook style to focus directly on problems you face today or are likely to face in the future. Using a problem-and-solution format, we make it easy for you to skim for a near match to your particular problem. We have pared down the headings to a bare minimum so you can assess if a recipe is pertinent.
Many of us occasionally browse through cookbooks looking for new recipe ideas or exploring the nuances of a culinary style. Similarly, we hope you find this book sufficiently interesting to browse, because in many respects, it is as much about software techniques and methodology as it is about ASP.NET. For example, we offer a full course of error-handling recipes because we believe the topic is important to our audience and there isnt enough information about it in general circulation. We could have restricted our discussion to page-level error handling, but that seems inadequate to us. Instead, we prefer to help you deal with error handling at the application level, a more difficult subject but ultimately more useful to serious developers. We have done the heavy lifting on this and many other important subjects so that you dont have to.
Code reuse is central among the techniques that serious developers consider important, and weve gone to some lengths to illustrate its application in ASP.NET. For example, includes a recipe that sets the focus to a specific control when a page is first loaded, something that could have been accomplished by including some JavaScript in the pages HTML. However, we take a different track by programmatically generating JavaScript client-side code. The reason stems from the type of development work we do, where we are constantly under the gun to quickly generate high-quality code, the ultimate in short-order cooking. Thus, we are constantly looking for ways to reuse code. By creating a forms library, complete with custom classes that can programmatically generate Java-Script, we are able to build custom forms with a few calls to the library. Its an approach that has proven highly successful for us, and we felt it was important to provide you with a glimpse of it. This is just one of many reuse-oriented approaches youll find in this book.
2. Who This Book Is For
This book is for journeyman programmers who know the basics of ASP.NET. That said, were confident that if you are a novice ASP.NET programmer, you will find a home here provided you have done some ASP.NET and either VB or C# programming and are willing to invest time in closely studying our code.
Because this book is not a complete reference for ASP.NET 2.0, it is unlikely to appeal to you if you have merely dabbled in ASP.NET up to this point. You will be better served by first reading a general introduction to ASP.NET programming, such as Programming ASP.NET 2.0 , by Jesse Liberty and Dan Hurwitz (OReilly), where topics are dosed out in measured spoonfuls. After you have mastered the basics, youll be ready to read this book. We encourage you to look to a general reference or to the MSDN Library when you have routine questions about ASP.NET.
The generous feedback we have received on the first edition of the book tells us that many readers have found the ASP.NET Cookbook to be a great way to learn ASP.NET. Some have even recommended that aspiring ASP.NET developers should read it from cover to cover. We are delighted that some readers have used the book this way, but we anticipate that most will reach for this edition only when they need to solve a development problem, so we have organized it for this purpose. Still, we hope you find, as others have, that after months of such use, the book is looking a bit tattered but your ASP.NET code is looking quite good.
3. How This Book Is Organized
This book is organized into 21 chapters, each of which focuses on a distinct ASP.NET 2.0 topic area.
,
Master PagesMaster pages are designed to help reduce the replication of identical HTML in your application pages, and the recipes in this chapter show how to make the most of them. The first recipe shows you how to create a quick master/content page arrangement to familiarize you with the approach for using master pages. Next, we cover how to extend a master pages content to include content for other application pages, which is useful when you want your applications login page to appear in one style and the pages that follow it to appear another. The third recipe shows you how to change to another master page without having to edit all of the pages in an application, a common scenario when reuse is important to your application. The last recipe describes how to set the master page programmatically, which can be important when you want to change the applications appearance at runtime. Besides showing you some useful techniques for using master pages, these recipes provide the consistent appearance for all the recipes in the book.
,
Tabular DataIn its simplest form, displaying tabular data is easy in ASP.NET 2.0: drop in an ASP.NET grid control, connect to the database, and bind it to the control. This is simple enough but it doesnt take long before you realize that the default appearance and behavior of the control is lacking. Indeed, you may even find that the control youve chosen is too bulky, slow, or confusing when it comes to adding to or modifying its behavior. This chapter helps you make a well-informed decision about which control to use and then provides you with some recipes to solve common development problems as you adapt the control to your liking. Special emphasis is given to ASP.NET 2.0s new GridView
control, for which we provide several recipes and discussions on the latest ASP.NET 2.0 techniques.