Anda di halaman 1dari 14

PDSA

Fundamentals of
ASP.NET

design architecture security training best practices:pdsa.com

Fundamentals of
ASP.NET
By: Paul D. Sheriff

With examples in C# and VB.NET

4th Edition December 2012


Published By: PDSA, Inc.
ISBN: 978-0-9816946-2-7

Copyright 2006-2012, PDSA, Inc.


All Rights Reserved Worldwide

Fundamentals of ASP.NET

Courseware Design: Paul D. Sheriff


Technical Editors:

Paul D. Sheriff
Bruce Jones
Brian Le
Noel Eom

Every effort has been made to supply complete and accurate information.
However, PDSA, Inc. assumes no responsibility for its use, nor for any
infringement of the intellectual property rights of third parties which would
result from such use.
2006-2012 PDSA, Inc.
All rights reserved worldwide. No part of this publication may be stored in
a retrieval system, transmitted, or reproduced in any way, including but not
limited to photocopy, photograph, magnetic or other record, without the
prior agreement and written permission of the publisher.
Printed in the United States of America

Fundamentals of ASP.NET
Copyright 2006-2012 by PDSA, Inc.
All rights reserved. Reproduction is strictly prohibited.

Table of Contents

Table of Contents
Table of Contents.................................................................................................3
Acknowledgements ..............................................................................................8
Introduction ..........................................................................................................9
Benefits of Reading this Book...................................................................9
Prerequisites ............................................................................................9
Assumptions.............................................................................................9
Technical Support ..............................................................................................10
About Paul D. Sheriff .........................................................................................10
Chapter 1 .....................................................................................................................1-1
Introduction to Internet Programming ...........................................................................1-1
What is the Internet? .........................................................................................1-1
Internet Service Provider (ISP) ..............................................................1-2
Putting it all Together.............................................................................1-2
HTML Basics ....................................................................................................1-2
Hyperlink ...............................................................................................1-3
Line Break .............................................................................................1-4
Paragraph Break ...................................................................................1-4
Bulleted List...........................................................................................1-5
Numbered List .......................................................................................1-5
DropDown and List Boxes .....................................................................1-5
Table .....................................................................................................1-6
Images ..................................................................................................1-7
Input Types ...........................................................................................1-8
Websites Created Using HTML .......................................................................1-11
Websites Created Using ASP .........................................................................1-12
Websites Created Using ASP.NET .................................................................1-13
Two Modes for Creating ASP.NET Applications ..................................1-14
Create a New ASP.NET Application ....................................................1-14
Creating the Login Form ......................................................................1-15
Run the Login Form .............................................................................1-18
Adding Code to the Button ..................................................................1-18
What happens when the Button is clicked? .........................................1-20
Summary ........................................................................................................1-22
Chapter 2 .....................................................................................................................2-1
Introduction to ASP.NET ..............................................................................................2-1

Fundamentals of ASP.NET
Copyright 2006-2012 by PDSA, Inc.
All rights reserved. Reproduction is strictly prohibited.

Fundamentals of ASP.NET
Overview of ASP.NET .......................................................................................2-1
Web Pages............................................................................................2-2
The Purpose for Web Pages .................................................................2-3
XML Web Services ................................................................................2-3
Components of a Web Service ..............................................................2-4
HTML Controls ..................................................................................................2-5
Web Page Server Controls ...............................................................................2-6
Validation Controls ............................................................................................2-9
AJAX...............................................................................................................2-10
How Web Pages Work ....................................................................................2-11
IIS Objects ......................................................................................................2-12
Response Object .................................................................................2-12
Request Object....................................................................................2-12
Session Object ....................................................................................2-13
Application Object ...............................................................................2-14
Server Object ......................................................................................2-14
Global.asax .........................................................................................2-14
User Controls ..................................................................................................2-17
Summary ........................................................................................................2-18
Chapter 3 .....................................................................................................................3-1
Validation Controls .......................................................................................................3-1
Introducing Validation Controls .........................................................................3-1
Using the RequiredFieldValidator Control to Force Data Entry ..........................3-2
Summarizing with the ValidationSummary Control ............................................3-7
Checking a Range using the RangeValidator Control .......................................3-8
Modify the Page_Load Event...............................................................3-10
Validating Expressions Using the Regular Expression Validator .....................3-12
Using the RegularExpressionValidator Control ....................................3-13
You Try It.............................................................................................3-14
Creating Your Own Validation with the CustomValidator Control ....................3-16
You Try It.............................................................................................3-16
JavaScript ...........................................................................................3-18
VBScript ..............................................................................................3-19
Comparing Values with the CompareValidator Control ...................................3-20
You Try It.............................................................................................3-20
Programming the Save Button ........................................................................3-21
Summary ........................................................................................................3-23

Fundamentals of ASP.NET
Copyright 2006-2012 by PDSA, Inc.
All rights reserved. Reproduction is strictly prohibited.

Table of Contents
Chapter 4 .....................................................................................................................4-1
The GridView Control ...................................................................................................4-1
Data Binding .....................................................................................................4-1
Binding Data to a GridView ...............................................................................4-2
Create the Sample Page .......................................................................4-2
Configure the Data ................................................................................4-3
Create New Connection ........................................................................4-5
Select Table to Bind ..............................................................................4-7
Try It Out ...............................................................................................4-9
Using a Parameterized Query ...........................................................................4-9
Add a WHERE Clause.........................................................................4-10
Paging ............................................................................................................4-13
GridView Paging Properties.................................................................4-13
Sorting ............................................................................................................4-15
Displaying a Drop-Down List when Editing ......................................................4-16
Adding Data in the GridView ...........................................................................4-17
Adding in the GridView ........................................................................4-17
The RowUpdating Event......................................................................4-20
Create an AppConfig Class .................................................................4-21
The DataAdd Method ..........................................................................4-21
Cancelling the Insert ............................................................................4-24
Adding Data using a DetailsView Control ........................................................4-24
Setup the GridView Control .................................................................4-25
Setup the DetailsView Control .............................................................4-26
Making the DetailsView work with the GridView ..................................4-27
Inserting Items with the DetailsView ....................................................4-28
Summary ........................................................................................................4-31
Chapter 5 .....................................................................................................................5-1
The Repeater Control ...................................................................................................5-1
How Does the Repeater Control Work? ............................................................5-2
Setting the DataSource Property ...........................................................5-2
Displaying the Data ...............................................................................5-2
Binding Data in Templates ....................................................................5-4
Creating the Repeater Example ........................................................................5-4
Creating a Bulleted List Page ................................................................5-6
Add the Header and Footer Templates ..................................................5-7
Create the Product List .....................................................................................5-8
Create the ItemTemplate Element .......................................................5-10

Fundamentals of ASP.NET
Copyright 2006-2012 by PDSA, Inc.
All rights reserved. Reproduction is strictly prohibited.

Fundamentals of ASP.NET
Add the AlternatingItem Template .......................................................5-12
Adding the ItemCommand Event Procedure........................................5-12
Summary ........................................................................................................5-15
Chapter 6 .....................................................................................................................6-1
The DataList Control.....................................................................................................6-1
The DataList Templates ........................................................................6-2
Creating the Sample Page ................................................................................6-3
Configuring the ItemTemplate ...........................................................................6-6
Using the Designer ................................................................................6-7
Place the Image Control ........................................................................6-7
Add the Other Label Controls ................................................................6-8
Copy in the Images ...............................................................................6-9
Try it Out .............................................................................................6-10
Making the DataList Look Better .....................................................................6-11
Setup the Editing Template .............................................................................6-15
Setup Custom Binding Expressions.....................................................6-17
Getting Into Edit Mode .........................................................................6-18
Cancelling the Edit Mode .....................................................................6-19
Updating the Data ...........................................................................................6-20
Test the Page ......................................................................................6-23
Summary ........................................................................................................6-24
Chapter 7 .....................................................................................................................7-1
Other ASP.NET Controls ..............................................................................................7-1
More ASP.NET Controls ...................................................................................7-1
The CheckBoxList and RadioButtonList Controls ..............................................7-2
The Calendar Control ........................................................................................7-5
Initializing the Calendar .........................................................................7-8
The AdRotator Control ......................................................................................7-9
Filtering the AdRotator Control ............................................................7-12
The Literal Control ..........................................................................................7-13
ValidateRequest Issue.........................................................................7-15
The Placeholder Control .................................................................................7-16
Summary ........................................................................................................7-19
Chapter 8 .....................................................................................................................8-1
State Management .......................................................................................................8-1
State Management Techniques ........................................................................8-1
Application Object .................................................................................8-2

Fundamentals of ASP.NET
Copyright 2006-2012 by PDSA, Inc.
All rights reserved. Reproduction is strictly prohibited.

Table of Contents
Cache Object ........................................................................................8-2
Session Object ......................................................................................8-2
Cookies .................................................................................................8-2
Hidden Input Fields ...............................................................................8-3
Embedded URL Parameters (Query String) ..........................................8-3
ViewState Object ...................................................................................8-4
SQL Server ...........................................................................................8-4
ASP.NET State Server ..........................................................................8-4
Web Forms Manage the View State ......................................................8-4
Automatic ViewState .........................................................................................8-5
Using the Application Object .............................................................................8-6
Wrap up the Application Object .............................................................8-9
Using the Cache Object ..................................................................................8-10
Wrap up the Cache Object ..................................................................8-14
Using the Session Object ................................................................................8-16
Session Longevity ...............................................................................8-16
Properties of the Session Object .........................................................8-17
Creating Session Variables .................................................................8-18
Issues with Session Objects ................................................................8-20
Turning Off Cookies Page By Page .....................................................8-21
Wrapping up Session ..........................................................................8-21
Using Cookies.................................................................................................8-23
Memory Cookies .................................................................................8-24
Permanent Cookies .............................................................................8-26
Issues with Cookies .............................................................................8-28
Using the ViewState Property .........................................................................8-28
ViewState Issues .................................................................................8-31
Passing Values on the URL Line.....................................................................8-32
Cookieless Sessions .......................................................................................8-35
ASP.NET State Service ..................................................................................8-36
Issues with the ASP.NET State Service ..............................................8-37
SQL Server State Management ......................................................................8-38
Issues with Automatic SQL Server State Management........................8-39
Summary ........................................................................................................8-40
Chapter 9 .....................................................................................................................9-1
Creating and Using User Controls ................................................................................9-1
Why Create User Controls? ..............................................................................9-1
Creating an Address Entry User Control ...........................................................9-2

Fundamentals of ASP.NET
Copyright 2006-2012 by PDSA, Inc.
All rights reserved. Reproduction is strictly prohibited.

Fundamentals of ASP.NET
Exploring the Code for the Address Control...........................................9-6
InitControlVisibility Method ....................................................................9-7
LoadDropDowns Methods .....................................................................9-8
ShowDetails Method ...........................................................................9-10
GetUserData Method...........................................................................9-13
SetUserData Method ...........................................................................9-15
PDSAAddress Class ...........................................................................9-17
ucAddressDisplay User Control ...........................................................9-17
Creating a Credit Card User Control ...............................................................9-17
Summary ........................................................................................................9-19
Chapter 10 .................................................................................................................10-1
Deploying an ASP.NET Application ............................................................................10-1
Deploying ASP.NET Applications ....................................................................10-1
Using XCOPY Deployment .............................................................................10-2
Maybe Not a Good Choice ..................................................................10-5
Publishing a Site .............................................................................................10-5
FTP publishing ....................................................................................10-6
HTTP Publishing .................................................................................10-6
File System .........................................................................................10-7
Windows Installer Deployment ........................................................................10-7
Summary ........................................................................................................10-8

Acknowledgements
We would like to thank a few people who helped us get this book put
together. There have been many people who have contributed to this and
they deserve some of the credit for making this a reality.

The staff at PDSA, Inc. for helping review and tech edit most of
these chapters.

Ken Getz, as usual, was a huge help in this book.

The input of several of our fellow instructors including Mike Groh,


Tom Howe, Bruce Jones, and Scott Barker.

Brian Le and Noel Eom for updating to later versions of Visual


Studio.
Fundamentals of ASP.NET
Copyright 2006-2012 by PDSA, Inc.
All rights reserved. Reproduction is strictly prohibited.

Introduction

Introduction
This book is designed for anyone who wants to learn how to use ASP.NET
to build web applications. Throughout this book you will be introduced to
the concepts of the web site creation using the Microsoft .NET
Framework. Emphasis will be on good programming standards and
practices. In this book you will learn the following:
How the Internet Works
HTML versus ASP.NET
Using the Validation Controls
Effective use of the GridView Control
How to use the Repeater and DataList Controls
Managing State
The proper use of User Controls

Benefits of Reading this Book


The major benefit you will derive from reading this book is that you will be
shown how to use ASP.NET to create business application-type web sites.
After reading this book you will be able to build web sites for business
applications.

Prerequisites
This book is designed for programmers who already have some
experience with VB.NET or C#. You should also be familiar with relational
database concepts, and have access to the Northwind sample database
that comes with SQL Server. You must be familiar with Windows XP,
2003, Vista, Windows 7 or later, and have access to one of these
operating systems to effectively use this book.

Assumptions
You will need several tools at your disposal so you can try out the many
exercises contained in this book. Below is a list of the tools that you
should have on your computer:
Microsoft SQL Server 2005, 2008 or later
The Northwind Sample database
Fundamentals of ASP.NET
Copyright 2006-2012 by PDSA, Inc.
All rights reserved. Reproduction is strictly prohibited.

Fundamentals of ASP.NET
Microsoft Windows XP, 2003, Vista, Windows 7
Microsoft .NET CLR Framework SDK
Microsoft Visual Studio 2010 or later

Technical Support
Because of the complexity of .NET software development, PDSA, Inc.
does not provide free technical support related to the use of the material in
this e-book. Technical support is available for problems with installation of
the samples that are a part of this e-book but only through email, please
do not call.
TIP:

It is strongly recommended that you read


this e-book thoroughly before emailing us
for technical support.

For technical support please send an email to Books@pdsa.com.

About Paul D. Sheriff


Paul is the CEO and President of PDSA, Inc., a Microsoft Partner in
Southern California. PDSA, Inc. has provided customers with high quality
custom software, training, and architecture services since 1991. Paul also
serves as the Microsoft Regional Director for Southern California.
Paul has over twenty-five years of experience architecting information
systems and his expertise is in much demand from companies such as
Kawasaki, Sunkist, Microsoft, Rockwell, American Express, J.D. Powers &
Associates and many others. Paul has been helping companies architect
.NET applications and create application frameworks.
Over the years Paul has written over 233 articles for many different
publications. Paul has authored over 14 books on topics ranging from
Visual Basic, SQL Server to .NET technologies. Paul speaks at many
different conferences around the world including Microsoft Tech-Ed,
10

Fundamentals of ASP.NET
Copyright 2006-2012 by PDSA, Inc.
All rights reserved. Reproduction is strictly prohibited.

About Paul D. Sheriff


VSLive, DevConnections, DevIntersections, MSDN Events and local
Microsoft events.
PDSA, Inc. is a full-service consulting company offering custom software
development, architecture services, consulting, mentoring, training and
more. Contact PDSA at (714) 734-9792.
Blog: http://weblogs.asp.net/psheriff
Twitter: http://www.twitter.com/pdsainc
Facebook: http://www.facebook.com/pdsapaulsheriff
E-mail: Psheriff@pdsa.com
Inner Circle: http://www.PaulSheriffInnerCircle.com

Fundamentals of ASP.NET
Copyright 2006-2012 by PDSA, Inc.
All rights reserved. Reproduction is strictly prohibited.

11

Fundamentals of ASP.NET

THIS PAGE INTENTIONALLY LEFT BLANK

12

Fundamentals of ASP.NET
Copyright 2006-2012 by PDSA, Inc.
All rights reserved. Reproduction is strictly prohibited.

Anda mungkin juga menyukai