Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:

ASP.NET 2.0 Everyday Apps For Dummies (2006)

.pdf
Скачиваний:
56
Добавлен:
17.08.2013
Размер:
10.07 Mб
Скачать

 

Table of Contents

xv

 

Building the Master Page ............................................................................

238

 

Building the Menu Page ..............................................................................

239

 

Building the Category Maintenance Page .................................................

240

 

The CatMaint.aspx file .......................................................................

240

 

The code-behind file for the Catalog Maintenance page...............

245

 

Building the Product Maintenance Page ...................................................

249

 

The ProdMaint.aspx file.....................................................................

249

 

The code-behind file for the Product Maintenance page..............

265

 

Chapter 8: Building a Report Application . . . . . . . . . . . . . . . . . . . . .

. .269

 

The Application’s User Interface................................................................

270

 

Designing the Application...........................................................................

271

 

Building the Database..................................................................................

272

 

Designing the database......................................................................

272

 

Creating the database ........................................................................

274

 

Adding test data .................................................................................

274

 

SQL statements to retrieve the order data .....................................

275

 

Connecting to the database ..............................................................

276

 

Building the Master Page ............................................................................

276

 

Building the Order User Control ................................................................

277

 

Building the Default Page ............................................................................

283

 

The Default.aspx file ..........................................................................

283

 

The code-behind file for the default page .......................................

284

 

Building the Print Order page.....................................................................

286

 

Part V: Building Community Applications ...................

287

 

Chapter 9: Building a Content Management System . . . . . . . . . . .

. .289

 

Making Some Basic Decisions ....................................................................

289

 

The Content Management System’s User Interface.................................

291

 

The Login page ...................................................................................

292

 

The Home page...................................................................................

292

 

The Department Home page .............................................................

293

 

The Content List page........................................................................

294

 

The Content Detail page ....................................................................

295

 

Designing the Database ...............................................................................

297

 

The Departments table......................................................................

297

 

The ContentTypes table ....................................................................

298

 

The ContentItems table .....................................................................

298

 

Creating the Database .................................................................................

299

 

Adding Test Data ..........................................................................................

301

 

SQL statements for working with the database .............................

302

 

Connecting to the database ..............................................................

303

 

Creating the User Accounts ........................................................................

303

 

Building the Master Page ............................................................................

304

 

Building the Login Page...............................................................................

307

 

Building the Home Page ..............................................................................

308

 

xvi

ASP.NET 2.0 Everyday Apps For Dummies

Building the Department Home Page ........................................................

309

Building the Content List Page ...................................................................

313

The List.aspx file ................................................................................

314

The code-behind file for the Content List page ..............................

317

Building the Content Detail Page ...............................................................

318

The Detail.aspx file.............................................................................

319

The code-behind file for the Content Detail page ..........................

325

Chapter 10: Building a Web Forum . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

329

Designing the Forum Application...............................................................

329

The User Interface for the Forum Application .........................................

331

The Forum Home page ......................................................................

331

The Threads page...............................................................................

332

The Messages page ............................................................................

332

The New Thread page........................................................................

334

The Post Reply page ..........................................................................

334

Designing the Database ...............................................................................

335

The Forums table ...............................................................................

335

The Topics table .................................................................................

336

The Threads table ..............................................................................

337

The Messages table............................................................................

338

Creating the Database .................................................................................

338

Adding Test Data ..........................................................................................

340

SQL statements for working with the database .............................

341

Connecting to the database ..............................................................

342

Building the Master Page ............................................................................

342

Building the Forum Home Page..................................................................

343

The Default.aspx page .......................................................................

344

The code-behind file for the Forum Home page.............................

346

Building the Threads Page ..........................................................................

350

The Threads.aspx page......................................................................

350

The code-behind file for the Threads page.....................................

353

Building the Messages Page........................................................................

355

The Messages.aspx page ...................................................................

355

The code-behind file for the Messages page ..................................

359

Building the New Thread Page ...................................................................

360

The NewThread.aspx page................................................................

361

The code-behind file for the New Thread page ..............................

363

Building the New Message Page.................................................................

367

The NewMessage.aspx page .............................................................

368

The code-behind file for the New Message page............................

371

Chapter 11: Building a Blog Application . . . . . . . . . . . . . . . . . . . . . . . .

375

Designing the Blog Application ..................................................................

375

Designing the User Interface.......................................................................

376

The Blog Home page ..........................................................................

376

The Blog page .....................................................................................

378

The Comments page ..........................................................................

379

The Leave Comment page .................................................................

379

 

Table of Contents

xvii

 

The Login page ...................................................................................

380

 

The Register page...............................................................................

381

 

The My Blogs page .............................................................................

381

 

The New Post page.............................................................................

383

 

Designing the Database ...............................................................................

383

 

The Blogs table ...................................................................................

384

 

The Posts table ...................................................................................

384

 

The Comments table..........................................................................

385

 

Creating the Database .................................................................................

386

 

Adding test data .................................................................................

388

 

SQL statements for working with the database .............................

389

 

Connecting to the database ..............................................................

390

 

Building the Master Page ............................................................................

390

 

Building the Blog Home Page .....................................................................

392

 

Building the Blog Page.................................................................................

395

 

The Blog.aspx page ............................................................................

395

 

The code-behind file for the Blog page............................................

399

 

Building the Comments Page......................................................................

402

 

The Comments.aspx page .................................................................

402

 

The code-behind file for the Comments page.................................

405

 

Building the Leave Comment Page ............................................................

407

 

The Comment.aspx page ...................................................................

407

 

The code-behind file for the Leave Comment page

.......................409

 

Building the Login Page...............................................................................

410

 

Building the Register Page ..........................................................................

411

 

Building the My Blogs Page ........................................................................

412

 

The MyBlogs.aspx page .....................................................................

413

 

The code-behind file for the My Blogs page ...................................

416

 

Building the New Post Page ........................................................................

418

 

The NewPost.aspx page.....................................................................

418

 

The code-behind file for the New Post page ...................................

421

 

Part VI: The Part of Tens............................................

423

 

Chapter 12: Ten New Features of ASP.NET 2.0 . . . . . . . . . . . . . . . . . . .

425

 

The New Code-Behind Model .....................................................................

426

 

App_ Folders.................................................................................................

427

 

Master Pages.................................................................................................

428

 

Creating a Master Page ......................................................................

429

 

Completing a Master Page.................................................................

430

 

Creating a Content page ....................................................................

431

 

New Data Controls .......................................................................................

432

 

The SqlDataSource control ...............................................................

433

 

The GridView control.........................................................................

436

 

The DetailsView control ....................................................................

438

 

The FormView Control.......................................................................

439

 

Login Controls ..............................................................................................

440

 

xviii ASP.NET 2.0 Everyday Apps For Dummies

The Wizard Control......................................................................................

440

The Generics Feature...................................................................................

441

The Web Site Administration Tool .............................................................

443

Chapter 13: Ten Rookie Mistakes . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. .445

Coding Too Soon ..........................................................................................

445

Skimping On Documentation......................................................................

446

Inadequate Testing.......................................................................................

447

Abusing State Features ................................................................................

447

Not Validating Input Data ............................................................................

448

Reinventing the Wheel.................................................................................

448

Not Asking for Help......................................................................................

449

Chapter 14: Ten Database Design Tips . . . . . . . . . . . . . . . . . . . . . . .

. .451

Use the Right Number of Tables.................................................................

451

Avoid Repeating Data ..................................................................................

452

Avoid Redundant Data.................................................................................

452

Use a Naming Convention ...........................................................................

452

Avoid nulls ....................................................................................................

453

Avoid Secret Codes ......................................................................................

453

Use Constraints Wisely................................................................................

454

Use Triggers When Appropriate.................................................................

454

Use Stored Procedures ................................................................................

454

Appendix: About the CD.............................................

455

System Requirements ..................................................................................

455

Using the CD .................................................................................................

456

Using the Source Files .................................................................................

456

Troubleshooting...........................................................................................

457

Index........................................................................

459

Introduction

Welcome to ASP.NET 2.0 Everyday Apps For Dummies, the book that teaches ASP.NET 2.0 Web programming by example. In this book, you’ll find eight complete ASP.NET applications. We’re not talking trivial

Hello-World-type applications here. Instead, they’re real-world applications like shopping carts and discussion forums. You can use any of them as-is, or modify them as you see fit. So you’ve got workable stuff already included. (What a concept.)

About This Book

This book is a practical introduction to ASP.NET 2.0 Web programming. It provides you with actual working code to build the most popular types of applications on the Web. These applications enable you to:

Restrict access to registered users, for all or part of your Web site

Sell products online via your Web site

Provide back-end functions for your public Web site, such as file maintenance and reporting

Let users manage specific types of online content

Create discussion forums and blogs

ASP.NET 2.0 Everyday Apps For Dummies doesn’t pretend to be a comprehensive reference for every detail of ASP.NET programming. Instead, it takes a learn-by-example approach, under the assumption that you are already a pretty competent programmer who can best learn by seeing real-world examples. Designed using the easy-to-follow For Dummies format, this book helps you get the information you need without laboring to find it.

Conventions Used in This Book

This book has a lot of code in it. You’ll find complete listing of every line of code, both C# and Visual Basic, for each of the eight applications presented in this book. You’ll also find listings for supporting files such as SQL scripts to generate databases and web.config files that provide configuration information for the applications.

2ASP.NET 2.0 Everyday Apps For Dummies

Most of these listings include reference numbers that correspond to numbered explanations in the text. In most cases, these explanations apply to both the C# and the Visual Basic versions of the code. (For example, the code explanation identified with reference number 3 applies to the line indicated with reference number 3 in both the C# and the Visual Basic versions of the listing.)

To keep page-flipping to a minimum, I always present the C# version of a listing first, followed by the text that explains it, followed by the Visual Basic listing. Thus, if you’re a C# programmer, you can flip forward from your listing to the text that explains it. And if you’re a Visual Basic programmer, you can flip backward from your listing to the corresponding blow-by-blow description.

On occasion, I’ll show a console prompt along with commands you need to enter. In that case, the command is presented as follows:

sqlcmd -S localhost\SQLExpress -s createdb.sql

How This Book Is Organized

This book is divided into six parts, with two or three chapters in each part. Chapters 4 through 11 present the applications themselves. In these particular chapters, you’re going to find the same basic structure, which (hopefully) gets the following information across for each one:

A discussion of design issues for the application.

An overview of the application’s user interface, including a diagram that shows the flow of the application’s pages, along with images of each page.

A description of the design for the database used by the application, along with listings of the scripts used to create the database and populate it with sample test data.

Listings of the .aspx files for each of the application’s pages.

Where appropriate, listings of the code-behind file in both C# and Visual Basic.

Explanations of the key parts of the listings.

If you’re up for a quick summary, the following sections offer a bird’s-eye view of what you can find in each part of the book.

Introduction 3

Part I: Introducing ASP.NET 2.0

Application Development

Chapter 1 in this part is a general introduction to ASP.NET application development. It explains bedrock concepts such as the system-development life cycle, using layered architectures in ASP.NET applications, and designing relational databases. Then Chapter 2 presents a quick introduction to building ASP.NET applications using Visual Studio 2005 or Visual Web Developer 2005 Express Edition.

Part II: Building Secure Applications

This part shows you how to build security into your applications. Chapter 3 is an introduction to ASP.NET 2.0 security. Then, behold: Chapter 4 presents the first application in this book — a user-authentication application that you can incorporate into any application that requires users to register and log in.

Part III: Building E-Commerce

Applications

This part provides two complete e-commerce applications. The first is an online product catalog that lets users view your products via a Web site. The second is a shopping-cart application that lets users purchase products. In fact, the shopping-cart application is an extension of the product-catalog application: It lets users purchase products they’ve had a chance to view via the online catalog.

Part IV: Building Back-End Applications

Just about all public Web applications have a back end that isn’t visible to the general public. This part presents two applications you’ll often need in the back end. The first is a file-maintenance application that lets you add, update, or delete records in a products database. And the second is a report application that generates reports based on data gathered from Web visitors.

4ASP.NET 2.0 Everyday Apps For Dummies

Part V: Building Community Applications

The three applications in this part let users create Web-based communities. Chapter 9 presents a content-management system for users’ Web sites; Chapter 10 presents a forum application for posting messages and replies. And Chapter 11 presents a blog application that lets users create blog articles that visitors can then read and comment upon.

Part VI: The Part of Tens

No For Dummies book would be complete without a Part of Tens. In Chapter 12, you get an overview of the most important new features of ASP.NET 2.0. If you’re an experienced ASP.NET programmer but haven’t worked with version 2.0 yet, you may want to read this chapter first to see what new features have been added for this version.

Next Chapter 13 describes ten-or-so rookie mistakes that (with any luck) you can avoid. And finally, Chapter 14 presents ten-or-so tips for designing your databases.

About the CD

The CD that’s glued into the back of this book contains all the source code for the applications presented in this book. You’ll find two versions of each application: a C# version and a Visual Basic version. The CD lets you choose which versions of the applications to install. If you want, you can install both versions of the applications; C# and VB can easily coexist within the same ASP.NET system. (Although it’s uncommon, C# and VB can even coexist within the same ASP.NET application.)

Icons Used in This Book

Like any For Dummies book, this book is chock-full of helpful icons that draw your attention to items of particular importance. You find the following icons throughout this book:

Pay special attention to this icon; it lets you know that some particularly useful tidbit is at hand.

Introduction 5

Did I tell you about the memory course I took?

Danger, Will Robinson! This icon highlights information that may help you avert disaster.

Watch out! Some technical drivel is just around the corner. Read it only if you have your pocket protector firmly attached.

Where to Go from Here

Yes, you can get there from here. With this book in hand, you’re ready to get right to the task of creating ASP.NET 2.0 Web applications. Browse through the table of contents and decide which type of application interests you most. Then, jump in and hang on. Be bold! Be courageous! Be adventurous! And above all, have fun!

6ASP.NET 2.0 Everyday Apps For Dummies