Build Your Own ASP.NET 3.5 Website Using C# & VB (2 page)

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

BOOK: Build Your Own ASP.NET 3.5 Website Using C# & VB
9.49Mb size Format: txt, pdf, ePub

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

What’s in This Book? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxii

The Book’s Web Site
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
xxv

The Code Archive
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
xxvi

Updates and Errata
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
xxvi

The SitePoint Forums
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
xxvi

The SitePoint Newsletters
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
xxvii

Your Feedback . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxvii

Conventions Used in This Book
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
xxvii

Code Samples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxvii

Tips, Notes, and Warnings
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
xxix

Acknowledgments
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
xxix

Chapter 1

Introducing ASP.NET and the .NET

Platform
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1

What is ASP.NET? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2

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

Getting Help . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22

Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23

Chapter 2

ASP.NET Basics
. . . . . . . . . . . . . . . . . . . . . . . . . . . 25

ASP.NET Page Structure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26

Licensed to [email protected]

x

Directives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30

Code Declaration Blocks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30

Code Render Blocks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33

ASP.NET Server Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34

Server-side Comments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35

Literal Text and HTML Tags . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36

View State . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37

Working with Directives
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
41

ASP.NET Languages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42

Visual Basic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42

C#
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
43

Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43

Chapter 3

VB and C# Programming Basics
. . . .
45

Programming Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46

Control Events and Subroutines
. . . . . . . . . . . . . . . . . . . . . . . . . . . . .
46

Page Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51

Variables and Variable Declaration . . . . . . . . . . . . . . . . . . . . . . . . . . . 54

Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58

Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61

Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65

Conditional Logic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67

Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69

Object Oriented Programming Concepts . . . . . . . . . . . . . . . . . . . . . . . . . . 75

Objects and Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75

Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78

Methods
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
79

Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80

Constructors
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
80

Scope
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
81

Licensed to [email protected]

xi

Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82

Understanding Inheritance
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
82

Objects in .NET . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83

Namespaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85

Using Code-behind Files
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
86

Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93

Chapter 4

Constructing ASP.NET Web

Pages
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
95

Web Forms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 96

HTML Server Controls
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
97

Using the HTML Server Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99

Web Server Controls
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
104

Standard Web Server Controls
. . . . . . . . . . . . . . . . . . . . . . . . . . . . .
106

List Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115

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

Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 155

Chapter 5

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

Global.asax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 176

Using Application State . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 178

Working with User Sessions
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
187

Using the Cache Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 189

Using Cookies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 191

Starting the Dorknozzle Project
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
193

Preparing the Sitemap . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 194

Using Themes, Skins, and Styles . . . . . . . . . . . . . . . . . . . . . . . . . . . . 196

Other books

Balancing Act by Joanna Trollope
The Devil's Door by Sharan Newman
The Alien's Return by Jennifer Scocum
The Rival by Kristine Kathryn Rusch
El castillo de cristal by Jeannette Walls
Snake Bite by Andrew Lane
When I Was Otherwise by Stephen Benatar
Open Season by Archer Mayor