Read Build Your Own ASP.NET 3.5 Website Using C# & VB Online
Authors: Cristian Darie,Zak Ruvalcaba,Wyatt Barnett
Tags: #C♯ (Computer program language), #Active server pages, #Programming Languages, #C#, #Web Page Design, #Computers, #Web site development, #internet programming, #General, #C? (Computer program language), #Internet, #Visual BASIC, #Microsoft Visual BASIC, #Application Development, #Microsoft .NET Framework
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.
Published by SitePoint Pty. Ltd.
48 Cambridge Street Collingwood
VIC Australia 3066.
Web: www.sitepoint.com
Email: [email protected]
ISBN 978-0-9804552-1-2
Printed and bound in the United States of America
Licensed to [email protected]
v
About the Authors
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 E-Commerce
series. Having worked with computers since he was old enough to use a keyboard, he initially
tasted programming success with a prize in his first programming contest at the age of 12.
From there, Cristian moved on to many other similar achievements, and is now studying
distributed application architectures for his PhD.
He always loves hearing feedback about his books, so don't hesitate to drop him a “hello”
message when you have a spare moment. Cristian can be contacted through his personal web
site at http://www.cristiandarie.ro.
Wyatt Barnett leads the in-house development team for a major industry trade association
in Washington DC. When not slinging obscene amounts of C# and SQL at a few exceedingly
large monitors, he is most often spotted staring at HDTV and other forms of entertainment
in local watering holes. He also writes for SitePoint's .NET blog,
The Daily Catch
.1
About the Technical Editor
Andrew Tetlaw has been tinkering with web sites as a web developer since 1997 and has
also worked as a high school English teacher, an English teacher in Japan, a window cleaner,
a car washer, a kitchen hand, and a furniture salesman. At SitePoint he is dedicated to making the world a better place through the technical editing of SitePoint books, kits, and articles. He is also a busy father of five, enjoys coffee, and often neglects his blog at http://tetlaw.id.au/.
About the Technical Director
As Technical Director for SitePoint, Kevin Yank oversees all of its technical publications—books, articles, newsletters, and blogs. He has written over 50 articles for SitePoint, but is best known for his book,
Build Your Own Database Driven Website Using PHP &
MySQL
. Kevin lives in Melbourne, Australia, and enjoys performing improvised comedy
theater and flying light aircraft.
About SitePoint
SitePoint specializes in publishing fun, practical, and easy-to-understand content for web
professionals. Visit http://www.sitepoint.com/ to access our books, newsletters, articles, and community forums.
1 http://www.sitepoint.com/blogs/category/net/
Licensed to [email protected]
Licensed to [email protected]
To my family and friends.
—Cristian Darie
To my Father, whose guidance got
me this far.
—Wyatt Barnett
Licensed to [email protected]
Licensed to [email protected]
Table of Contents
Preface
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
xxi
Who Should Read This Book?
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
xxi
The Book’s Web Site
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
xxv
The Code Archive
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
xxvi
Updates and Errata
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
xxvi
The SitePoint Forums
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
xxvi
The SitePoint Newsletters
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
xxvii
Conventions Used in This Book
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
xxvii
Tips, Notes, and Warnings
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
xxix
Acknowledgments
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
xxix
Introducing ASP.NET and the .NET
Platform
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
Installing the Required Software
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
5
Installing Visual Web Developer 2008 Express Edition
. . . . . . . . . . . .
6
Installing SQL Server Management Studio Express
. . . . . . . . . . . . . .
8
Writing Your First ASP.NET Page . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
ASP.NET Basics
. . . . . . . . . . . . . . . . . . . . . . . . . . . 25
Licensed to [email protected]
x
Code Declaration Blocks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30
Code Render Blocks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
ASP.NET Server Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
Server-side Comments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
Literal Text and HTML Tags . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36
Working with Directives
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
41
C#
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
43
VB and C# Programming Basics
. . . .
45
Control Events and Subroutines
. . . . . . . . . . . . . . . . . . . . . . . . . . . . .
46
Variables and Variable Declaration . . . . . . . . . . . . . . . . . . . . . . . . . . . 54
Object Oriented Programming Concepts . . . . . . . . . . . . . . . . . . . . . . . . . . 75
Objects and Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75
Methods
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
79
Constructors
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
80
Scope
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
81
Licensed to [email protected]
xi
Understanding Inheritance
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
82
Using Code-behind Files
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
86
Pages
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
95
HTML Server Controls
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
97
Using the HTML Server Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99
Web Server Controls
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
104
Standard Web Server Controls
. . . . . . . . . . . . . . . . . . . . . . . . . . . . .
106
Advanced Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117
Web User Controls
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
133
Creating a Web User Control
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
134
Master Pages
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
142
Using Cascading Style Sheets (CSS)
. . . . . . . . . . . . . . . . . . . . . . . . . . . . .
148
Types of Styles and Style Sheets . . . . . . . . . . . . . . . . . . . . . . . . . . . 149
Building Web Applications
. . . . . . . . . .
157
Introducing the Dorknozzle Project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 158
Using Visual Web Developer
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
160
Meeting the Features . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 161
Executing Your Project
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
169
Core Web Application Features
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
171
Licensed to [email protected]
xii
Web.config
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
172
Using Application State . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 178
Working with User Sessions
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
187
Using the Cache Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 189
Starting the Dorknozzle Project
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
193
Preparing the Sitemap . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 194
Using Themes, Skins, and Styles . . . . . . . . . . . . . . . . . . . . . . . . . . . . 196