What you need to know
This book is for beginners who have no previous experience of ASP, C#, XML, object- oriented programming, or the .NET Framework. A little knowledge of HTML is useful, but by no means essential as all the concepts that you need in order to create dynamic ASP. NET web sites are presented and explained in full.
What you will learn from this book
This book will teach you how to build tailored ASP. NET web sites from the ground up. It covers the following broad topics (see the book's Introduction for a more detailed explation):
The early part of the book is dedicated to familiarizing you with the anatomy and basic functionality of HTML, XML, ASRNET, the .NET Framework, and C#. This done, it looks at the way in which they can be used together to create flexible web sites.
The s ecQnd part of the book introduces object-oriented programming which is crucial to maximizing your benefit from .NET. Ideas are explained in detail with many programmatic examples and real-world analogies.
Having 4aid the groundwork we then progress to discussing the technologies and techniques that ASRNET can draw upon to increase its functionality. This includes'ADO. NET for data source access, Web Services for inter-web site communication and the encapsulation ideas of Server and User Controls to ease code maintenance and reuse.
The book concludes by considering the ways in which you can optimize your ASP. NET sites to increase their speed, security, and robustness; detailed advice on debugging is also included.
Introduction
What does this Book Cover?
Who is this Book For?
What do you Need to use This Book?
Conventions
Customer Support
How to Download the Sample Code for the Book
Errata
E-mail Support
p2p.wrox.com
Why this System Offers the Best Support
Chapter 1: Getting Started with ASP.NET
What is a Static Web Page?
How are Static Web Pages Served?
The Limitations of Static Web Pages
What is a Web Server?
How are Dynamic Web Pages Served?
Two Ways of Providing Dynamic Web Page Content
Client-Side Dynamic Web Pages
Server-Side Dynamic Web Pages
An Overview of the Technologies
Client-Side Technologies for Providing Dynamic Content
Server-Side Technologies for Providing Dynamic Content
What is ASP.NET?
How does ASP.NET Differ from ASP?
I'm Still Confused about ASP, ASP. NET, and C#
The Installation Process
Installing the IlS 5.x Web Server
Working with IlS
Testing your Installation
Identifying your Web Server's Name
Browsing to a Page on your Web Server
Managing Directories on your Web Server
Virtual Directories
Permissions
Configuring Directory Security
Prerequisites for Installing ASP.NET
Installing ASP.NET and the .NET Framework SDK
Troubleshooting Hints and Tips
ASP.NET Test Example
ASP.NET Troubleshooting
Program Not Found, the Result of the ASP.NET Isn't Being Displa!
Page Cannot Be Displayed: HTTP Error 403
Page Cannot Be Found: HTTP Error 404
Web Page Unavailable while Offline
I Just Get a Blank Page
The Page Displays the Message but not the Time
I Get an Error Statement Citing a Server Error
I Have a Different Problem
Summary
Chapter 2: Anatomy of an ASP.NEI Page
What is .NET?
From Your Code to Machine Code
Introducing a Common Intermediate Language
Objects, Objects Everywhere
The .NET Base Classes
The Class Browser
How ASP.NEI:Works
Saving your ASP.NET Files with an ASPX Suffix
Inserting ASP.NET Code into our Web Pages
The Tags
Inline Code Blocks (the <% %> Delimiters)
Server Controls
ASP.NET in Action
Binding to a Database
Binding to a Simple XML File
Summary
Chapter 3: Forms and HTML Server Controls
Forms in the Real World
Web Pages, HTML Forms, and Web Forms
Simple Web Theory
The HTTP Protocol
Where ASP.NET Fits in with the .NET Framework
HTML Forms
The Tag
The action Attribute
The method Attribute
HTML Form Controls
How the Tag Works in ASP.NET
ASP.NET Server Controls
The Control
Control Attributes
The Control
The Control
The Control
The and Controls
The and Controls
Summary
Exercises
Chapter 4: Storing Information in C#
What is a Variable?
Declaration
Data Types
Numeric Types
Suffixes
Character Data Types
The string Type
The char Type
Escape Sequences
Boolean Type
The Object Type
Handling Dates
Naming Variables
Case Issues
Hungarian Notation
Operators
Assignment Operator
Arithmetic Operations
Operator Precedence
Concatenating Strings
Converting Between Data Types
Implicit Conversion
Explicit Conversion
Invalid Casts
Data Conversion
String Output
Data Input
What if Conversions Fail?
Constants
Structured Data Types
Arrays
Multi-Dimensional Arrays
Jagged Arrays
Structs
Enumerations
Summary
Exercises
Chapter 5: Introducing XML
The Format of XML
Tags and Elements
Attributes
Examples of Markup Languages
SGML
HTML
Drawbacks of HTML
How XML Came About
Creating an XML Document
Beyond Data Display
WelI-Forme~l vs. Valid Documents
WelFFormed Documents
Valid Documents
Styling XML
Why Use Style Sheets?
Cascading Style Sheets
Using XML with ASP.NET
Summary
Exercises
Chapter 6: Control Structures and Procedural Programming
A Quick Look at Control Structures
Overview of Branching Structures
Overview of Looping Structures
Overview of Functions
When to Use a Particular Control Structure
Branching Structures in Detail
Comparison Operators
Logical Comparisons Using if
Logical Operators
Extending if Statements Using else if and else
The Conditional or Ternary Operator
Comparing Strings
A Word about Nested ifs and Indenting
Conditional Statements Using switch...case
Another Note on Nesting and Indenting
Looping Structures in Detail
The for Loop
The while Loop
The do...while Loop
The foreach Loop
Break and Continue
Functions in Detail
Defining Functions
Return Types and Parameters
Calling Functions
Variable Scope
Local Variables
Global Variables
Summary
Exercises
Chapter 7: Event-driven Programming and Postback
What is an Event?
What is Event-driven Programming?.
ASP.NET Events
Events in HTML
Server Control Events in ASP.NET
The ASP.NET button Server Control
Event-Driven Programming and Postback
The IsPostBack Test
Changing the Way we Program on the Web
Summary
Exercises
Chapter 8: Introduction to Objects
Organizing Your Code
So, What's an Object?
Abstraction - Modeling the Real World
Encapsulation - The Black Box
Object Properties
Object Methods
Using Objects
Where do Objects Come From?
Defining a class
Creating Objects
Setting Objects to null
Using Methods and Properties
Initializing Objects with Constructors
Overloaded Methods
Operator Overloading
An Example of Operator Overloading
More on Operator Overloading
Summary
Exercises
Chapter 9: Shared Members and Class Relationships
Shared Members
Shared Properties
Shared Methods
Class Relationships
Association - 'Uses A'
Containment - A 'Has a' Relationship
Containment in .NET
Inheritance - 'Is A'
Terminology for Inheritance
The Pros and Cons of Inheritance
System.Object - The Ultimate Base Class
The Limitations of Inheritance
Advanced Inheritance Concepts
Virtual Functions
Referring to the Current Object - The this Reference
Abstract Classes and Interfaces
Converting between Object Types
Implicit Conversions
Explicit Conversions
Boxing and Unboxing
User-Defined Conversions
Summary
Exercises
Chapter 10: Objects in ASP.NET
Namespaces
Namespaces for ASP.NET
The Page Class
Redirecting the User to Another Page
ASP,NET Core Objects
Request Object
Response Object
Server Object
State Handling
Application State
Session State
Cookies
How Do Cookies Work?
Shelf Life - How Long Do Cookies Last?
Sessions and Cookies
Summary
Exercises
Chapter 11: Objects and Structured Data
What is a Collection?
Different Types of Collection
Arrays as Collections
Searching an Array
Working on Multiple Elements in an Array
Data Binding
Pros and Cons of Working with Arrays
Getting More from Collections
ArrayList
Some More ArrayList Techniques
Hashtable
Using a Hashtable
SortedList
Summary
Exercises
Chapter !2: Reading from Data Sources
Understanding Modern Databases
Data Models
Normalization and Relational Databases
ADO.NET
.NET Data Providers
ADO.NET Objects
The Connection Object
Connecting to Northwind
The Command Object and DataReader
Simple Data Binding
The DataSet and DataTable Objects
Microsoft SQL Server and ADO.NET
Catching Errors
Summary
Exercises
Chapter 13: Manipulating Data Sources
Disconnected Data
Methods of Updating Data
DataSets and DataTables
The DataRow Object
Updating the Original Data Source
The Command Object
The DataAdapter Object
The CommandBuilder Object
The DataAdapter. Update Method
Referential I nte, grity
Updating Data - Summary
Using SQL
Adapting the DataAdapter Commands
Using Stored Procedures
Direct SQL Commands
Using SQL - Summary
XML
XML into Strings
XML in a DataSet
What to Study Next
Summary
Exercises
Chapter 14: ASP.NET Server Controls
Other Types of Controls
HTML Server Controls
HTML Server Controls vs. ASP.NET Server Controls
User Controls
ASP,NET Server Controls
Rich Object Model
Automatic Browser Detection
Properties
Events
Page Lifecycle
Page_Load
Event Handling
Page_Unload
Control Families
Intrinsic Controls
Validation Controls
Rich Controls
Data Rendering Controls
The XML Source - MyCalendar.xml
The Web Form - MyCalendar.aspx
Editing the Data Using Templates
Summary
Exercises
Chapter 15: Reusable Code for ASP.NET
Encapsulation
Components
Why use Components?
Applying Component Theory to our Applications
User Controls
When Should we use User Controls?
Code Behind
Using Code Behind
Inheritance
Summary
Exercises
Chapter 16:.NET Assemblies and Custom Controls
Three-Tier Application Design
ASP. NET Application Design
.NET Assemblies
What is Compilation?
Accessing a Component from within an ASP.NET Page
XCopy Deployment
Accessing Assemblies in Other Locations
Writing Code in Other Languages
Data Access Components
Custom Server Controls
What are Custom Controls?
How are Custom Controls Different from User Controls?
How are Custom Controls different from Business Objects?
How It All Fits Together
Summary
Exercises
Chapter 17: Debugging and Error Handling
A Few Good Habits
Good Coding Practice
How and from where do Errors Arise?
Syntax Errors
Logical Errors
Trapping Invalid Data
System Errors
Finding Errors
Debug mode
Tracing
Page~Level Tracing
Writing to the trace Icg
Application-Level Tracing
Handling Errors
On Error Goto...?
Structured Error Handling
Exceptions
Using try...catch...finally
Multiple catch Blocks
What You Can Put in a catch Block
And finally
Generating Exceptions using throw
Nested try Blocks
Handling Errors Programmatically
Notification and Logging
Customized Error Messages
Writing to the Event Log
System.Diagnostics Namespace
Mailing the Site Administrator
System.Web. Mail Namespace
Summary
Chapter 18: Web Services
What is a Web Service?
HTTP, XML, and Web Services
HTTP GET
HTTP POST
Simple Object Access Protocol (SOAP)
Building an ASP.NET Web Service
Processing Directive
Namespaces
Public Class
Web Methods
Testing your Web Service
Using your Web Service
Consuming a Web Service
How does a Proxy Work?
Creating a Proxy
Web Service Discovery
Securing a Web Service
Username-Password Combination or Registration Keys
Secure Sockets Layer (SSL)
IP Address Restriction
Other Considerations
Network Connectivity
Asynchronous Method Calls
Service Hijacking (or Piggybacking)
Provider Solvency
The Interdependency Scenario
Summary
Exercises
Chapter 19: Configuration and Optimization
Configuration Overview
Browsing .config Fifes
The Configuration Files
Configuration File Rules
Configuration File Format
The Structure of the Configuration Files
General Configuration
Page Configuration
Application Settings
Custom Errors
Performance Optimization
Caching
output Caching
Fragment Caching
Tracing
Monitoring the ASP, NET Process
Tips and Tricks
Summary
Chapter 20: ASP.NET Security
What is Security?
Types of Security
Authentication
Implementing Basic Authentication
Weighing up Basic Authentication
Integrated Windows Authentication
Weighing up Integrated Windows Authentication
implementing Forms-based Authentication
Forms-based Authentication using a database
Encryption using SSL
Microsoft Passport Authentication
What is Microsoft Passport?
Why Passport?
Technologies supported by Passport
Summary
Index
ASP.NET 1.0 is the latest incarnation of Microsoft's Active Server Pages (ASP). It is a powerful server based technology designed to create dynamic, interactive, HTML pages on demand for your World Wide Web site, or corporate intranet.
ASP.NET constitutes a key element in Microsoft's .NET Framework, providing web-based access to the immensely powerful .NET development environment. It allows us to create web applications in a new, flexible, way by encapsulating commonly used code into object-oriented controls of various kinds that can be fired by events initiated by our site's users.
ASP.NET branches out into many other technologies, such as Web Services, ADO.NET, Custom Controls, and Security. We will briefly touch upon its relationship with these fields throughout the book to provide a solid, comprehensive understanding of how ASP.NET can be used to benefit your work in a practical way.
By the end of this book you will be familiar with the anatomy of ASP.NET and be able to create flexible, secure, and robust web sites that can collect and work with information in a multitude of ways to the benefit of both yourself and your users.
What does this Book Cover?
Here is a quick breakdown of what you will find within the chapters of this book: Chapter 1: Getting Started with ASP.NET - in this first chapter we introduce ASP.NET and look at some of the reasons that you'd want to use server-side code for creating web pages and the technologies that are available to do so. This done we spend the bulk of the chapter explaining the ASP.NET installation process in detail, along with the ancillary installation of MDAC 2.7. We finish up with a simple ASP.NET example page to check that our installation is working correctly.
Chapter 2: Anatomy of an ASP.NET Page - having completed the installation in the previous chapter we consider the structure of an ASP.NET page and the way that it functions in relation to the .NET Framework. We use examples to demonstrate how the page is parsed by the ASP.NET module.
Chapter 3: Forms and HTML Server Controls - we really get to grips with ASP.NET code for the first time in this chapter, as we introduce ASP.NET server controls in relation to their HTML counterparts. We begin to demonstrate the additional functionality that they provide us with.
Chapter 4: Storing Information in C# - having acquainted ourselves with the basics of ASP.NET controls this chapter considers the use of variables for holding data in C#. We look at how variables are implemented, what they can contain, and how they can be placed into your ASP.NET pages.
Chapter 5: Introducing XML - complementing the previous chapter's discussion of variables, here we introduce eXtensible Markup Language (XML). We consider the implications of self- describing XML data, its plethora of uses, and its strengths and weaknesses when compared with HTML.
Chapter 6: Control Structures and Procedural Programming - this chapter takes a whirlwind tour of the key building blocks of C# in the context of an ASP.NET page. We learn how to make our ASP.NET pages more responsive through the use of C# branching and jumping structures that enable us to control the order in which our program's statements execute.
Chapter 7: Event-driven Programming and Postback - we cover the fundamental ideas of the event-driven way that ASP.NET works in this chapter. We learn what an event is and how it impacts on the way that we think about, and work with, ASP.NET pages. We also discuss the concept of 'postback' and how it is used to pass information between ASP.NET pages and your server.
Chapter 8' Introduction to Objects - this chapter is the first of four dealing with the idea of objects. ASP.NET pages derive a great deal of their flexibility and power from the object-oriented way in which they are structured. This chapter lays the groundwork for a solid understanding of this technique. It introduces concepts such as properties, methods, constructors, and overloadingusing plentiful examples relating to real-world objects to aid your understanding.
Chapter 9' Shared Members and Class Relationships - this chapter builds on the one before it and introduces some of the relationships it is possible to establish between objects in order to massively increase their functionality and power. In particular we consider the idea of inheritance and how an object can take advantages of the functionality of another objects simply by 'inheriting' them from it.
Chapter 10:Objects in ASP.NET - we begin this chapter with a high-level look at the .NET Framework from an object-oriented perspective, considering some of the useful objects and namespaces that it provides us with. Before moving on to pay particular attention to the Response, Request, and Browser objects that provide us with a great deal of day-to-day functionality. The chapter concludes by introducing the concept of state management in .NET and discussing how and where we can store the state of our pages.
Chapter 11: Objects and Structured Data - here we discuss the idea of collections, and how they work to hold groups of objects together as a cohesive unit that we can manipulate and organize as if it were one object. In particular we look at the use of the Array and Hashtable classes for containing information we wish to keep together (such as lists of users and their telephone numbers).
Chapter 12: Reading from Data Sources - by this point in the book we're familiar with the basic anatomy of ASP.NET pages and object-oriented design, so we branch out to look at ADO.NET in the context of ASP.NET. Most specifically we look at the use of the Connection and Command objects for opening data sources and retrieving information into DataSets.
Chapter 13: Manipulating Data Sources - having mastered the basics ~ reading data in the previous chapter we take things much further; looking in detail at the way we can manipulate the information in our DataSets, and store the results back to the data source from which they came.
Chapter 14: ASP.NET Server Controls - this chapter explains how ASP.NET server controls derive their properties and methods from the various classes and objects that make up the .NET Framework. It explains the syntax required to make their functionality available, together with a look at the benefits that these controls can give.
Chapter 15: Reusable Code for ASP.NET - here we consider the great benefits that can be achieved by encapsulating our code to make it more maintainable. Firstly we cover the idea of User Controls - designed to store sections of your ASP.NET code that are repeated on multiple pages of your site - before going on to consider the idea of codebehind, where the