Tải bản đầy đủ (.pdf) (868 trang)

Beginning ASP NET 4 5 1 in c and VB

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (14.38 MB, 868 trang )

www.it-ebooks.info


www.it-ebooks.info


Beginning ASP.NET 4.5.1
Foreword. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxv
Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxvii
Chapter 1Getting started with ASP.NET 4.5.1 . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
Chapter 2

Building an ASP.NET Website . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33

Chapter 3

Designing Your Web Pages. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63

Chapter 4

Working with ASP.NET Server Controls. . . . . . . . . . . . . . . . . . . . . . . 101

Chapter 5

Programming Your ASP.NET Web Pages. . . . . . . . . . . . . . . . . . . . . . 135

Chapter 6

Creating Consistent Looking Websites. . . . . . . . . . . . . . . . . . . . . . . 195

Chapter 7Navigation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 239


Chapter 8

User Controls. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 273

Chapter 9

Validating User Input. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 299

Chapter 10 ASP.NET AJAX. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 337
Chapter 11 jQuery. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 369
Chapter 12 Introducing Databases. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 407
Chapter 13 Displaying and Updating Data. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 439
Chapter 14 LINQ and the ADO.NET Entity Framework. . . . . . . . . . . . . . . . . . . . 485
Chapter 15 Working with Data — ​Advanced Topics . . . . . . . . . . . . . . . . . . . . . . 539
Chapter 16Security in Your ASP.NET Website. . . . . . . . . . . . . . . . . . . . . . . . . . . 589
Chapter 17 Personalizing Websites. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 629
Chapter 18 Exception Handling, Debugging, and Tracing . . . . . . . . . . . . . . . . . 663
Chapter 19 Deploying Your Website. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 715
Appendix A Exercise Answers. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 757
Appendix B Configuring SQL Server 2012. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 781
Index. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 795

www.it-ebooks.info

ffirs.indd 1

25-02-2014 10:44:30


www.it-ebooks.info


ffirs.indd 2

25-02-2014 10:44:30


Beginning

ASP.NET 4.5.1

www.it-ebooks.info

ffirs.indd 3

25-02-2014 10:44:30


www.it-ebooks.info

ffirs.indd 4

25-02-2014 10:44:30


Beginning

ASP.NET 4.5.1
in C# and VB

Imar Spaanjaars


www.it-ebooks.info

ffirs.indd 5

25-02-2014 10:44:36


Beginning ASP.NET 4.5.1: in C# and VB
Published by
John Wiley & Sons, Inc.
10475 Crosspoint Boulevard
Indianapolis, IN 46256
www.wiley.com

Copyright © 2014 by John Wiley & Sons, Inc., Indianapolis, Indiana
Published simultaneously in Canada
ISBN: 978-1-118-84677-3
ISBN: 978-1-118-84690-2 (ebk)
ISBN: 978-1-118-84696-4 (ebk)
Manufactured in the United States of America
10 9 8 7 6 5 4 3 2 1
No part of this publication may be reproduced, stored in a retrieval system or transmitted in any form or by any means,
electronic, mechanical, photocopying, recording, scanning or otherwise, except as permitted under Sections 107 or 108
of the 1976 United States Copyright Act, without either the prior written permission of the Publisher, or authorization
through payment of the appropriate per-copy fee to the Copyright Clearance Center, 222 Rosewood Drive, Danvers,
MA 01923, (978) 750-8400, fax (978) 646-8600. Requests to the Publisher for permission should be addressed to the
Permissions Department, John Wiley & Sons, Inc., 111 River Street, Hoboken, NJ 07030, (201) 748-6011,
fax (201) 748-6008, or online at />Limit of Liability/Disclaimer of Warranty: The publisher and the author make no representations or warranties with
respect to the accuracy or completeness of the contents of this work and specifically disclaim all warranties, including

without limitation warranties of fitness for a particular purpose. No warranty may be created or extended by sales or
promotional materials. The advice and strategies contained herein may not be suitable for every situation. This work
is sold with the understanding that the publisher is not engaged in rendering legal, accounting, or other professional
services. If professional assistance is required, the services of a competent professional person should be sought. Neither
the publisher nor the author shall be liable for damages arising herefrom. The fact that an organization or Web site is
referred to in this work as a citation and/or a potential source of further information does not mean that the author or the
publisher endorses the information the organization or Web site may provide or recommendations it may make. Further,
readers should be aware that Internet Web sites listed in this work may have changed or disappeared between when this
work was written and when it is read.
For general information on our other products and services please contact our Customer Care Department within the
United States at (877) 762-2974, outside the United States at (317) 572-3993 or fax (317) 572-4002.
Wiley publishes in a variety of print and electronic formats and by print-on-demand. Some material included with standard
print versions of this book may not be included in e-books or in print-on-demand. If this book refers to media such as a CD
or DVD that is not included in the version you purchased, you may download this material at http://booksupport
.wiley.com. For more information about Wiley products, visit www.wiley.com.
Library of Congress Control Number: 2013958296
Trademarks: Wiley, the Wiley logo, Wrox, the Wrox logo, Programmer to Programmer, and related trade dress are
trademarks or registered trademarks of John Wiley & Sons, Inc. and/or its affiliates, in the United States and other
countries, and may not be used without written permission. All other trademarks are the property of their respective
owners. John Wiley & Sons, Inc., is not associated with any product or vendor mentioned in this book.

www.it-ebooks.info

ffirs.indd 6

25-02-2014 10:44:36


To Niek


www.it-ebooks.info

ffirs.indd 7

25-02-2014 10:44:36


www.it-ebooks.info

ffirs.indd 8

25-02-2014 10:44:36


About the Author

Imar Spaanjaars  graduated in Leisure Management at the Leisure Management School in the

Netherlands, but he quickly changed his career path to the Internet world.
After working in the Internet business at various web agencies for over twelve years, he now runs
his own company called De Vier Koeden (), a small Internet agency
specializing in consultancy and development of Internet and intranet applications with Microsoft
technologies such as ASP.NET 4.5.1. He’s also the CTO of Dynamicweb North America, the U.S.
branch of the popular Danish content management, e-commerce, and online marketing platform.
Imar has written books on ASP.NET and Macromedia Dreamweaver, all published under the Wrox
brand. He is also one of the top contributors to the Wrox Community Forum at p2p.wrox.com,
where he shares his knowledge with fellow programmers.
Imar has received Microsoft’s Most Valuable Professional (MVP) award each year since 2008 for
his contributions to the ASP.NET community. In early 2012, Imar joined the ASPInsiders, a small
group of international professionals that provide feedback and direction on new features for future

versions of ASP.NET.
Imar lives in Utrecht, the Netherlands, with his girlfriend Fleur and his son Niek. You can contact
him through his personal website at or by e-mail at


www.it-ebooks.info

ffirs.indd 9

25-02-2014 10:44:36


www.it-ebooks.info

ffirs.indd 10

25-02-2014 10:44:36


Credits
Acquisitions Editor

Vice President and Executive Group
Publisher

Mary James

Richard Swadley

Project Editor

Brian Herrmann

Associate Publisher
Jim Minatel

Technical Editor
Jason Gaylord

Project Coordinator, Cover
Katie Crocker

Production Editor
Daniel Scribner

Proofreaders
Josh Chase, Louise Watson, Sarah Kaikini,
Word One

Editorial Manager
Mary Beth Wakefield

Associate Director of Marketing

Indexer

David Mayhew

Robert Swanson

Marketing Manager


Cover Designer

Ashley Zurcher

Ryan Sneed

Business Manager

Cover Image

Amy Knies

© Nevin Giesbrecht / iStockPhoto

www.it-ebooks.info

ffirs.indd 11

25-02-2014 10:44:36


www.it-ebooks.info

ffirs.indd 12

25-02-2014 10:44:36


Acknowledgments


Although the jump  in version number by only 0.1 seems to suggest that there’s not much new

in ASP.NET 4.5.1 or Visual Studio 2013, you’d be surprised at the number of changes — ​small
and large — ​that made their way into these products. I spent the past couple of months working
on updating this book from the .NET 4.5 release to the new .NET 4.5.1 release. I discovered new
features and functionality every day. Some of those changes are really small, but could mean a
boost in productivity on a day-to-day basis. Others are much bigger and affect the way you build
or deploy your websites. I tried to incorporate all the new features found in ASP.NET and Visual
Studio that will make sense for you, someone with no or limited experience with ASP.NET.
I have also made a lot of changes to the book based on reader feedback. Just as with the previous
versions of the book, I went over all the errata that have been submitted as well as over the hundreds
of forum posts that were made, identifying areas in the book that readers had difficulties with,
and finding ways to improve it. If you have the previous edition and posted a question in the Wrox
forums: thanks for your valuable feedback; you’ve really helped to make this book better.
Besides my readers, I owe a lot to other people who helped me write this book.
First of all, a big thanks goes out to Brian Herrmann for his editorial work. Once again, it was a
pleasure to work with you! I also want to thank Jason Gaylord for his many useful suggestions he
provided as a technical editor. Both of you really helped shape this book. Many thanks also to the
people from Wrox for their support and contributions to this book.
Another person I owe a lot to is my friend Anne Ward from Blue Violet, a UK-based web and
graphic design company. Anne has done most of the designs used in this book and I highly
appreciate her input. Thanks again, Anne! The concert pictures you see in this book come from
Nigel D. Nudds, who kindly let me use pictures from his collection.
Finally, I would like to thank my lovely girlfriend Fleur for her support during this project.
I couldn’t have done it without you!

www.it-ebooks.info

ffirs.indd 13


25-02-2014 10:44:36


www.it-ebooks.info

ffirs.indd 14

25-02-2014 10:44:36


Contents

Foreword

xxv

Introduction

xxvii

Chapter 1: Getting Started with ASP.NET 4.5.1

Microsoft Visual Studio Express for Web
Getting Visual Studio
Installing Visual Studio Express for Web (VSEW)

Creating Your First ASP.NET 4.5.1 Website
An Introduction to ASP.NET 4.5.1
Understanding HTML

A First Look at ASP.NET Markup

A Tour of the IDE

1

2
3
3

5
9
10
15

16

The Main Development Area
Informational Windows

Customizing the IDE

16
22

23

Rearranging Windows
Modifying the Toolbox
Customizing the Document Window

Customizing Toolbars
Customizing Keyboard Shortcuts
Resetting Your Changes

23
24
26
27
28
28

The Sample Application
29
Practical Tips on Visual Studio
30
Summary31
Chapter 2: Building an ASP.NET Website

33

Creating Websites with Visual Studio 2013

34

Different Project Types
Choosing the Right Website Template
Creating and Opening a New Website

Working with Files in Your Website
The Many File Types of an ASP.NET Website

Adding Existing Files
Organizing Your Site
Special File Types

Working with Web Forms

34
35
37

41
41
44
45
46

47

www.it-ebooks.info

ftoc.indd 15

25-02-2014 10:45:05


CONTENTS

The Different Views on Web Forms
Choosing between Code Behind and Pages with Inline Code
Adding Markup to Your Page

Connecting Pages

47
49
54
59

Practical Tips on Working with Web Forms
61
Summary61
Chapter 3: Designing Your Web Pages

Why Do You Need CSS?
Problems with Using HTML Formatting
How CSS Fixes Formatting Problems

An Introduction to CSS

63

64
64
65

65

CSS — The Language
The Style Sheet
Adding CSS to Your Pages


Working with CSS in Visual Studio
Using the CSS Editor
Creating Embedded and Inline Style Sheets
Applying Styles
Managing Styles

69
70
82

84
85
89
93
94

Practical Tips on Working with CSS
97
Summary97
Chapter 4: Working with ASP.NET Server Controls

Introduction to Server Controls
A Closer Look at ASP.NET Server Controls
Defining Controls in Your Pages
Common Properties for All Controls

Types of Controls

101


102
106
106
106

109

Standard Controls
109
HTML Controls
122
Data Controls
123
Validation Controls
123
Navigation Controls
123
Login Controls
123
Ajax Extensions
124
WebParts124
Dynamic Data
124

The ASP.NET State Engine
What Is State and Why Is It Important?

124
124


xvi

www.it-ebooks.info

ftoc.indd 16

25-02-2014 10:45:05


CONTENTS

How the State Engine Works
Not All Controls Rely on View State
A Note about View State and Performance

125
129
130

Practical Tips on Working with Controls
131
Summary132
Chapter 5: Programming Your ASP.NET Web Pages

Introduction to Programming
Data Types and Variables
Converting and Casting Data Types
Using Arrays and Collections


135

136
137
140
142

Statements147
Operators147
Making Decisions
155
Loops162

Organizing Code

167

Methods: Functions and Subroutines
The App_Code Folder
Organizing Code with Namespaces
Writing Comments

Object Orientation Basics

167
169
173
176

178


Important OO Terminology
178
Events190

Practical Tips on Programming
191
Summary192
Chapter 6: Creating Consistent Looking Websites

Consistent Page Layout with Master Pages
Creating Master Pages
Creating Content Pages

195

196
198
200

Using a Centralized Base Page
An Introduction to the ASP.NET Page Life Cycle
Implementing the Base Page
Creating Reusable Page Templates

206
207
208
212


Themes216
Different Types of Themes
Choosing Between Theme and StyleSheetTheme
Applying Themes
Extending Themes
Dynamically Switching Themes

217
217
218
222
224
xvii

www.it-ebooks.info

ftoc.indd 17

25-02-2014 10:45:05


CONTENTS

Skins232
Creating a Skin File
Named Skins
Disable Theming for Specific Controls

233
234

235

Practical Tips on Creating Consistent Pages
235
Summary236
Chapter 7: Navigation

239

Different Ways to Move Around Your Site

240

Understanding Absolute and Relative URLs
Understanding Default Documents

240
243

Using the Navigation Controls
Architecture of the Navigation Controls
Examining the Web.sitemap File
Using the Menu Control
Using the TreeView Control
Using the SiteMapPath Control

243
244
244
246

254
258

Routing260
Setting up Extension-less URLs
Considerations for Extension-less URLs

Programmatic Redirection
Programmatically Redirecting the Client
  to a Different Page
Server-Side Redirects

261
263

263
264
266

Practical Tips on Navigation
268
Summary269
Chapter 8: User Controls

Introduction to User Controls
Creating User Controls
Adding User Controls to a Content Page or Master Page
Sitewide Registration of User Controls
User Control Caveats


Adding Logic to Your User Controls
Creating Your Own Data Types for Properties
Implementing View State Properties
View State Considerations

273

274
274
277
280
282

284
284
289
295

Practical Tips on User Controls
295
Summary296

xviii

www.it-ebooks.info

ftoc.indd 18

25-02-2014 10:45:05



CONTENTS

Chapter 9: Validating User Input

Gathering Data from the User
Validating User Input in Web Forms
Understanding Request Validation

Processing Data at the Server
Sending E-mail from Your Website
Reading from Text Files

299

300
301
320

321
321
327

Practical Tips on Validating Data
333
Summary333
Chapter 10: ASP.NET AJAX

Introducing Ajax
Using ASP.NET AJAX in Your Projects

Creating Flicker-free Pages
Providing Feedback to Users
The Timer Control

Using Web Services in Ajax Websites
What Are Web Services?
Creating Web Services

337

338
340
340
345
350

351
351
357

Practical Ajax Tips
365
Summary366
Chapter 11: jQuery

369

An Introduction to jQuery

370


Introducing NuGet
Choosing the Location for Your jQuery Reference
Different Ways to Include the jQuery Library

jQuery Syntax

371
375
376

380

jQuery Core
Selecting Items Using jQuery

Modifying the DOM with jQuery
CSS Methods
Handling Events
Miscellaneous jQuery Functionality
Common Mistakes When Working with jQuery

380
381

388
388
390
391
392


Effects with jQuery
393
jQuery and Validation
399
Practical Tips on jQuery
403
Summary403

xix

www.it-ebooks.info

ftoc.indd 19

25-02-2014 10:45:06


CONTENTS

Chapter 12: Introducing Databases

What Is a Database?
Different Kinds of Relational Databases
Installing SQL Server 2012 Express

Using SQL to Work with Database Data
Retrieving and Manipulating Data with SQL
Reading Data
Creating Data

Updating Data
Deleting Data

407

408
409
410

410
414
414
423
424
424

Creating Your Own Tables
Data Types in SQL Server
Understanding Primary Keys and Identities
Creating Relationships between Tables

427
427
429
432

Practical Database Tips
436
Summary436
Chapter 13: Displaying and Updating Data


Data Controls

439

439

Data-bound Controls
Data Source Controls
Other Data Controls

440
442
442

Data Source and Data-bound Controls Working Together
Displaying and Editing Data with GridView
Inserting Data with DetailsView
Storing Your Connection Strings in Web.config
Filtering Data

Customizing the Appearance of the Data Controls
Configuring Columns or Fields of Data-bound Controls

Updating and Inserting Data
Using DetailsView to Insert and Update Data

443
443
449

451
453

459
460

466
466

Practical Tips for Displaying and Updating Data
480
Summary481
Chapter 14: LINQ and the ADO.NET Entity Framework

Introducing LINQ

485

486

LINQ to Objects
LINQ to XML
LINQ to ADO.NET

487
487
487

xx


www.it-ebooks.info

ftoc.indd 20

25-02-2014 10:45:06


CONTENTS

Introducing the ADO.NET Entity Framework
Mapping Your Data Model to
  an Object Model
Introducing Query Syntax
Standard Query Operators
Shaping Data with Anonymous Types

Using Model Binding with LINQ Queries
Introducing Model Binding
A Note about Performance

488
489
495
495
499

504
505
535


Practical LINQ and ADO.NET Entity Framework Tips
536
Summary536
Chapter 15: Working with Data — ​Advanced Topics

539

Formatting Your Controls Using Styles

540

An Introduction to Styles
Combining Styles, Themes, and Skins

541
545

Handling Events

549

The ASP.NET Page and Control Life Cycles Revisited
The ASP.NET Page Life Cycle and Events in Data Controls
Handling Errors That Occur in the
  Data Source Controls

549
555
560


Hand-Coding Data Access Code
564
Caching574
Common Pitfalls with Caching Data
Different Ways to Cache Data in ASP.NET
  Web Applications

575
576

Practical Data Tips
585
Summary586
Chapter 16: Security in Your ASP.NET Website

Introducing Security

589

590

Identity: Who Are You?
Authentication: How Can You Prove Who You Are?
Authorization: What Are You Allowed to Do?
An Introduction to the ASP.NET Application Services

Introducing the Login Controls
The Login Controls
Configuring Your Web Application


The Role Manager

590
590
591
591

593
598
609

612

The Role Manager Configuration

613

xxi

www.it-ebooks.info

ftoc.indd 21

25-02-2014 10:45:06


CONTENTS

Managing Users with the WSAT
Configuring the Web Application to Work with Roles

Programmatically Checking Roles

613
618
622

Practical Security Tips
625
Summary626
Chapter 17: Personalizing Websites

629

Understanding Profile

630

Configuring the Profile
Using the Profile

631
637

Other Ways of Dealing with Profile
Anonymous Identification
Cleaning Up Old Anonymous Profiles
Looking at Other Users’ Profiles

655
655

655
657

Practical Personalization Tips
660
Summary660
Chapter 18: Exception Handling, Debugging,
and Tracing

Exception Handling

663

664

Different Types of Errors
Catching and Handling Exceptions
Global Error Handling and Custom Error Pages

The Basics of Debugging
Tools Support for Debugging

664
667
675

683
686

Moving Around in Debugged Code

Debugging Windows

686
687

Debugging Client-Side Script
Debugging with the Page Inspector

693
696

Introducing the Page Inspector
Using the Page Inspector

Cross-browser Testing with Browser Link
Introducing Browser Link
Using Browser Link

Tracing Your ASP.NET Web Pages
Using the Standard Tracing Capabilities
Adding Your Own Information to the Trace
Tracing and Performance
A Security Warning

696
697

701
702
702


704
705
708
710
710

Practical Debugging Tips
711
Summary712
xxii

www.it-ebooks.info

ftoc.indd 22

25-02-2014 10:45:06


CONTENTS

Chapter 19: Deploying Your Website

Preparing Your Website for Deployment

715

716

Avoiding Hard-Coded Settings

The Web.config File
Expression Syntax
The WebConfigurationManager Class

716
717
717
718

Introducing Bundling and Minification
Preparing for Deployment

723
727

Publishing Your Site
Introducing Web.config Transformations

Running Your Site Under IIS
Installing and Configuring the Web Server
Installing and Configuring ASP.NET
Understanding Security in IIS
NTFS Settings for Planet Wrox
Troubleshooting Web Server Errors

Moving Data to a Remote Server
Exporting Your Data to a File
Recreating the Database

727

730

734
735
737
741
742
745

747
748
750

The Deployment Checklist
751
What’s Next
753
Summary754
Appendix A: Exercise Answers

757

Appendix B: Configuring SQL Server 2012

781

Index

795


xxiii

www.it-ebooks.info

ftoc.indd 23

25-02-2014 10:45:06


×