28,99 €
The definitive programming guide to ASP.NET, by popular author and Microsoft MVP Imar Spaanjaars
Updated for ASP.NET 4, this introductory book retains its helpful examples and step-by-step format from the previous version and keeps the style of offering code examples written in both C# and Visual Basic. Beloved author and Microsoft ASP.NET MVP walks you through ASP.NET, Microsoft's technology for building dynamically generated Web pages from database content. You'll discover many improvements that ASP.NET 4 offers over the previous version, such as the ASP.NET MVC framework, Ajax improvements, jQuery support, and more.
You'll gradually build a Web site example that takes you through the processes of building basic ASP.NET Web pages, adding features with pre-built server controls, designing consistent pages, displaying data, and more.
Spaanjaars's distinct writing style puts you at ease with learning ASP.NET 4.
Das E-Book können Sie in Legimi-Apps oder einer beliebigen App lesen, die das folgende Format unterstützen:
Veröffentlichungsjahr: 2010
Cover
Title
Copyright
Dedication
About the Author
Credits
Acknowledgments
Foreword
Introduction
Who This Book Is For
What This Book Covers
How This Book Is Structured
What You Need to Use This Book
Conventions
Source Code
Errata
p2p.wrox.com
Chapter 1: Getting Started with ASP.NET 4
Microsoft Visual Web Developer
Creating Your First ASP.NET 4 Web Site
An Introduction to ASP.NET 4
A Tour of the IDE
Customizing the IDE
The Sample Application
Practical Tips on Visual Web Developer
Summary
EXERCISES
Chapter 2: Building an ASP.NET Web Site
Creating Web Sites with VWD 2010
Working with Files in Your Web Site
Working with Web Forms
Practical Tips on Working with Web Forms
Summary
EXERCISES
Chapter 3: Designing Your Web Pages
Why Do You Need CSS?
An Introduction to CSS
Working with CSS in Visual Web Developer
Practical Tips on Working with CSS
Summary
EXERCISES
Chapter 4: Working with ASP.NET Server Controls
Introduction to Server Controls
A Closer Look at ASP.NET Server Controls
Types of Controls
The ASP.NET State Engine
Practical Tips on Working with Controls
Summary
EXERCISES
Chapter 5: Programming Your ASP.NET Web Pages
Introduction to Programming
Data Types and Variables
Statements
Organizing Code
Object Orientation Basics
Practical Tips on Programming
Summary
EXERCISES
Chapter 6: Creating Consistent Looking Web Sites
Consistent Page Layout with Master Pages
Using a Centralized Base Page
Themes
Skins
Practical Tips on Creating Consistent Pages
Summary
EXERCISES
Chapter 7: Navigation
Different Ways to Move Around Your Site
Using the Navigation Controls
Programmatic Redirection
Practical Tips on Navigation
Summary
EXERCISES
Chapter 8: User Controls
Introduction to User Controls
Adding Logic to Your User Controls
Practical Tips on User Controls
Summary
EXERCISES
Chapter 9: Validating User Input
Gathering Data from the User
Processing Data at the Server
Practical Tips on Validating Data
Summary
EXERCISES
Chapter 10: ASP.NET AJAX
Introducing Ajax
Using ASP.NET AJAX in Your Projects
Using Web Services and Page Methods in Ajax Web Sites
Practical Ajax Tips
Summary
EXERCISES
Chapter 11: jQuery
An Introduction to jQuery
jQuery Syntax
Modifying the DOM with jQuery
Effects with jQuery
jQuery and Extensibility
Practical Tips on jQuery
Summary
EXERCISES
Chapter 12: Introducing Databases
What Is a Database?
Different Kinds of Relational Databases
Using SQL to Work with Database Data
Retrieving and Manipulating Data with SQL
Creating Your Own Tables
Practical Database Tips
Summary
EXERCISES
Chapter 13: Displaying and Updating Data
Data Controls
Data Source and Data-bound Controls Working Together
Customizing the Appearance of the Data Controls
Updating and Inserting Data
Practical Tips for Displaying and Updating Data
Summary
EXERCISES
Chapter 14: LINQ and the ADO.NET Entity Framework
Introducing LINQ
Introducing the ADO.NET Entity Framework
Mapping Your Data Model to an Object Model
Introducing Query Syntax
Using Server Controls with LINQ Queries
Practical LINQ and ADO.NET Entity Framework Tips
Summary
EXERCISES
Chapter 15: Working with Data — Advanced Topics
Formatting Your Controls Using Styles
Handling Events
Hand-Coding Data Access Code
Caching
Practical Data Tips
Summary
EXERCISES
Chapter 16: Security in Your ASP.NET 4 Web Site
Introducing Security
Introducing the Login Controls
The Role Manager
Practical Security Tips
Summary
EXERCISES
Chapter 17: Personalizing Web Sites
Understanding Profile
Other Ways of Dealing with Profile
Practical Personalization Tips
Summary
EXERCISES
Chapter 18: Exception Handling, Debugging, and Tracing
Exception Handling
The Basics of Debugging
Tools Support for Debugging
Debugging Client-Side Script
Tracing Your ASP.NET Web Pages
Practical Debugging Tips
Summary
EXERCISES
Chapter 19: Deploying Your Web Site
Preparing Your Web Site for Deployment
Copying Your Web Site
Running Your Site under IIS
Moving Data to a Remote Server
The Deployment Checklist
What’s Next
Summary
EXERCISES
Appendix A: Exercise Answers
Chapter 1
Chapter 2
Chapter 3
Chapter 4
Chapter 5
Chapter 6
Chapter 7
Chapter 8
Chapter 9
Chapter 10
Chapter 11
Chapter 12
Chapter 13
Chapter 14
Chapter 15
Chapter 16
Chapter 17
Chapter 18
Appendix B: Configuring SQL Server 2008
Configuring SQL Server 2008
Configuring Application Services
Index
Take your library wherever you go.
End User License Agreement
Cover
Table of Contents
Begin Reading
Introduction
Figure I-1
Figure I-2
Chapter 1: Getting Started with ASP.NET 4
Figure 1-1
Figure 1-2
Figure 1-3
Figure 1-4
Figure 1-5
Figure 1-6
Figure 1-7
Figure 1-8
Figure 1-9
Figure 1-10
Figure 1-11
Figure 1-12
Figure 1-13
Figure 1-14
Figure 1-15
Chapter 2: Building an ASP.NET Web Site
Figure 2-1
Figure 2-2
Figure 2-3
Figure 2-4
Figure 2-5
Figure 2-6
Figure 2-7
Figure 2-8
Figure 2-9
Figure 2-10
Figure 2-11
Figure 2-12
Figure 2-13
Figure 2-14
Figure 2-15
Figure 2-16
Figure 2-17
Figure 2-18
Figure 2-19
Figure 2-20
Figure 2-21
Figure 2-22
Figure 2-23
Figure 2-24
Figure 2-25
Figure 2-26
Chapter 3: Designing Your Web Pages
Figure 3-1
Figure 3-2
Figure 3-3
Figure 3-4
Figure 3-5
Figure 3-6
Figure 3-7
Figure 3-8
Figure 3-9
Figure 3-10
Figure 3-11
Figure 3-12
Figure 3-13
Figure 3-14
Figure 3-15
Figure 3-16
Figure 3-17
Figure 3-18
Figure 3-19
Figure 3-20
Figure 3-21
Figure 3-22
Figure 3-23
Figure 3-24
Figure 3-25
Figure 3-26
Figure 3-27
Chapter 4: Working with ASP.NET Server Controls
Figure 4-1
Figure 4-2
Figure 4-3
Figure 4-4
Figure 4-5
Figure 4-6
Figure 4-7
Figure 4-8
Figure 4-9
Figure 4-10
Figure 4-11
Figure 4-12
Figure 4-13
Figure 4-14
Figure 4-15
Figure 4-16
Chapter 5: Programming Your ASP.NET Web Pages
Figure 5-1
Figure 5-2
Figure 5-3
Figure 5-4
Figure 5-5
Figure 5-6
Figure 5-7
Figure 5-8
Figure 5-9
Figure 5-10
Chapter 6: Creating Consistent Looking Web Sites
Figure 6-1
Figure 6-2
Figure 6-3
Figure 6-4
Figure 6-5
Figure 6-6
Figure 6-7
Figure 6-8
Figure 6-9
Figure 6-10
Figure 6-11
Figure 6-12
Figure 6-13
Figure 6-14
Figure 6-15
Figure 6-16
Figure 6-17
Figure 6-18
Figure 6-19
Figure 6-20
Figure 6-21
Chapter 7: Navigation
Figure 7-1
Figure 7-2
Figure 7-3
Figure 7-4
Figure 7-5
Figure 7-6
Figure 7-7
Figure 7-8
Figure 7-9
Figure 7-10
Figure 7-11
Figure 7-12
Figure 7-13
Figure 7-14
Chapter 8: User Controls
Figure 8-1
Figure 8-2
Figure 8-3
Figure 8-4
Figure 8-5
Figure 8-6
Figure 8-7
Figure 8-8
Figure 8-9
Chapter 9: Validating User Input
Figure 9-1
Figure 9-2
Figure 9-3
Figure 9-4
Figure 9-5
Figure 9-6
Figure 9-7
Figure 9-8
Figure 9-9
Figure 9-10
Figure 9-11
Chapter 10: ASP.NET AJAX
Figure 10-1
Figure 10-2
Figure 10-3
Figure 10-4
Figure 10-5
Figure 10-6
Figure 10-7
Figure 10-8
Figure 10-9
Figure 10-10
Figure 10-11
Figure 10-12
Chapter 11: jQuery
Figure 11-1
Figure 11-2
Figure 11-3
Figure 11-4
Figure 11-5
Figure 11-6
Figure 11-7
Figure 11-8
Figure 11-9
Chapter 12: Introducing Databases
Figure 12-1
Figure 12-2
Figure 12-3
Figure 12-4
Figure 12-5
Figure 12-6
Figure 12-7
Figure 12-8
Figure 12-9
Figure 12-10
Figure 12-11
Figure 12-12
Figure 12-13
Figure 12-14
Figure 12-15
Figure 12-16
Figure 12-17
Figure 12-18
Figure 12-19
Figure 12-20
Figure 12-21
Figure 12-22
Figure 12-23
Figure 12-24
Figure 12-25
Figure 12-26
Chapter 13: Displaying and Updating Data
Figure 13-1
Figure 13-2
Figure 13-3
Figure 13-4
Figure 13-5
Figure 13-6
Figure 13-7
Figure 13-8
Figure 13-9
Figure 13-10
Figure 13-11
Figure 13-12
Figure 13-13
Figure 13-14
Figure 13-15
Figure 13-16
Figure 13-17
Figure 13-18
Figure 13-19
Figure 13-20
Figure 13-21
Figure 13-22
Figure 13-23
Figure 13-24
Chapter 14: LINQ and the ADO.NET Entity Framework
Figure 14-1
Figure 14-2
Figure 14-3
Figure 14-4
Figure 14-5
Figure 14-6
Figure 14-7
Figure 14-8
Figure 14-9
Figure 14-10
Figure 14-11
Figure 14-12
Figure 14-13
Figure 14-14
Figure 14-15
Figure 14-16
Figure 14-17
Figure 14-18
Figure 14-19
Figure 14-20
Chapter 15: Working with Data — Advanced Topics
Figure 15-1
Figure 15-2
Figure 15-3
Figure 15-4
Figure 15-5
Figure 15-6
Figure 15-7
Figure 15-8
Figure 15-9
Figure 15-10
Figure 15-11
Figure 15-12
Figure 15-13
Figure 15-14
Figure 15-15
Chapter 16: Security in Your ASP.NET 4 Web Site
Figure 16-1
Figure 16-2
Figure 16-3
Figure 16-4
Figure 16-5
Figure 16-6
Figure 16-7
Figure 16-8
Figure 16-9
Figure 16-10
Figure 16-11
Figure 16-12
Figure 16-13
Figure 16-14
Figure 16-15
Figure 16-16
Figure 16-17
Figure 16-18
Figure 16-19
Figure 16-20
Figure 16-21
Figure 16-22
Figure 16-23
Chapter 17: Personalizing Web Sites
Figure 17-1
Figure 17-2
Figure 17-3
Figure 17-4
Figure 17-5
Figure 17-6
Figure 17-7
Figure 17-8
Figure 17-9
Figure 17-10
Figure 17-11
Chapter 18: Exception Handling, Debugging, and Tracing
Figure 18-1
Figure 18-2
Figure 18-3
Figure 18-4
Figure 18-5
Figure 18-6
Figure 18-7
Figure 18-8
Figure 18-9
Figure 18-10
Figure 18-11
Figure 18-12
Figure 18-13
Figure 18-14
Figure 18-15
Figure 18-16
Figure 18-17
Figure 18-18
Figure 18-19
Figure 18-20
Figure 18-21
Figure 18-22
Figure 18-23
Figure 18-24
Figure 18-25
Figure 18-26
Chapter 19: Deploying Your Web Site
Figure 19-1
Figure 19-2
Figure 19-3
Figure 19-4
Figure 19-5
Figure 19-6
Figure 19-7
Figure 19-8
Figure 19-9
Figure 19-10
Figure 19-11
Figure 19-12
Figure 19-13
Figure 19-14
Figure 19-15
Figure 19-16
Figure 19-17
Appendix B: Configuring SQL Server 2008
Figure B-1
Figure B-2
Figure B-3
Figure B-4
Figure B-5
Figure B-6
Figure B-7
Figure B-8
Figure B-9
Figure B-10
C1
v
vi
vii
ix
xi
xiii
xv
xxv
xxvii
xxviii
xxix
xxx
xxxi
xxxii
xxxiii
xxxiv
xxxv
xxxvi
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
e1
Imar Spaanjaars
Beginning ASP.NET 4: in C# and VB
Published by
Wiley Publishing, Inc.
10475 Crosspoint Boulevard
Indianapolis, IN 46256
www.wiley.com
Copyright © 2010 by Wiley Publishing, Inc., Indianapolis, Indiana
Published simultaneously in Canada
ISBN: 978-0-470-50221-1
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 http://www.wiley.com/go/permissions.
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 also publishes its books in a variety of electronic formats. Some content that appears in print may not be available in electronic books.
Library of Congress Control Number: 2009943646
Trademarks: Wiley, the Wiley logo, Wrox, the Wrox logo, and Wrox Programmer to Programmer 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. Wiley Publishing, Inc. is not associated with any product or vendor mentioned in this book.
To my friends
Imar Spaanjaars graduated in Leisure Management at the Leisure Management School in the Netherlands, but he quickly changed his career path into the Internet world.
After working in the Internet business at various web agencies for the past twelve years, he recently started up his own company called De Vier Koeden (www.devierkoeden.nl), a small Internet agency specializing in consultancy and development of Internet and intranet applications with Microsoft technologies such as ASP.NET 4.
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.
In 2008 and 2009, Imar received Microsoft’s Most Valuable Professional (MVP) award for his contributions to the ASP.NET community.
Imar lives in Utrecht, the Netherlands, with his girlfriend, Fleur. You can contact him through his personal web site at http://imar.spaanjaars.com or by e-mail at [email protected].
Acquisitions Editor
Paul Reese
Project Editor
Brian Herrmann
Technical Editor
Michael J. Apostol
Production Editor
Rebecca Anderson
Copy Editor
Kim Cofer
Editorial Director
Robyn B. Siesky
Editorial Manager
Mary Beth Wakefield
Associate Director of Marketing
David Mayhew
Production Manager
Tim Tate
Vice President and Executive Group Publisher
Richard Swadley
Vice President and Executive Publisher
Barry Pruett
Associate Publisher
Jim Minatel
Project Coordinator, Cover
Lynsey Stanford
Compositor
Craig Johnson, Happenstance Type-O-Rama
Proofreader
Josh Chase, Word One New York
Indexer
Robert Swanson
Cover Designer
Michael E. Trent
Cover Image
© Nevin Giesbrecht/istockphoto
Just as with my prior books, I really enjoyed writing this one. The process from an empty Word document at the very beginning to a printed copy as the final result is a really interesting one. Along the way, I discovered, understood and used a lot of the new features that ASP.NET 4 and Visual Web Developer 2010 bring. Since this book is a major update on the previous edition — Beginning ASP.NET 3.5 in C# and VB — I have been able to incorporate a lot of reader feedback into this edition. While working on this book, I went through the more than 450 threads in the book’s forum, looking for feedback that could improve this edition. This allowed me to add clarification and more detailed instructions where needed. 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 I’d like to thank Brian Herrmann for his editorial work. Just as before, it was a pleasure to work with you! I also want to thank Michael J. Apostol for his work as a technical editor, and 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 new 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 good friends Joost and René and my lovely girlfriend Fleur for their support during this project.
This is a very nice book written by one of ASP.NET’s very own Most Valuable Professionals, Imar Spaanjaars. It takes a lot to be a Microsoft MVP — not only do you need expertise in the subject matter, you also need to be a great teacher and a technology philanthropist. Through his blogs, articles, and books, Imar has given a considerable amount of his time to benefit the web development community.
Imar has been active in the ASP.NET community for a long time and has written several books on web development, including the previous version of this book, Beginning ASP.NET 3.5. What makes this book so special is that it does not assume the reader has any prior knowledge of web development and at the same time it is a great book for anyone hoping to upgrade to ASP.NET 4. The book uses the free Visual Studio edition, Visual Web Developer Express 2010, starting from a chapter covering how to get started and then slowly introducing more advanced concepts in a seamless fashion.
In the process of helping create Visual Studio 2010 for Web Developers, I often interacted with community leaders to collect feedback on how we could make the product better for every web developer. Imar has been using ASP.NET 4 and VWD 2010 Express since their first Beta versions, and has been pouring in feedback on which bugs are important to fix and which features to emphasize. His passion for the technology is reflected in this book and I am positive that the readers of this book will feel that same passion.
ASP.NET 4 and Visual Studio 2010 have feature improvements in many areas, including building standards-compliant web sites, JScript IntelliSense, jQuery integration, Ajax, CSS improvements, HTML and markup snippets, Web Deployment, and data integration. I believe ASP.NET 4 and Visual Studio 2010 are great technologies to build fine web sites, and this book will be an excellent companion to these products, helping users excel in the world of web development.
—Vishal R. Joshi
Senior Program Manager Lead Microsoft Web Platform & Toolshttp://vishaljoshi.blogspot.com
To build effective and attractive database-driven web sites, you need two things: a solid and fast framework to run your web pages on and a rich and extensive environment to create and program these web pages. With ASP.NET 4 and Visual Web Developer 2010 you get both. Together they form the platform to create dynamic and interactive web sites.
ASP.NET 4 builds on top of its popular predecessors ASP.NET 2.0 and ASP.NET 3.5. While maintaining backward compatibility with sites built using these older versions, ASP.NET 4 and Visual Web Developer 2010 introduce new, exciting features and bring many smaller, but much needed changes to the framework and development tools.
With each new release of Visual Studio (which includes Visual Web Developer) since Visual Studio 2003, I am surprised by the sheer amount of new functionality and changes Microsoft has been able to put in the product. Visual Studio 2010 is no exception. A major new feature in Visual Studio 2010 is the full integration of the ADO.NET Entity Framework 4 that lets you work with databases with very little code. Another great change in Visual Studio is the use of Windows Presentation Foundation (WPF) for the User Interface which brings a better user experience and new behavior in Visual Studio itself.
Although not a new feature by itself, the inclusion of jQuery in Visual Web Developer is an excellent decision that will help you write fancier web sites in less time. jQuery is a compelling client side, cross-browser JavaScript framework and is discussed in detail in Chapter 11.
If you’re familiar with earlier versions of ASP.NET, you’ll be happy to find many small gems in the new version of the framework that will make your life as a developer easier. I’ll mention and discuss these new features throughout this book where appropriate. For a complete list of all new features in ASP.NET, check out the following white paper at the official ASP.NET web site:
http://www.asp.net/learn/whitepapers/aspnet4/
Probably the best thing about Visual Web Developer 2010 is its price: it’s still available for free. Although the commercial versions of Visual Studio 2010 ship with Visual Web Developer, you can also download and install the free Express edition. This makes Visual Web Developer 2010 and ASP.NET 4 probably the most attractive and compelling web development technologies available today.
This book is for anyone who wants to learn how to build rich and interactive web sites that run on the Microsoft platform. With the knowledge you gain from this book, you create a great foundation to build any type of web site, ranging from simple hobby-related web sites to sites you may be creating for commercial purposes.
Anyone new to web programming should be able to follow along because no prior background in web development is assumed, although it helps if you do have a basic understanding of HTML and the web in general. The book starts at the very beginning of web development by showing you how to obtain and install Visual Web Developer. The chapters that follow gradually introduce you to new technologies, building on top of the knowledge gained in the previous chapters.
Do you have a strong preference for Visual Basic over C# or the other way around? Or do you think both languages are equally cool? Or maybe you haven’t made up your mind yet and want to learn both languages? Either way, you’ll like this book because all code examples are presented in both languages!
Even if you have some experience with prior versions of ASP.NET, you may gain a lot from this book. Although many concepts from previous versions are brought forward into ASP.NET 4, you’ll discover there’s a lot of new stuff to be found in this book, including an introduction to the ADO.NET Entity Framework, the inclusion of jQuery, ASP.NET AJAX, the many changes to the ASP.NET 4 Framework, and much more.
This book teaches you how to create a feature-rich, data-driven, and interactive web site called Planet Wrox. Although this is quite a mouthful, you’ll find that with Visual Web Developer 2010, developing such a web site isn’t as hard as it seems. You’ll see the entire process of building a web site, from installing Visual Web Developer 2010 in Chapter 1 all the way up to putting your web site on a live server in Chapter 19. The book is divided into 19 chapters, each dealing with a specific subject.
Chapter 1, “Getting Started with ASP.NET 4.”
In this chapter you’ll see how to obtain and install Visual Web Developer 2010. You’ll get instructions for downloading and installing the free edition of Visual Web Developer 2010, called the Express edition. You are also introduced to HTML, the language behind every web page. The chapter closes with an overview of the customization options that Visual Web Developer gives you.
Chapter 2, “Building an ASP.NET Web Site.”
This chapter shows you how to create a new web site and how to add new elements like pages to it. Besides learning how to create a well-structured site, you also see how to use the numerous tools in Visual Web Developer to create HTML and ASP.NET pages.
Chapter 3, “Designing Your Web Pages.”
Visual Web Developer comes with a host of tools that enable you to create well-designed and attractive web pages. In this chapter, you see how to make good use of these tools. Additionally, you learn about CSS, the language that is used to format web pages.
Chapter 4, “Working with ASP.NET Server Controls.”
ASP.NET Server Controls are one of the most important concepts in ASP.NET. They enable you to create complex and feature-rich web sites with very little code. This chapter introduces you to the large number of server controls that are available, explains what they are used for, and shows you how to use them.
Chapter 5, “Programming Your ASP.NET Web Pages.”
Although the built-in CSS tools and the ASP.NET Server Controls can get you a long way in creating web pages, you are likely to use a programming language to enhance your pages. This chapter serves as an introduction to programming with a strong focus on programming web pages. Best of all: all the examples you see in this chapter (and the rest of the book) are in both Visual Basic and C#, so you can choose the language you like best.
Chapter 6, “Creating Consistent Looking Web Sites.”
Consistency is important to give your web site an attractive and professional appeal. ASP.NET helps you create consistent-looking pages through the use of master pages, which enable you to define the global look and feel of a page. Skins and themes help you to centralize the looks of controls and other visual elements in your site. You also see how to create a base page that helps to centralize programming code that you need on all pages in your site.
Chapter 7, “Navigation.”
To help your visitors find their way around your site, ASP.NET comes with a number of navigation controls. These controls are used to build the navigation structure of your site. They can be connected to your site’s central site map that defines the pages in your web site. You also learn how to programmatically send users from one page to another.
Chapter 8, “User Controls.”
User controls are reusable page fragments that can be used in multiple web pages. As such, they are great for repeating content such as menus, banners, and so on. In this chapter, you learn how to create and use user controls and enhance them with some programmatic intelligence.
Chapter 9, “Validating User Input.”
A large part of interactivity in your site is defined by the input of your users. This chapter shows you how to accept, validate, and process user input using ASP.NET Server Controls. Additionally, you see how to send e-mail from your ASP.NET web site and how to read from text files.
Chapter 10, “ASP.NET AJAX.
” Microsoft ASP.NET AJAX enables you to create good-looking, flicker-free web pages that close the gap between traditional desktop applications and web sites. In this chapter you learn how to use the built-in Ajax features to enhance the presence of your web pages, resulting in a smoother interaction with the web site.
Chapter 11, “jQuery.”
jQuery is a popular, open source and cross-browser JavaScript library designed to make it easier to interact with web pages in the client’s browser. In this chapter you learn the basics of jQuery and see how to add rich visual effects and animations to your web pages.
Chapter 12, “Introducing Databases.”
Understanding how to use a database is critical to building web sites, as most modern web sites require the use of a database. You’ll learn the basics of SQL, the query language that enables you to access and alter data in a database. In addition, you are introduced to the database tools found in Visual Web Developer that help you create and manage your SQL Server databases.
Chapter 13, “Displaying and Updating Data.”
Building on the knowledge you gained in Chapter 12, this chapter shows you how to use the ASP.NET data-bound and data source controls to create a rich interface that enables your users to interact with the data in the database that these controls target.
Chapter 14, “LINQ and the ADO.NET Entity Framework.”
LINQ is Microsoft’s solution for accessing objects, databases, XML, and more. The ADO.NET Entity Framework (EF) is Microsoft’s new technology for database access. This chapter shows you what LINQ is all about, how to use the visual EF designer built into Visual Studio, and how to write LINQ to EF queries to get data in and out of your SQL Server database.
Chapter 15, “Working with Data — Advanced Topics.”
While earlier chapters focused mostly on the technical foundations of working with data, this chapter looks at the same topic from a front-end perspective. You see how to change the visual appearance of your data through the use of control styles. You also see how to interact with the data-bound controls and how to speed up your web site by keeping a local copy of frequently accessed data.
Chapter 16, “Security in Your ASP.NET 4 Web Site.”
Although presented quite late in the book, security is a first-class, important topic. This chapter shows you how to make use of the built-in ASP.NET features related to security. You learn about a number of application services that facilitate security. You also learn how to let users sign up for an account on your web site, how to distinguish between anonymous and logged-on users, and how to manage the users in your system.
Chapter 17, “Personalizing Web Sites.”
Building on the security features introduced in Chapter 16, this chapter shows you how to create personalized web pages with content targeted at individual users. You see how to configure and use ASP.NET Profile that enables you to store personalized data for known and anonymous visitors.
Chapter 18, “Exception Handling, Debugging, and Tracing.”
In order to understand, improve, and fix the code you write for your ASP.NET web pages you need good debugging tools. Visual Web Developer ships with great debugging support that enables you to diagnose the state of your application at runtime, helping you find and fix problems before your users do.
Chapter 19, “Deploying Your Web Site.”
By the end of the book, you should have a web site that is ready to be shown to the world. But how exactly do you do that? What are the things you need to know and understand to put your web site out in the wild? This chapter gives the answers and provides you with a good look at configuring different production systems in order to run your final web site.
This book takes the time to explain concepts step by step using working examples and detailed explanations. Using the famous Wrox Try It Out and How It Works sections, you are guided through a task step by step, detailing important things as you progress through the task. Each Try It Out task is followed by a detailed How It Works section that explains the steps you performed in the exercise.
At the end of each chapter, you find exercises that help you test the knowledge you gained in this chapter. You’ll find the answers to each question in Appendix A at the end of this book. Don’t worry if you don’t know all the answers to the questions. Later chapters do not assume you followed and carried out the tasks from the exercise sections of previous chapters.
Since this is a beginner’s book, I can’t go into great detail on a number of topics. For pretty much each chapter in this book, you’ll easily find numerous other books that exclusively deal with the topic discussed. Where appropriate, I have included references to these books so you can easily decide where to go to next if you want to deepen your knowledge on a specific subject.
This book assumes you have a system that meets the following requirements:
Capable of running Visual Web Developer. For the exact system requirements, consult the readme file that comes with the software.
Running Windows Vista or Windows 7 (both require at least the Home Premium edition), or one of the Windows Server 2008 editions.
Although you should be able to follow along with most of the exercises using another version of Windows such as Windows XP (as long as it’s supported by Visual Web Developer), the exercises in Chapter 19 on deployment require the use of Microsoft’s web server IIS 7 or later, which only ships with the Windows versions in the requirements list.
Chapter 1 shows you how to obtain and install Visual Web Developer 2010, which in turn installs the Microsoft .NET Framework version 4 and SQL Server 2008 Express edition; all you need is a good operating system and the drive to read this book!
Service Pack 1 for Visual Web Developer 2010 Express and Visual Studio 2010 introduces a change in the way the Visual Basic editor writes methods for you in the Code Behind of your pages. The editor no longer adds the ByVal keyword to new methods it creates. Since ByVal was already the default for method parameters, this change doesn’t affect your code. When you have VS 2010 Service Pack 1 installed and you see ByVal used in the book, you can choose to type that keyword as shown in the book, or you can leave out the ByVal keyword as VWD 2010 / VS 2010 do when writing new methods. For a detailed explanation of the issue, check out this forum post:http://p2p.wrox.com/book-beginning-asp-net-4-c-vb/83002-you-using-service-pack-1-visual-web-developer-2010-express-vs-2010-a.html
To help you get the most from the text and keep track of what’s happening, a number of conventions are used throughout the book.
The Try It Out is an exercise you should work through, following the text in the book.
1.
They usually consist of a set of steps.
2.
Each step has a number.
3.
Follow the steps through with your copy of the code.
4.
Then read the How It Works section to find out what’s going on.
After each Try It Out, the actions you carried out and the code you’ve typed in will be explained in detail.
NOTE Boxes like this one hold important, not-to-be forgotten information that is directly relevant to the surrounding text.
COMMON MISTAKES Mistakes that are easily made while following the exercises are presented in a box like this. Be sure to read these carefully when you get stuck in an exercise.
As for styles in the text:
New terms and important words are
italicized
when they are introduced.
Code within the text is presented like this:
Request.QueryString.Get("Id")
URLs that do not start with
www
are prefixed with
http://
to make it clear it’s an Internet address. URLs within the text are presented like this:
http://imar.spaanjaars.com
.
You’ll see many URLs that start with
tinyurl.com
which is a handy, online service to make URLs shorter (and thus easier to type). Entering a
tinyurl.com
address in your browser should take you to its final destination.
Menu items that require you to click multiple submenus have a special symbol that looks like this: ⇒. For example: File ⇒ New ⇒ Folder.
Code or content irrelevant to the discussion is either left out completely or replaced with ellipsis points (three dots), like this:
<tr>
<td style="white-space: nowrap;">
... Menu items go here; not shown
</td>
</tr>
The three dots are used regardless of the programming language used in the example, so you’ll see it for C#, Visual Basic, HTML, CSS, and JavaScript. When you see it in code you’re instructed to type into the code editor, you can simply skip the three dots and anything that follows them on the same line.
Code shown for the first time, or other relevant code, is in the following format:
Dim roles As New ArrayList()
roles.Add("Administrators")
roles.Add("ContentManagers")
To put emphasis on a block of code surrounded by other code, I used a bolded font like this:
<appSettings>
<add key="FromAddress" value="[email protected]"/>
</appSettings>
The surrounding code is used to make it easier to see where the bolded code should be placed.
Quite often, white space in code is irrelevant, as is mostly the case with ASP.NET markup and HTML. To fit code within the boundaries of this book, I often wrap code over multiple lines and indent the part that should have been on the previous line like this:
<!DOCTYPE html PUBLIC"-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
If you’re typing this code yourself, you can put it all on one line, or use the same line breaks if you prefer.
Text that appears on screen often has Each Word Start With A Capital Letter, even though the original screen text uses a different capitalization. This is done to make the screen text stand out from the rest of the text.
As you work through the examples in this book, you may choose either to type in all the code manually or to use the source code files that accompany the book. All of the source code used in this book is available for download from the book’s own page on the Wrox web site at www.wrox.com/go/beginningaspnet4. If somehow this link no longer works, go to www.wrox.com and locate the book either by using the Search box or by using one of the title lists. Click the Download Code link on the book’s detail page to obtain all the source code for the book.
Because many books have similar titles, you may find it easiest to search by ISBN; for this book the ISBN is 978-0-470-50221-1.
You can download the full source for this book as a single file for each programming language used in the book (C# or Visual Basic). You can decompress these files with your favorite decompression tool. If you extract the source, make sure you maintain the original folder structure that is part of the code download. The different decompression tools use different names for this feature, but look for a feature like Use Folder Names or Maintain Directory Structure. Once you have extracted the files from the code download, you should end up with a folder called Source and a folder called Resources. Then create a new folder in the root of your C drive, call it BegASPNET, and move the Source and Resources folders into this new folder so you end up with folders like C:\BegASPNET\Source and C:\BegASPNET\Resources. The Source folder contains the source for each of the 19 chapters of this book and the final version of the Planet Wrox web site. The Resources folder contains files you need during some of the exercises in this book. If everything turned out correctly, you should end up with the structure shown in Figure I-1.
Figure I-1
Later chapters have you create folders called Site and Release inside the same C:\BegASPNET folder giving you a folder structure similar to that in Figure I-2.
The Site folder contains the site as you’ll build it throughout this book, while the Release folder will contain your final version at the end of this book. Whenever you’re stuck with some examples in this book, you can take a peek in the Source folder to see how things should have ended up.
If you want to run the site for a specific chapter to see how it works, be sure to open the chapter’s folder in Visual Web Developer as a web site. So you should open a folder such as C:\BegASPNET\Source\Chapter 12 directly rather than opening its parent folder C:\BegASPNET\Source.
Figure I-2
If you want to follow along in both programming languages, create a second folder called C:\BegASPNETVB to hold the files for the Visual Basic version. This way, the two sites can coexist without any problems. If you create a folder specifically for the C# language, don’t include the hash symbol (#) as that’s an invalid character in the path name for a web site.
Sticking to this structure ensures a smooth execution of the Try It Out exercises in this book. Incorrectly mixing or nesting these folders make it harder to carry out the exercises and may even lead to unexpected situations and errors. Whenever you run into an issue or error that is not explained in this book, ensure that your site structure is still closely related to the one presented here.
I have made every effort to ensure that there are no errors in the text or in the code. However, no one is perfect, and mistakes do occur. If you find an error in this book, such as a spelling mistake or a faulty piece of code, I’d be very grateful for your feedback. By sending in errata you may save another reader hours of frustration and at the same time you will be helping me provide even higher quality information.
To find the errata page for this book, go to www.wrox.com/go/beginningaspnet4 or go to www.wrox.com and locate the title using the Search box or one of the title lists. Then, on the book details page, click the Errata link. On this page you can view all errata that has been submitted for this book and posted by Wrox editors. A complete book list including links to each book’s errata is also available at www.wrox.com/misc-pages/booklist.shtml.
If you don’t spot “your” error on the book’s Errata page, go to www.wrox.com/contact/techsupport.shtml and complete the form there to send us the error you have found. I’ll check the information and, if appropriate, post a message to the book’s errata page and fix the problem in subsequent editions of the book.
For author and peer discussion, join the P2P forums at p2p.wrox.com. The forums are a web-based system for you to post messages relating to Wrox books and related technologies and interact with other readers and technology users. The forums offer a subscription feature to e-mail you topics of interest of your choosing when new posts are made to the forums. I am a frequent visitor of the Wrox forums, and I’ll do my best to help you with any questions you may have about this book.
At p2p.wrox.com you will find a number of different forums that will help you not only as you read this book, but also as you develop your own applications. To join the forums, just follow these steps:
1.
Go to
p2p.wrox.com
and click the Register Now link.
2.
Read the terms of use and click Agree.
3.
Complete the required information to join as well as any optional information you wish to provide and click Submit.
4.
You will receive an e-mail with information describing how to verify your account and complete the joining process.
You can read messages in the forums without joining P2P but in order to post your own messages, you must join (which is free).
After you join, you can post new messages and respond to messages other users post. You’ll find this book’s own forum under the ASP.NET 4 category that is available from the homepage. You can read messages at any time on the Web. If you would like to have new messages from a particular forum e-mailed to you, click the Subscribe to this Forum icon by the forum name in the forum listing.
For more information about how to use the Wrox P2P, be sure to read the P2P FAQs for answers to questions about how the forum software works as well as many common questions specific to P2P and Wrox books. To read the FAQs, click the FAQ link on any P2P page.
What you will learn in this chapter:
How to acquire and install Visual Web Developer 2010 Express and Visual Studio 2010
How to create your first web site with Visual Web Developer
How an ASP.NET page is processed by the server and sent to the browser
How you can use and customize the development environment
Ever since the first release of the .NET Framework 1.0 in early 2002, Microsoft has put a lot of effort and development time into ASP.NET, the part of the .NET Framework that enables you to build rich web applications. This first release meant a radical change from the older Microsoft technology to build web sites called Active Server Pages (ASP), now often referred to as classic ASP. The introduction of ASP.NET 1.0 and the associated Visual Studio .NET 2002 gave developers the following benefits over classic ASP:
A clean separation between presentation and code. With classic ASP, your programming logic was often scattered throughout the
HTML
of the page, making it hard to make changes to the page later.
A development model that was much closer to the way desktop applications are programmed. This made it easier for the many Visual Basic desktop programmers to make the switch to web applications.
A feature-rich development tool (called Visual Studio .NET) that allowed developers to create and code their web applications visually.
A choice between a number of
object-oriented programming
languages, of which Visual Basic .NET and C# (pronounced as C-Sharp) are now the most popular.
Access to the entire .NET Framework, which for the first time meant that web developers had a unified and easy way to access many advanced features to work with databases, files, e-mail, networking tools, and much more.
Despite the many advantages of ASP.NET over the older model, using ASP.NET also meant an increase of complexity and the knowledge you needed to build applications with it, making it harder for many new programmers to get started with ASP.NET.
After the initial release in 2002, Microsoft released another version of the .NET Framework (called .NET 1.1) and the development IDE Visual Studio .NET in 2003. Many people saw this as a service pack for the initial release, although it also brought a lot of new enhancements in both the framework and the development tools.
In November 2005, Visual Studio 2005 and ASP.NET 2.0 were released. To the pleasant surprise of many developers around the world, Microsoft had again been able to drastically improve and expand the product, adding many features and tools that helped reduce the complexity that was introduced with ASP.NET 1.0. New wizards and smart controls made it possible to reduce the code required to build an application, decreasing the learning curve for new developers and increasing their productivity.
Although Visual Studio 2005 and ASP.NET 2.0 were already very feature rich, Microsoft managed again to add a whole bunch of cool new features in Visual Studio 2008 and ASP.NET 3.5, which were released in November 2007. Major new functionality included LINQ (discussed in Chapter 14) and the integration of the AJAX Framework (which you learn more about in Chapter 10). In August 2008 Microsoft released Service Pack 1 for Visual Studio and the .NET Framework, introducing major new features like the ADO.NET Entity Framework (discussed in Chapter 14) and Dynamic Data.
The current versions, Visual Studio 2010 (often pronounced as “twenty-ten”) and ASP.NET 4, build on top of the successful Visual Studio 2008 and ASP.NET 3.5 releases, leaving many of the beloved features in place, while adding new features and tools in other areas.
Over the next 19 chapters, you learn how to build full-featured ASP.NET web sites using Visual Web Developer 2010, Microsoft’s development tool for ASP.NET web applications, which is part of the full Visual Studio 2010 suite. This book guides you through the process of creating a fully functional, database-driven web site, starting with a bare-bones web site in this chapter, all the way down to the deployment of it to a production environment in Chapter 19.