Search for notes by fellow students, in your own course and all over the country.
Browse our notes for titles which look like what you need, you can preview any of the notes via a sample of the contents. After you're happy these are the notes you're after simply pop them into your shopping cart.
Document Preview
Extracts from the notes are below, to see the PDF you'll receive please use the links above
Summary of Contents
Preface
...
Introducing ASP
...
NET Platform
...
ASP
...
33
3
...
51
4
...
NET Web Pages
...
Building Web Applications
...
Using the Validation Controls
...
Database Design and Development
...
Speaking SQL
...
ADO
...
331
10
...
401
11
...
427
12
...
469
13
...
527
14
...
571
A
...
611
Index
...
NET 2
...
NET 2
...
Ltd
...
No part of this book may be reproduced, stored in a retrieval system or transmitted
in any form or by any means, without the prior written permission of the publisher, except in the
case of brief quotations embodied in critical articles or reviews
...
However, the information contained in this book is sold without warranty, either express or implied
...
Ltd
...
Trademark Notice
Rather than indicating every occurrence of a trademarked name as such, this book uses the names
only in an editorial fashion and to the benefit of the trademark owner with no intention of infringement of the trademark
...
Ltd
...
Web: www
...
com
Email: business@sitepoint
...
He holds a Bachelor’s Degree from San Diego State University and a Master of Science
in Instructional Technology from National University in San Diego
...
More recently, he’s worked as a wireless software engineer
developing
...
Currently, Zak holds a programming position with ADCS Inc
...
NET applications
...
He also lectures on various
technologies and tools, including Dreamweaver and ASP
...
Cristian Darie is a software engineer with experience in a wide range of modern technologies, and the author of numerous technical books, including the popular Beginning ECommerce series
...
From there, Cristian moved on to many other similar achievements, and is
now studying distributed application architectures for his PhD
...
Cristian can be contacted through his personal
web site at http://www
...
ro
...
He also writes for SitePoint's
...
1
Sara Smith is an ASP
...
She has been working with the
...
Sara just relocated to Belgium from the US with her family
...
He spent much of this time trying to convince Visual Basic developers that one of these days they would have to learn object oriented programming
...
sitepoint
...
About the Technical Director
As Technical Director for SitePoint, Kevin Yank oversees all of its technical publications—books, articles, newsletters, and blogs
...
Kevin lives in Melbourne, Australia, and enjoys performing improvised comedy
theatre and flying light aircraft
...
Visit http://www
...
com/ to access our books, newsletters, articles,
and community forums
...
lookwhostoxic
...
—Zak Ruvalcaba
To my family and friends
...
xi
1
...
NET and the
...
1
What is ASP
...
2
Installing the Required Software
...
6
Installing the
...
9
Configuring the Web Server
...
22
Installing SQL Server Management Studio Express
...
25
Writing your First ASP
...
26
Getting Help
...
32
2
...
NET Basics
...
NET Page Structure
...
36
Code Declaration Blocks
...
39
ASP
...
40
Server-side Comments
...
42
View State
...
47
ASP
...
48
Visual Basic
...
49
Summary
...
VB and C# Programming Basics
...
51
Control Events and Subroutines
...
56
Variables and Variable Declaration
...
62
Functions
...
68
Conditional Logic
...
72
Build Your Own ASP
...
0 Web Site Using C# & VB
Object Oriented Programming Concepts
...
77
Properties
...
81
Classes
...
81
Scope
...
83
Understanding Inheritance
...
NET
...
86
Using Code-behind Files
...
91
4
...
NET Web Pages
...
94
HTML Server Controls
...
97
Web Server Controls
...
103
List Controls
...
112
Web User Controls
...
126
Master Pages
...
135
Types of Styles and Style Sheets
...
141
5
...
143
Introducing the Dorknozzle Project
...
147
Meeting the Features
...
156
Using Visual Web Developer’s Built-in Web Server
...
160
Core Web Application Features
...
config
...
asax
...
173
Working with User Sessions
...
182
iv
Using Cookies
...
Preparing the Sitemap
...
Building the Master Page
...
Extending Dorknozzle
...
Debugging with Visual Web Developer
...
Custom Errors
...
Summary
...
Using the Validation Controls
...
NET Validation Controls
...
223
Using Validation Controls
...
230
CompareValidator
...
233
ValidationSummary
...
236
CustomValidator
...
242
Updating Dorknozzle
...
250
7
...
251
What is a Database?
...
254
Creating a New Database Using Visual Web Developer
...
256
Creating Database Tables
...
262
Column Properties
...
265
Creating the Employees Table
...
271
Populating the Data Tables
...
276
v
Build Your Own ASP
...
0 Web Site Using C# & VB
Foreign Keys
...
Implementing Relationships in the Dorknozzle Database
...
Summary
...
Speaking SQL
...
294
Using the SELECT Statement
...
299
Selecting Unique Data with DISTINCT
...
302
Selecting Ranges of Values with BETWEEN
...
304
Using the IN Operator
...
306
Limiting the Number of Results with TOP
...
307
Subqueries
...
309
Expressions and Operators
...
313
Arithmetic Functions
...
315
Date and Time Functions
...
318
The COUNT Function
...
319
Filtering Groups Using HAVING
...
322
Updating Existing Data
...
323
The UPDATE Statement
...
325
Stored Procedures
...
330
9
...
NET
...
NET
...
333
Defining the Database Connection
...
336
vi
Executing the Command
...
Reading the Data
...
Bulletproofing Data Access Code
...
Creating the Dorknozzle Employee Directory
...
Inserting Records
...
Deleting Records
...
Summary
...
Displaying Content Using Data Lists
...
402
Handling DataList Events
...
413
DataList and Visual Web Developer
...
424
Summary
...
Managing Content Using Grid View and Details View
...
428
Customizing the GridView Columns
...
436
Selecting Grid Records
...
445
Styling the DetailsView
...
452
Entering Edit Mode
...
459
Updating DetailsView Records
...
468
12
...
469
Using Data Source Controls
...
472
Binding the DetailsView to a SqlDataSource
...
489
More on SqlDataSource
...
494
What is a Data Set Made From?
...
NET 2
...
Implementing Paging
...
Implementing Sorting
...
Updating a Database from a Modified DataSet
...
498
504
506
509
520
521
526
13
...
527
Basic Security Guidelines
...
NET 2
...
530
Working with Forms Authentication
...
NET 2
...
544
Creating the Membership Data Structures
...
547
Using the ASP
...
552
Creating Users and Roles
...
556
Securing your Web Application
...
NET Login Controls
...
569
14
...
571
Writing and Reading Text Files
...
573
Writing Content to a Text File
...
580
Accessing Directories and Directory Information
...
586
Uploading Files
...
NET
...
595
Sending a Test Email
...
601
Summary
...
Web Control Reference
...
611
Properties
...
612
Standard Web Controls
...
613
BulletedList
...
Calendar
...
CheckBoxList
...
619
FileUpload
...
620
HyperLink
...
621
ImageButton
...
622
Label
...
623
ListBox
...
624
MultiView
...
625
PlaceHolder
...
625
RadioButtonList
...
627
Xml
...
628
CompareValidator
...
629
RangeValidator
...
631
RequiredFieldValidator
...
633
Navigation Web Controls
...
634
Menu
...
640
HTML Server Controls
...
644
HtmlButton Control
...
645
HtmlGeneric Control
...
647
HtmlInputButton Control
...
648
HtmlInputFile Control
...
NET 2
...
HtmlInputImage Control
...
HtmlInputText Control
...
HtmlTable Control
...
HtmlTableRow Control
...
650
651
652
653
653
655
656
657
658
Index
...
There’s nothing like the feeling you have after
you place your first dynamic web site online, and see your little toy in action
while other people are actually using it!
Web development with ASP
...
If you’ve never created
a dynamic web site before, I’m sure you’ll fall in love with this area of web development
...
ASP
...
Though it can take some time to learn, ASP
...
Whether you want to create simple web forms, or feature-rich
shopping carts, or even complex enterprise applications, ASP
...
All the tools you’ll need to get up and running are immediately available
and easy to install, and require very little initial configuration
...
NET,
teaching you the foundations step by step
...
To
demonstrate some of the more complex functionality, and to put the theory into
a cohesive, realistic context, we’ll develop a project through the course of this
book
...
NET in action, and to understand through
practice exactly how
...
We hope you’ll find reading this book an enjoyable experience that will significantly help you with your future web development projects!
Who Should Read this Book?
This book is aimed at beginner, intermediate, and advanced web designers looking
to make the leap into server-side programming with ASP
...
We expect that
you’ll already feel comfortable with HTML and a little CSS, as very little explanation of these topics is provided here
...
NET and the
...
NET pages, install and run SQL Server 2005, create
Preface
database tables, and work with advanced, dynamic ASP
...
All examples provided in the book are written in both Visual Basic and C#, the
two most popular languages for creating ASP
...
The examples start
at beginners’ level and proceed to more advanced levels
...
Experience with other programming or scripting languages (such as JavaScript) will certainly grease the wheels,
though, and should enable you to grasp fundamental programming concepts more
quickly
...
Read them from beginning to end
to gain a complete understanding of the subject, or skip around if you feel you
need a refresher on a particular topic
...
NET
Before you can start building your database-driven web presence, you must
ensure that you have the right tools for the job
...
NET Framework
...
Next,
we’ll walk through the installation of the Microsoft database solution: SQL
Server 2005
...
NET page to make sure that
everything’s running and properly configured
...
NET Basics
In this chapter, you’ll create your first useful ASP
...
We’ll explore
all of the components that make up a typical ASP
...
Then, we’ll walk through the process of deployment, focusing specifically on allowing the user to view the processing of a
simple ASP
...
Chapter 3: VB and C# Programming Basics
In this chapter, we’ll look at two of the programming languages that are used
to create ASP
...
You’ll learn about the syntax of the
two languages as we explore the concepts of variables, data types, conditionals,
loops, arrays, functions, and more
...
xii
Chapter 4: Constructing ASP
...
NET equivalent of web pages but, as we’ll see, the
process of building ASP
...
NET is bundled with hundreds of controls—including
HTML controls, web controls, and so on—that are designed for easy deployment within your applications
...
You’ll also learn about
master pages, which are a very exciting new feature of ASP
...
0
...
So it’s no surprise
that when we build web applications, we must consider more aspects than
when we build individual web forms
...
Next, we configure your web application, learn how to use the application state, user sessions, and cookies, explore
the process for debugging errors in your project, and more
...
With validation controls, Microsoft basically eliminated the headache of fumbling through, and configuring, tired, reused client-side validation scripts
...
Then, we’ll learn how
to perform more advanced validation using regular expressions and custom
validators
...
NET
...
Also in this chapter, we’ll begin to build the database for the
Dorknozzle intranet project
...
After a gentle introduction to the basic concepts
of SQL, which will teach you how to write SELECT, INSERT, UPDATE, and DELETE queries, we’ll move on to more advanced topics such as expressions,
conditions, and joins
...
xiii
Preface
Chapter 9: ADO
...
NET—the technology
that facilitates communication between your web application and the database
server
...
We’ll then help you begin the transition from working with
static applications to those that are database-driven
...
NET further, this chapter shows you how to utilize the DataList
control provided within the
...
DataLists play a crucial role
in simplifying the presentation of information with ASP
...
In learning
how to present database data within your applications in a cleaner and more
legible format, you’ll gain an understanding of the concepts of data binding
at a high level
...
NET: GridView and DetailsView
...
NET
1
...
DetailsView completes the picture by
offering us the functionality needed to display the details of a single grid
item
...
We’ll start by looking at direct data access
using ADO
...
We’ll then compare this approach
with that of using data sets to access data in a disconnected fashion
...
Chapter 13: Security and User Authentication
This chapter will show you how to secure your web applications with
ASP
...
We’ll discuss the various security models available, including IIS,
Forms, Windows, and Passport, and explore the roles that the Web
...
This chapter will also introduce you to the new
ASP
...
0 membership model, and the new ASP
...
0 login controls
...
Next, the chapter will show you how
to work with file streams to create text files, write to text files, and read from
text files stored on your web server
...
NET
...
NET
...
sitepoint
...
The Code Archive
As you progress through this book, you’ll note a number of references to the code
archive
...
You can get it from the book’s web site
...
Each folder contains
CS and VB subfolders, which contain the C# and VB versions of all the examples
for that chapter, respectively
...
Updates and Errata
No book is perfect, and we expect that watchful readers will be able to spot at
least one or two mistakes before the end of this one
...
NET and the
various web standards that apply
...
sitepoint
...
php
xv
Preface
The SitePoint Forums
If you’d like to communicate with us or anyone else on the SitePoint publishing
team about this book, you should join SitePoint’s online community
...
NET
forum, in particular, can offer an abundance of information above and beyond
the solutions in this book
...
It’s a good way to learn new stuff, get questions answered in a hurry, and just
have fun
...
In them, you’ll read
about the latest news, product releases, trends, tips, and techniques for all aspects
of web development
...
NET articles and tips,
but if you’re interested in learning other technologies, you’ll find them especially
valuable
...
sitepoint
...
Your Feedback
If you can’t find your answer through the forums, or if you wish to contact us
for any other reason, the best place to write is books@sitepoint
...
We have
a well-manned email support system set up to track your inquiries, and if our
support staff members are unable to answer your question, they will send it
straight to us
...
Acknowledgements
First and foremost, I’d like to thank the SitePoint team for doing such a great
job in making this book possible, for being understanding as deadlines inevitably
slipped past, and for the team’s personal touch, which made it a pleasure to work
on this project
...
sitepoint
...
sitepoint
...
php?f=141
Particular thanks go to Simon Mackie, whose valuable insight and close cooperation throughout the process has tied up many loose ends and helped make this
book both readable and accessible
...
Finally, returning home, I’d like to thank my wife Jessica, whose patience, love,
and understanding throughout continue to amaze me
...
Warm thanks and
gratitude go to my parents, my girlfriend, and my close friends for constantly
being there for me
...
NET and the
...
NET is one of the most exciting web development technologies on offer
today
...
Here was a powerful platform
with lots of built-in functionality, astonishing performance levels, and one of the
best IDEs (Integrated Development Environments) around: Visual Studio
...
NET showed the way for the faster, easier,
and more disciplined development of dynamic web sites, and the results were
impressive
...
NET has grown
...
NET 2
...
Not only that, but the basic versions of all development tools, including Visual
Web Developer 2005 Express Edition and SQL Server 2005 Express Edition, are
free!
This book shows you how to use all these technologies together in order to produce
fantastic results
...
Developers who have already
worked with earlier versions of ASP
...
NET 2
...
Let’s begin!
Chapter 1: Introducing ASP
...
NET Platform
What is ASP
...
Early in 2002, Microsoft
released a new technology for Internet development called ASP
...
ASP
...
It continues to offer flexibility in terms of language
support, but rather than a range of simple scripting languages, several fully-fledged
programming languages are now at the fingertips of ASP
...
Development in ASP
...
In the next few sections, we’ll introduce you to the basics of ASP
...
We’ll
walk through the process of installing it on your web server, and step through a
simple example that demonstrates how ASP
...
But first,
let’s define what ASP
...
ASP
...
NET Framework
...
ASP
...
Most web
designers start their careers learning client-side technologies like HTML, JavaScript, and Cascading Style Sheets (CSS)
...
The client
is entirely responsible for reading the markup in those files and interpreting that
markup to display the page on the screen
...
NET, are different
...
NET page) is
interpreted by the web server
...
NET, the code in the page is
read by the server and used to generate HTML, JavaScript, and CSS that is then
sent to the browser
...
NET code occurs on the
server, it’s called a server-side technology
...
1 shows, the client only
sees the HTML, JavaScript, and CSS
...
2
What is ASP
...
1
...
web client
This is the software program that the person uses to interact to
the web application
...
web server
This is the software program located on the server
...
ASP
...
A web application is just a
fancy name for a dynamic web site
...
Many different programming technologies and supported languages have been developed to create web applications; PHP, JSP, Ruby on Rails,
3
Chapter 1: Introducing ASP
...
NET Platform
CGI, and ColdFusion are just a few of the more popular ones
...
NET lets you write
web applications using a variety of familiar programming languages
...
NET uses the Microsoft
...
The
...
Even with all the jargon explained, you’re probably still wondering what makes
ASP
...
The truth is that there are many server-side technologies
around, each of which has its own strengths and weaknesses
...
NET has
a few features that really are unique:
❑ ASP
...
The
...
NET web sites
...
❑ ASP
...
In ASP
...
In ASP
...
The code is compiled into efficient binary
files, which can be run very quickly, again and again, without the overhead
involved in re-reading the page each time
...
❑ ASP
...
NET Framework
...
NET, which saves you from
having to reinvent the wheel
...
NET allows you to separate the server-side code in your pages from the
HTML layout
...
❑ ASP
...
During the course of this book, you’ll learn how to add powerful
4
Installing the Required Software
features to your web site, and to reuse them in many places with a minimum
of effort
...
NET web applications
...
In the course of
this book, you’ll learn how to use this tool to build the examples we discuss
...
NET Framework was first available only to the Windows platform, but
thanks to projects such as Mono,1 it’s since been ported to other operating
systems
...
NET, you first need to make sure you have all the
necessary software components installed and working on your system
...
Internet Information Services (IIS) or Cassini
IIS is the web server of choice for running ASP
...
You’ll
need your copy of the Windows CD to install and configure it
...
If you’re one of those users, there’s Cassini
...
NET web sites
...
When we come to use Visual Web Developer in Chapter 5,
we’ll be making use of that product’s built-in development web server, so not
having access to IIS on your system won’t be a problem
...
Any modern browser will
do
...
NET Framework 2
...
NET Framework drives ASP
...
When
you install the
...
mono-project
...
NET and the
...
NET
...
NET Framework already, as it
installs automatically through the Windows Update service
...
NET Framework Software Development Kit (SDK)
The
...
0 Software Development Kit (SDK) is a free download that contains the necessary Web Application development tools, a debugger for error correcting, and a suite of samples and documentation
...
In this book, we’ll use the following:
Microsoft SQL Server 2005 Express Edition
This is the free, but still fully functional, version of SQL Server 2005
...
You can read more on the differences between various
SQL Server 2005 editions at the Microsoft site
...
Installing the Web Server
Installing Internet Information Services (IIS)
IIS comes with most versions of server-capable Windows operating systems—including Windows 2000 Professional, Server, and Advanced Server; Windows XP
Professional; Windows XP Media Center Edition; and Windows Server 2003—but
it’s not installed automatically in all versions, which is why it may not be present
on your computer
...
If you run this, you’ll need to rely on Cassini,
which we discuss below
...
If the shortcut isn’t visible, then it’s not installed
...
microsoft
...
asp
To see this folder, you’ll need to view the Control Panel in “classic view
...
In the Control Panel, select Add or Remove Programs
...
Choose Add/Remove Windows Components
...
3
...
2
...
NET development, but you may want to click Details… to view the extra options you
could add
...
2
...
Click Next
...
Add Administrative Tools to the Start Menu
Here’s how to add Administrative Tools to the Windows XP Start menu:
1
...
2
...
7
Chapter 1: Introducing ASP
...
NET Platform
3
...
4
...
Once IIS is installed, close the Add or Remove Programs dialog
...
If you can, IIS is installed
...
3
...
Figure 1
...
The IIS administration tool
You can close this tool for now; you’ll meet it again later
...
Although we won’t cover the configuration of IIS for external use, we will show you how to configure IIS to support
local development of ASP
...
8
Installing the
...
Go to the Cassini download page
...
Download the Cassini installer executable (cassini
...
3
...
exe and follow the steps presented by the installer, accepting
the default options
...
Double-click CassiniWebServer
...
4
...
4
...
NET Framework and the Software Development Kit
first
...
NET Framework and the SDK
To begin creating ASP
...
NET Framework and the Software Development Kit (SDK)
...
NET Framework includes
4
http://www
...
net/Projects/Cassini/Download/
9
Chapter 1: Introducing ASP
...
NET Platform
the files necessary to run and view ASP
...
Installing the
...
NET Framework is to download and install it
directly from the Web
...
To check, open the folder
C:\WINDOWS\Microsoft
...
If this folder
doesn’t exist, you definitely don’t have the
...
If it does
exist, you should find inside it at least one folder with a name like v1
...
4322
...
NET Framework
you have installed
...
NET Framework
...
NET Framework, simply follow the steps
outlined below:
1
...
NET support site5 and click the Download the
...
2
...
NET Framework Version 2
...
Remember, we’ll install
the redistributable package first, then the SDK
...
3
...
4
...
After the download is complete, doubleclick the executable to begin the installation
...
Follow the steps presented by the wizard until installation completes
...
asp
...
Go back to the ASP
...
NET
Framework link again
...
This time, click the appropriate download link under the
...
0 Software Development Kit heading
...
3
...
4
...
5
...
Before you do so, I strongly recommend that you close all other
programs to ensure the install proceeds smoothly
...
Follow the steps outlined by the
...
When asked for setup options, it’s safe to use the default values
...
A Big Download!
The
...
Configuring the Web Server
Configuring IIS
After installing the
...
NET
...
Open the command prompt by selecting Start > All Programs > Microsoft
...
0 > SDK Command Prompt
...
Type the following command to install ASP
...
0>aspnet_regiis
...
NET (2
...
50727)
...
Finished installing ASP
...
0
...
11
Chapter 1: Introducing ASP
...
NET Platform
3
...
NET is installed, close the command prompt and check again to
confirm that ASP
...
Running aspnet_regiis
...
NET may already have been installed
for you, but running aspnet_regiis
...
Also, remember that
you need to run this utility again in case you reinstall IIS
...
NET
Framework to coerce Cassini into working as it should
...
Open the command prompt by selecting Start > All Programs > Microsoft
...
0 > SDK Command Prompt
...
Enter the following command at the prompt:
C:\Program Files\…\SDK\v2
...
dll
Microsoft (R)
...
Version 2
...
All rights reserved
...
Where do I Put my Files?
IIS Recommended
From here on in, the instructions we provide will be centered around IIS, as
Cassini isn’t suitable for production environments
...
Where needed, Cassini instructions will be given, but IIS
will receive the bulk of the discussion
...
NET up and running, let’s find out where the files for
your web applications are kept on the computer
...
By default, IIS maps the C:\Inetpub\wwwroot folder of your disk
to your web site’s root directory, which is generally considered a good repository
for storing and managing your web applications
...
5, you’ll notice that the files and folders are the same (some
extra items will be listed in IIS; we’ll look at these shortly)
...
We’ll see more on this soon
...
5
...
If you’ve been developing web pages for a long time, habit may drive
you to open files directly in your browser by double-clicking on the HTML files
...
NET is a server-side language, your web server needs to
have a crack at the file before it’s sent to your browser for display
...
NET code won’t be converted into HTML
that your browser can understand
...
NET files can’t be opened
directly from the disk using Windows Explorer
...
If you try this now, IIS will open up a
default help page (although this behavior will vary depending on the settings of
13
Chapter 1: Introducing ASP
...
NET Platform
your Windows installation; for example, if you get an error instead of the default
help page, don’t worry)
...
To see this in practice, create a new file named index
...
htm
This is a simple HTML page
...
htm, as shown in Figure 1
...
Figure 1
...
Testing IIS
Experiencing an Error?
If the page doesn’t load as illustrated in Figure 1
...
You might want to double-check that you correctly followed the
steps for installing it, and re-check the IIS configuration procedure
...
com
...
14
Configuring the Web Server
This localhost name is equivalent to the so-called loopback IP address, 127
...
0
...
0
...
1/index
...
If you know them, you can also use the name or IP address of your
machine to the same end
...
This occurs because
you’re no longer using your local authentication, which is implicit with localhost
...
NET installed, let’s look at
how you can start, stop, and restart IIS if the need arises
...
Also, some
external programs may stop IIS upon launch because of potential security
vulnerabilities, so you’ll need to start it again yourself
...
Alternatively, after selecting Default Web Site, you can use the Stop, Pause,
and Play icons from the toolbar
...
For instance,
imagine your company has a web server that serves documents from C:\Inetpub\wwwroot
...
example
...
If you create a subfolder of wwwroot, named about, files in that
directory can be accessed via http://www
...
com/about/
...
If, for instance, you were developing another
web application, you could store the files for it in C:\dev\OrderSystem
...
This new site would then be accessible through the URL
http://www
...
com/order/
...
By default, a virtual directory, called IISHelp, is preconfigured in IIS; it maps to
c:\windows\help\iishelp
...
7 that IISHelp contains
15
Chapter 1: Introducing ASP
...
NET Platform
subdirectories called common and iis—these are physical folders inside c:\windows\help\iishelp
...
7
...
2
...
htm file you created earlier into your newly created Learning
folder
...
In the Internet Information Services management console, right-click Default
Web Site and select New > Virtual Directory
...
Click Next
...
You need to choose an alias for your virtual directory: enter Learning, then
click Next
...
16
First, you need to create on your disk a folder to which your virtual directory
will be mapped
...
We’ll use this folder, C:\WebDocs\Learning, for various exercises in
this book
...
Click Next
...
In the next screen, you can select permissions settings for your directory
...
Click Next
...
Click Finish
...
8 illustrates
...
8
...
Load this link by entering http://localhost/Learning/index
...
If everything went well, you should see your
little HTML page load, as has the one in Figure 1
...
Figure 1
...
Testing your new virtual directory
Note that by loading the page through the HTTP protocol, your request goes
through IIS
...
htm is a simple HTML page that doesn’t need any
server-side processing, you can also load it directly from the disk
...
NET and the
...
NET scripts you’ll see through the rest of this
book
...
8
...
As index
...
To see and edit the default document names for a virtual directory (or any directory, for that matter), you can right-click the directory’s name in the IIS management console, click Properties, and select the Documents tab
...
10
...
10
...
htm or
default
...
If there is no index page, IIS assumes we want to see the contents
of the requested location
...
You’ll find that option in the
Directory tab of the Properties window
...
Allowing visitors to freely see and access all the files and directories that make
up your web page is not only a little messy and unprofessional, but also increases the potential for security issues (you don’t want any hackers to stick
their nose into your code, do you?)
...
htm
isn’t there, ready to be served to the visitor, a message reading “Directory
Listing Denied” will be served instead
...
The
Properties dialog that we’ve just used lets us configure various useful properties,
including:
Virtual Directory
This option allows you to configure directory-level
properties, including path information, the virtual
directory name, access permissions, etc
...
Documents
This option allows you to configure a default page that
displays when the user types in a full URL
...
aspx is listed as a default
page, the user need only enter http://www
...
com/, rather than http://www
...
com/default
...
You can
easily change and remove these default pages by selecting the appropriate button to the right of the menu
...
HTTP Headers
This option gives you the ability to forcefully control
page caching on the server, add custom HTTP Headers,
Edit Ratings (this helps identify the content your site
provides to users), and create MIME types
...
19
Chapter 1: Introducing ASP
...
NET Platform
Custom Errors
This option allows you to define your own custom error
pages
...
ASP
...
NET applications stored in that folder
...
Using Cassini
If you’re stuck using a version of Windows that doesn’t support IIS, you’ll need
to make use of Cassini to get your simple ASP
...
Cassini doesn’t support virtual directories, security settings, or any of
IIS’s other fancy features; it’s just a very simple web server that gives you the
basics you need to get up and running
...
Create a directory called C:\WebDocs\Learning, just like the one we created
in the section called “Virtual Directories”
...
Copy index
...
We first saw index
...
3
...
exe
...
Cassini has just three configuration options:
Application Directory
It’s here that your application’s files are stored
...
Server Port
Web servers almost always operate on port 80, so we won’t touch this
setting
...
By default, it’s set to /, meaning that you can access the
file C:\WebDocs\Learning\index
...
htm
...
htm
...
5
...
6
...
11
...
11
...
htm straight
away—you’ll be presented with a list of files instead
...
htm or default
...
21
Chapter 1: Introducing ASP
...
NET Platform
Installing SQL Server 2005 Express Edition
After making sure IIS, the
...
SQL Server 2005 is Microsoft’s database solution for medium to large companies
and enterprises
...
For the examples in this book, we’ll use SQL Server 2005 Express Edition, which
is free and sufficiently powerful for our needs
...
You can install SQL Server 2005 Express Edition as follows:
1
...
microsoft
...
2
...
The former
will be fine for our purposes
...
3
...
It’s safe to use the default
options all the way through, though it is a rather long process
...
Like IIS, SQL Server runs as a service in
the background, accepting connections to databases instead of web pages
...
Installing SQL Server Management Studio Express
In order to use your SQL Server 2005 install effectively, you’ll need some sort
of administration tool that will allow you to work with your databases
...
To install SQL Server Management Studio Express, follow these steps:
1
...
microsoft
...
2
...
3
...
Once it’s installed, SQL Server Manager Express can be accessed from Start > All
Programs > Microsoft SQL Server 2005 > SQL Server Management Studio Express
...
12 illustrates
...
12
...
Because you’re the user that
installed SQL Server 2005, you’ll already have full privileges to the SQL Server
...
23
Chapter 1: Introducing ASP
...
NET Platform
Figure 1
...
Managing your database server
After you’re authenticated, you’ll be shown the interface in Figure 1
...
SQL Server Management Studio lets you browse through the objects inside your
SQL Server, and even modify their settings
...
14
...
How is this useful? Imagine
you have a production server that runs two applications with two separate
databases on the same instance of SQL Server
...
If the
second application’s database was operating on a second instance of SQL
Server, we wouldn’t have such a problem—the second application would
continue working without missing a beat
...
The default instance name for
SQL Server 2005 Express Edition is SQLEXPRESS
...
14
...
You can see this specification back in Figure 1
...
13,
where we’re connecting to an instance called SQLEXPRESS on a computer
called VM2
...
For the
first exercises in this book, we’ll recommend you use a simple text editor such as
25
Chapter 1: Introducing ASP
...
NET Platform
Notepad, but you’ll gradually learn how to use Visual Web Developer to ease
some of the tasks we’ll tackle
...
1
...
microsoft
...
2
...
3
...
At one point, you’ll be asked about installing
Microsoft MSDN 2005 Express Edition, which is the product’s documentation
...
(Note that you’ve already installed the
...
0 documentation, together with the SDK
...
NET Framework 2
...
In this book, we’ll start using Visual Web Developer to build real web applications
in Chapter 5
...
Writing your First ASP
...
NET exercise, we’ll create the simple example shown in Figure 1
...
Figure 1
...
Your first ASP
...
NET Page
Let’s get started! Open your text editor (Notepad is fine)
...
NET pages automatically, such as Visual Studio
...
NET works
...
aspx in the
Learning folder you created earlier
...
aspx by entering
the HTML for our page, shown below:
File: FirstPage
...
0 Strict//EN"
"http://www
...
org/TR/xhtml1/DTD/xhtml1-strict
...
NET 2
...
NET code that will create the
dynamic elements of the page, starting with the time
...
aspx (excerpt)
...
0 Page
Hello there!
The time is now:
We’ve added an
...
The asp: part of the tag name
identifies it as a built-in ASP
...
ASP
...
27
Chapter 1: Introducing ASP
...
NET Platform
The runat="server" attribute identifies the tag as something that needs to be
handled on the server
...
NET sees it
and converts it to regular HTML tags before the page is sent to the browser
...
NET to replace this particular tag
with the current time
...
ASP
...
The two most common
languages are VB and C#
...
Here’s a
version of the page in VB:
Visual Basic
File: FirstPage
...
0 Strict//EN"
"http://www
...
org/TR/xhtml1/DTD/xhtml1-strict
...
NET Page
28
Writing your First ASP
...
If you type the case of a letter
incorrectly, the page won’t load
...
Both versions of the page achieve exactly the same thing
...
If you’ve never
done any server-side programming before, the code may look a little scary
...
Using
your web browser, load http://localhost/Learning/FirstPage
...
Whether you load the C# version or the VB version, the output should look like
Figure 1
...
No Time?
If the time isn’t displayed in the page, chances are that you opened the file
directly in your browser instead of loading it through your web server
...
NET is a server-side language, your web server needs to process the file
before it’s sent to your browser for display
...
NET code is never converted into HTML that your browser can
understand, so make sure you load the page by entering an actual HTTP
URL (such as http://localhost/Learning/FirstPage
...
aspx)
...
File: FirstPage
...
Like
the
The End Sub in the VB code, and the } in the C# code, mark the end of the
script that’s to be run when the page is loaded, and the tag marks the
end of the script block
...
NET generated
for you
...
Here’s what you’ll see:
...
NET code has gone! Even the script block has been
completely removed, and the
tag (which has the same id attribute as the
contains the date and time
...
NET and the
...
NET works
...
NET page; it’s just plain HTML like any other
...
NET code is run by your web server and converted to plain HTML that’s
sent to the browser
...
The
next chapter will get a bit more challenging as we investigate some valuable programming concepts
...
NET web applications, you will undoubtedly have questions
that need answers, and problems that need to be solved
...
NET support
web site7 was developed by Microsoft as a portal for the ASP
...
NET
...
You can also ask questions of the
experienced community members in the SitePoint Forums
...
NET
...
NET and that it’s a part of the
...
First, you learned about the components of ASP
...
NET Framework
...
NET development
...
NET! The next chapter
will build on this knowledge as we begin to introduce you to ASP
...
7
8
32
http://www
...
net/
http://www
...
com/forums/
2
ASP
...
NET is, and what it can do
...
NET
page
...
NET at more advanced levels
...
But before you can
begin to develop applications with ASP
...
NET page—with this knowledge, you’ll be able to
identify the parts of the ASP
...
So, in this chapter, we’ll talk about some key mechanisms
of an ASP
...
NET Basics
We’ll also cover two of the “built-in” languages supported by the
...
As this section progresses, we’ll explore the differences and
similarities between these two languages, and get a clear idea of the power that
they provide to those creating ASP
...
So, what exactly makes up an ASP
...
NET page
...
NET Page Structure
ASP
...
aspx file name extension,
and can be placed on any web server equipped with ASP
...
When a client requests an ASP
...
NET runtime, a program that runs on the web server that’s responsible for
reading the page and compiling it into a
...
This class is then used to
produce the HTML that’s sent back to the user
...
NET class can respond directly to the
request, producing the page’s HTML and sending it to the client, until such time
as the
...
This process is illustrated in Figure 2
...
An ASP
...
NET server controls
❑ server-side comments
❑ literal text and HTML tags
Figure 2
...
NET page
...
NET Page Structure
Figure 2
...
The life cycle of the ASP
...
2
...
NET page
35
Chapter 2: ASP
...
aspx within the Learning virtual directory you created
in Chapter 1
...
aspx
should render the result shown in Figure 2
...
Figure 2
...
Sample Page in action
As you can see, this ASP
...
NET page
...
Directives
The directives section is one of the most important parts of an ASP
...
Directives control how a page is compiled, specify how a page is cached by web
browsers, aid debugging (error-fixing), and allow you to import classes to use
within your page’s code
...
This is followed by the
directive name, plus any attributes and their corresponding values
...
There are many directives that you can use within your pages, and we’ll discuss
them in greater detail later, but, for now, know that the Import and Page directives
are the most useful for ASP
...
Looking at the sample ASP
...
2, we can see that a Page directive was used at the top of the
page like so:
Visual Basic
<%@ Page Language="VB" %>
36
File: Hello
...
aspx (excerpt)
<%@ Page Language="C#" %>
In this case, the Page directive specifies the language that’s to be used for the
application logic by setting the Language attribute
...
A
whole range of different directives is available; we’ll see a few more later in this
chapter
...
NET directives can appear anywhere on a page, but they’re
commonly included at its very beginning
...
NET page’s HTML
...
NET page
...
In our page, we’ve placed the code
inside
Here, the tags enclose VB code, but it could just as easily be C#:
C#
Comments in VB and C# Code
Both of these code snippets contain comments—explanatory text that will be ignored by ASP
...
37
Chapter 2: ASP
...
In C# code, two slashes (//) achieve the same end
...
NET emerged, ASP also supported such script tags using a runat="server" attribute
...
Code declaration blocks are generally placed inside the head of your ASP
...
The sample ASP
...
2, for instance, contains the
following code declaration block:
Visual Basic
File: Hello
...
Text = "Hello World"
End Sub
Perhaps you can work out what the equivalent C# code would be:
C#
File: Hello
...
Text = "Hello World";
}
The
C#
File: ViewState
...
0 Strict//EN"
"http://www
...
org/TR/xhtml1/DTD/xhtml1-strict
...
Text = nameTextBox
...
NET Basics
In this case, the code uses ASP
...
As you can see in Figure 2
...
Figure 2
...
ASP
...
But where’s all that information
stored? ASP
...
View the source of the page after you’ve submitted the form,
and look for the following code:
value="/wEPDwUKLTEwNDY1Nzg0MQ9…0fMCR+FN5P6v5pkTQwNEl5xhBk" />
This is a standard HTML hidden form field
...
View state is enabled for every page by default
...
To do this, set the EnableViewState property of the Page directive to
false:
<%@ Page EnableViewState="False" %>
46
Working with Directives
Disabling View State, Control by Control
View state can also be disabled for particular controls in a page: simply set
their EnableViewState property to false
...
Speaking of directives, it’s time to take a closer look at these curious beasts!
Working with Directives
For the most part, ASP
...
In essence, just using the
...
NET Framework for processing
...
We talked a little about directives and what they can do earlier in this chapter
...
Three of the most commonly used directives are:
Page
This directive defines page-specific attributes for the ASP
...
We’ve already seen this directive in
use
...
The following example, for instance, imports functionality from the System
...
Mail namespace, which you could use to send
email from a page
...
NET’s way of keeping all its
functionality neatly organized—we’ll see how they work in Chapter 3
...
Web
...
Register
This directive allows you to register a user control for use on your page
...
NET Basics
<%@ Register TagPrefix="uc" TagName="footer"
Src="footer
...
NET Languages
As we saw in the previous chapter,
...
If you’re
used to writing ASP, you may think the choice of VBScript or JScript would be
an obvious one
...
NET, Microsoft did away with VBScript, merging
it with Visual Basic
...
NET’s support for C# is likely to find favor with developers from other backgrounds
...
By the end
of this section, you will, I hope, agree that the similarities between the two are
astonishing—any differences are minor and, in most cases, easy to figure out
...
For instance, old-style CGI scripts were typically
written with Perl or C/C++, JSP uses Java, Coldfusion uses CFML, and PHP is
a technology and a language rolled into one
...
To keep things simple, this
book will consider the two most popular: VB and C#
...
Visual Basic
The latest version of Visual Basic is the result of a dramatic overhaul of Microsoft’s
hugely popular Visual Basic language
...
The latest version of VB has many advantages over older versions, most notably the fact that it has now became a fully object oriented language
...
Despite the changes, VB generally stays close to the
structured, legible syntax that has always made it so easy to read, use, and
maintain
...
However, there’s little doubt that its development was at least
hurried along by Microsoft’s legal disputes with Sun
...
We’re not going to worry about J# here, as
C# is preferable
...
Summary
In this chapter, we started out by introducing key aspects of an ASP
...
As the chapter progressed, we took a closer look at the two
most popular languages that ASP
...
In the next chapter, we’ll create a few more ASP
...
49
50
3
VB and C# Programming Basics
As you learned at the end of the last chapter, one of the great things about using
ASP
...
NET languages
we like
...
We’ll start by discussing some basic
concepts of programming ASP
...
We’ll explore programming fundamentals such as variables, arrays, functions,
operators, conditionals, loops, and events, and work through a quick introduction
to object oriented programming (OOP)
...
The final sections of the chapter cover some of the ideas underlying modern, effective ASP
...
We finish with an examination of
how object oriented programming techniques impact the ASP
...
Programming Basics
One of the building blocks of an ASP
...
To get anywhere with
ASP
...
All ASP
...
Each of these controls
Chapter 3: VB and C# Programming Basics
allows the user to interact with the application in some way: checking checkboxes,
scrolling through lists, selecting list items, and so on
...
It is by handling these events
within our code that we get ASP
...
For instance, imagine that a user clicks a button on an ASP
...
That
button (or, more specifically, the ASP
...
A method called an event handler executes
automatically when an event is raised—in this case, the event handler code performs a specific action for that button
...
Events really are the key to ASP
...
It wouldn’t be practical, or even necessary, to cover all aspects of VB and C# in
this book, so we’re going to discuss enough to get you started, and complete this
chapter’s projects and samples using both languages
...
NET
...
For instance,
the code below creates a server-side button and label
...
If you want to test the code, save the file in the
Learning virtual directory you’ve been using for the other examples
...
aspx (excerpt)
52
Control Events and Subroutines
When the button’s clicked, it raises the Click event, and ASP
...
In the code above, we told ASP
...
aspx (excerpt)
You can see that the event handlers (the functions that are executed to handle
the events) aren’t specifically defined anywhere
...
NET page, and their
handlers have the default names that we’ve used in the code (Page_Init,
Page_Load, etc)
...
Within
each event’s event handler, we’ve added a message to the Label control; this will
give us visual proof that the events actually fire in order
...
2
...
This is because, at
that point, the HTML output has already been generated and sent to the browser
...
However,
in certain situations the other events will be helpful as well
...
NET supports other events, which we haven’t covered here
...
58
Variables and Variable Declaration
Figure 3
...
Handling ASP
...
Basically, variables let you give a name, or identifier,
to a specific piece of data; we can then use that identifier to store, modify, and
retrieve the data in question
...
NET Framework
...
There are many different kinds
of data types, including strings, integers (whole numbers), and floating point
numbers (fractions or decimals)
...
” In VB, we use the keyword Dim, which
stands for “dimension,” while in C#, we simply precede the variable name with
the appropriate data type
...
This practice isn’t recommended, though, as it makes the code more
difficult to read
...
1 lists the most useful data types available in VB and C#
...
1
...
Many Aliases are Available
These data types are the VB- and C#-specific aliases for types of the
...
For example, instead of Integer or int, you could use
System
...
NET language; likewise, instead of Boolean or bool,
you could use System
...
60
Variables and Variable Declaration
To sum up, once you’ve declared a variable as a given type, it can only hold data
of that type: you can’t put a string into an integer variable, for instance
...
Have a look at this code:
Visual Basic
Dim intX As Integer
Dim strY As String = "35"
intX = strY + 6
C#
int intX;
string strY = "35";
intX = strY + 6;
Now, you or I might think that this could make sense—after all, the string strY
contains a number, so we might wish to add it to another number
...
The VB version of the code will execute
without a hitch, because the string will be converted to a number before the
mathematical operation is applied
...
As a rule of thumb, it’s better to stay on the safe side and avoid mixing types
wherever possible
...
Strongly typed languages are those that are very
strict about data types
...
For example, if you try to sum a
number with a string, as we did in the previous code snippet, the JavaScript
interpreter would make the conversion for you automatically
...
In
...
Parse(strY) + 6
C#
int intX;
string strY = "35";
intX = Convert
...
This principle holds true whenever we’re mixing types in a single expression
...
Any one item in an array can be accessed using the array’s
name, followed by that item’s position in the array (its offset)
...
The results of this code are shown in Figure 3
...
aspx
C#
File: Functions
...
0 Strict//EN"
"http://www
...
org/TR/xhtml1/DTD/xhtml1-strict
...
NET Functions