Table Of ContentASP.NET 3.5
A Beginner’s Guide
William B. Sanders
New York Chicago San Francisco
Lisbon London Madrid Mexico City
Milan New Delhi San Juan
Seoul Singapore Sydney Toronto
Copyright © 2009 by The McGraw-Hill Companies. All rights reserved. Manufactured in the United States of America. Except as permitted
under the United States Copyright Act of 1976, no part of this publication may be reproduced or distributed in any form or by any means, or
stored in a database or retrieval system, without the prior written permission of the publisher.
0-07-159195-8
The material in this eBook also appears in the print version of this title: 0-07-159194-X.
All trademarks are trademarks of their respective owners. Rather than put a trademark symbol after every occurrence of a trademarked name,
we use names in an editorial fashion only, and to the benefit of the trademark owner, with no intention of infringement of the trademark. Where
such designations appear in this book, they have been printed with initial caps.
McGraw-Hill eBooks are available at special quantity discounts to use as premiums and sales promotions, or for use in corporate training
programs. For more information, please contact George Hoare, Special Sales, at [email protected] or (212) 904-4069.
TERMS OF USE
This is a copyrighted work and The McGraw-Hill Companies, Inc. (“McGraw-Hill”) and its licensors reserve all rights in and to the work. Use
of this work is subject to these terms. Except as permitted under the Copyright Act of 1976 and the right to store and retrieve one copy of the
work, you may not decompile, disassemble, reverse engineer, reproduce, modify, create derivative works based upon, transmit, distribute,
disseminate, sell, publish or sublicense the work or any part of it without McGraw-Hill’s prior consent. You may use the work for your own
noncommercial and personal use; any other use of the work is strictly prohibited. Your right to use the work may be terminated if you fail to
comply with these terms.
THE WORK IS PROVIDED “AS IS.” McGRAW-HILL AND ITS LICENSORS MAKE NO GUARANTEES OR WARRANTIES AS TO THE
ACCURACY, ADEQUACY OR COMPLETENESS OF OR RESULTS TO BE OBTAINED FROM USING THE WORK, INCLUDING ANY
INFORMATION THAT CAN BE ACCESSED THROUGH THE WORK VIA HYPERLINK OR OTHERWISE, AND EXPRESSLY
DISCLAIM ANY WARRANTY, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO IMPLIED WARRANTIES OF MER-
CHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. McGraw-Hill and its licensors do not warrant or guarantee that the func-
tions contained in the work will meet your requirements or that its operation will be uninterrupted or error free. Neither McGraw-Hill nor its
licensors shall be liable to you or anyone else for any inaccuracy, error or omission, regardless of cause, in the work or for any damages
resulting therefrom. McGraw-Hill has no responsibility for the content of any information accessed through the work. Under no
circumstances shall McGraw-Hill and/or its licensors be liable for any indirect, incidental, special, punitive, consequential or similar damages
that result from the use of or inability to use the work, even if any of them has been advised of the possibility of such damages.
This limitation of liability shall apply to any claim or cause whatsoever whether such claim or cause arises in contract, tort or otherwise.
DOI: 10.1036/007159194X
Contents at a Glance
PART I Getting Started
1 Introduction to ASP.NET 3.5 .............................................. 3
2 ASP.NET 3.5 Tools and Development Environment ....................... 25
3 C# and ASP.NET 3.5 ...................................................... 47
4 Doing More with C# and ASP.NET ....................................... 85
PART II Forms and Controls
5 HTML Forms: A Review .................................................. 107
6 Standard Web Controls ................................................... 127
7 CSS for ASP.NET 3.5 Page Formatting ................................... 173
8 Control Events and Event Handlers ...................................... 197
9 Validation Controls ........................................................ 215
10 Rich Data Controls ........................................................ 247
vii
viii ASP.NET 3.5: A Beginner’s Guide
PART III ASP.NET and Databases
11 A SQL Primer ............................................................. 269
12 ADO.NET: Hello Database ................................................ 287
13 Data Binding .............................................................. 329
14 LINQ ...................................................................... 353
15 Files ........................................................................ 371
16 Security .................................................................... 389
17 ASP.NET Ajax ............................................................. 419
Index ...................................................................... 441
For more information about this title, click here
Contents
ACKNOWLEDGMENTS ........................................................... xvii
INTRODUCTION .................................................................. xix
PART I Getting Started
1 Introduction to ASP.NET 3.5 .............................................. 3
HTML Forms to Nowhere ........................................................... 4
Try This: HTML Data Entry with No Exit ........................................... 5
Your Browser Is a Thin Client ................................................. 5
A Protocol Without a Country: Stateless HTTP ................................ 6
ASP.NET 3.5 as an Alternative to CGI ............................................... 8
From Client Side to Server Side ..................................................... 9
.NET Organization .................................................................. 12
Development Contexts ........................................................ 14
Running ASP.NET Files on Your Computer or LAN ........................... 14
ASP.NET in the Real World ................................................... 22
Language Partners with ASP.NET ............................................. 22
2 ASP.NET 3.5 Tools and Development Environment ....................... 25
Visual Studio 2008 .................................................................. 26
Installing Visual Studio 2008 .................................................. 27
The Visual Studio 2008 Environment .......................................... 27
ix
x ASP.NET 3.5: A Beginner’s Guide
Creating a Simple ASP.NET Application ............................................ 28
ASPX and C# Files ................................................................. 38
The ASPX File ................................................................ 38
The C# File ................................................................... 39
More Visual Studio 2008 Tools ...................................................... 41
Other Tools for ASP.NET Development with C# ..................................... 44
Dreamweaver ................................................................. 44
POTE—Plain Old Text Editor ................................................. 44
3 C# and ASP.NET 3.5 ...................................................... 47
C# 3.0 and ASP.NET 3.5 ............................................................ 48
Case and Naming Variables and Instances ..................................... 49
Types, Statements, and Expressions ........................................... 50
Basic C# Structures ................................................................. 54
Variables and Constants ....................................................... 54
Operators and Punctuators .......................................................... 58
Basic Operators ............................................................... 59
Symbols in Grammar and Punctuators ......................................... 63
Conditional Statements .............................................................. 64
If and Else .................................................................... 64
Loops ......................................................................... 69
Arrays ........................................................................ 78
4 Doing More with C# and ASP.NET ....................................... 85
Events and Handling Them .......................................................... 86
Adding ASP.NET Controls Events to C# ...................................... 86
Adding Statements to the Event Handlers ...................................... 88
Creating and Using Classes ......................................................... 92
Making a Simple C# Class .................................................... 92
Using Interfaces ..................................................................... 99
Doing More with C# and Object-Oriented Programming ............................. 103
PART II Forms and Controls
5 HTML Forms: A Review .................................................. 107
Working with Form Tags and Attributes ............................................. 108
HTML Tag Properties ......................................................... 109
Try This: Using HTML Forms with ASP.NET Web Sites ............................. 109
C# and HTML Tags ........................................................... 111
HTML Buttons ................................................................ 112
Try This: Using HTML Buttons ..................................................... 112
Standard Buttons and HTML Forms ........................................... 113
Try This: Using Standard ASP.NET Buttons ......................................... 113
Contents xi
Using HTML Forms Interactively ................................................... 116
Extracting Values from HTML Forms ......................................... 117
Try This: A Political Poll Using HTML Forms ....................................... 122
6 Standard Web Controls ................................................... 127
Data Entry .......................................................................... 128
TextBox Control for Data Entry ............................................... 129
CheckBox and CheckBox List Controls ....................................... 135
Try This: CheckBox Web Control ................................................... 136
Try This: Creating CheckBoxList Web Control ...................................... 140
RadioButton and RadioButtonList Controls ................................... 144
Try This: Making Mutually Exclusive Selections .................................... 146
DropDownList and ListBox Controls .......................................... 148
Data Display ........................................................................ 154
TextBox (as a Data Display) ................................................... 155
Label and Literal Web Controls ............................................... 155
Panel ......................................................................... 157
Display All at Once ........................................................... 158
Triggers, Links, and Images ......................................................... 161
Buttons ....................................................................... 161
HyperLink .................................................................... 161
Image ......................................................................... 162
Trigger, Image, and Link Application .......................................... 163
Try This: Events and Triggers ....................................................... 164
ImageMap .................................................................... 166
Try This: Using the ImageMap ...................................................... 166
7 CSS for ASP.NET 3.5 Page Formatting ................................... 173
Styling with the Properties Window ................................................. 174
Styling Web Controls ......................................................... 174
Styling Web Forms ............................................................ 176
Designing with CSS ................................................................. 179
Visual Studio 2008 CSS Design Tool .......................................... 180
Try This: Designing CSS with Visual Studio 2008 ................................... 180
CSS External Style Sheets ........................................................... 185
The Color Scheme ............................................................ 186
C# Style ............................................................................ 194
8 Control Events and Event Handlers ...................................... 197
Automatic Events ................................................................... 198
Double-Click in the Design Mode ............................................. 198
Selecting Events in the Properties Window .................................... 199
IntelliSense and Events ........................................................ 200
xii ASP.NET 3.5: A Beginner’s Guide
Events and Event Handling .......................................................... 201
Try This: Using Multiple Web Controls .............................................. 202
Button Command Events ............................................................ 205
Changing the Event Handler Parameters ....................................... 205
One Event Handler, Many Controls ........................................... 206
Using Command Events for Tasks ............................................. 209
Try This: Using the CommandArgument ............................................ 210
9 Validation Controls ........................................................ 215
Where to Validate ................................................................... 216
Client-Side Validation ......................................................... 216
Types of Validation ........................................................... 217
RequiredFieldValidator .............................................................. 218
Visual Validation .............................................................. 218
Try This: Creating a Page with Required Validation ................................... 219
RangeValidator ..................................................................... 223
Try This: Validating on a Range of Values ........................................... 224
RegularExpressionValidator ......................................................... 227
Try This: Working with Regular Expressions ........................................ 227
CompareValidator ................................................................... 232
Try This: Comparing Fields for Validation ........................................... 232
CustomValidator .................................................................... 236
Try This: Customizing Validation ................................................... 237
Summarizing Validation Errors ...................................................... 241
10 Rich Data Controls ........................................................ 247
Calendar Web Control ............................................................... 248
Easy Style .................................................................... 249
Changing Selections .......................................................... 250
Try This: Responding to Selections .................................................. 250
Soccer Mom’s Calendar ....................................................... 253
Try This: Using the DayOfWeek Property ........................................... 254
AdRotator Web Server Control ...................................................... 257
XML and the Seven Tags ...................................................... 257
Try This: Basic AdRotator ........................................................... 258
PART III ASP.NET and Databases
11 A SQL Primer ............................................................. 269
Data Types in SQL and C#: Essential Details and Differences ........................ 270
Writing SQL Commands ...................................................... 272
Conventions Used in Writing SQL Commands ................................ 272
Contents xiii
CREATE: The SQL Command You Will Rarely Use ................................. 273
Planning a Table .............................................................. 274
Specifying SQL Data Types ................................................... 275
Primary Key and Unique Values ............................................... 275
Coding Table Creation ........................................................ 276
Adding Data to a Table with INSERT ................................................ 278
Looking into a Table with SELECT and FROM ..................................... 279
The WHERE Filter ............................................................ 279
Sorting Output with ORDER BY .............................................. 280
Changing Data with UPDATE ....................................................... 281
Multiple Record UPDATE .................................................... 282
Making Multiple Partial Changes .............................................. 283
ADD a New Field with ALTER ..................................................... 283
When You Must DELETE a Record ................................................. 284
Single DELETE ............................................................... 284
Clear a Whole Table with DELETE ........................................... 285
Removing a Table Using DROP ..................................................... 285
12 ADO.NET: Hello Database ................................................ 287
Creating a Database ................................................................. 288
Working with the Server Explorer ............................................. 289
Adding a Table to the Database ................................................ 290
Adding Data to a Table ........................................................ 292
Making a Table Query ......................................................... 294
Creating Web Sites for Database Use ................................................ 296
First Use the SqlClient Namespace ............................................ 296
Building a Web Site for Database Retrieval .................................... 299
Entering Data from a Web Site ...................................................... 301
Automatic Data Entry ......................................................... 301
Adding Data with INSERT .................................................... 303
Programming INSERT in C# .................................................. 306
Dynamic Data Entry .......................................................... 310
Changing Data with UPDATE ................................................. 313
Removing Data with DELETE ................................................ 317
Adding More Filters to the DELETE Operation with AND ..................... 320
Using Good Practices ............................................................... 324
13 Data Binding .............................................................. 329
What Is Data Binding? .............................................................. 330
Basic Binding ....................................................................... 331
Try This: Adding Data Binding ...................................................... 331
xiv ASP.NET 3.5: A Beginner’s Guide
Repeater ............................................................................ 334
Basic Templates ............................................................... 335
Try This: Repeating a Table ......................................................... 335
Alternating Templates ......................................................... 340
DataList ............................................................................ 342
Try This: Add a DataList ............................................................ 343
DataGrid Made Easy ................................................................ 346
ListView ............................................................................ 348
14 LINQ ...................................................................... 353
LINQ Basics ........................................................................ 354
ASP.NET 3.5 and LINQ ............................................................. 355
Try This: Getting Started with LINQ ................................................ 355
C# Uses LINQ ...................................................................... 366
Array Data Source ............................................................ 366
The IEnumerable<T> Interface ................................................ 366
A Simple C# Example ......................................................... 366
Try This: Working with the IEnumerable<T> Interface .............................. 366
15 Files ........................................................................ 371
Writing Files ........................................................................ 372
Using StreamWriter ........................................................... 372
Try This: Write a Text File .......................................................... 373
Using MapPath ............................................................... 375
Appending Files .............................................................. 378
Try This: Adding Content to an Existing File ........................................ 378
Reading Text Files .................................................................. 381
Using StreamReader .......................................................... 381
The ReadLine Method ........................................................ 381
Viewing Directories and Files ....................................................... 384
Locating and Displaying Directories ........................................... 384
Locating and Displaying Files ................................................. 384
16 Security .................................................................... 389
Putting Web.config to Work ......................................................... 390
Using Multiple Web.config Files .............................................. 391
Developing a Forms Authentication Configuration ............................. 393
First Login Application ........................................................ 393
Membership Security ............................................................... 398
Try This: A Member Based Security Web Site ....................................... 399
Remote Registration ................................................................ 411
How to Get to the Registration Page ........................................... 411
Try This: A Simple Login and Registration Application .............................. 413
Adding Roles ................................................................. 416