Tải bản đầy đủ (.pdf) (1,444 trang)

professional asp.net 4.5 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 (45.4 MB, 1,444 trang )

www.it-ebooks.info
ffirs.indd iffirs.indd i 4/10/2013 9:08:25 PM4/10/2013 9:08:25 PM
www.it-ebooks.info
PROFESSIONAL ASP.NET 4.5 IN C# AND VB
INTRODUCTION . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
xliii
 PART I ASP.NET FUNDAMENTALS
CHAPTER 1
One ASP.NET . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
CHAPTER 2 HTML5 and CSS3 Design with ASP.NET . . . . . . . . . . . . . . . . . . . . . . . . . . .17
CHAPTER 3
ASP.NET Web Forms Structure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53
PART II CONTROLS
CHAPTER 4
ASP.NET Server Controls and Client-Side Scripts . . . . . . . . . . . . . . . . . 103
CHAPTER 5
ASP.NET Web Server Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 123
CHAPTER 6
Validation Server Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 199
CHAPTER 7
User and Server Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 231
 PART III DATA ACCESS
CHAPTER 8
Data Binding . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 299
CHAPTER 9
Model Binding . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 355
CHAPTER 10
Querying with LINQ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .371
CHAPTER 11
Entity Framework . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 411
CHAPTER 12


ASP.NET Dynamic Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 443
CHAPTER 13
Working with Services . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 455
PART IV PROVIDERS
CHAPTER 14
Introduction to the Provider Model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .529
CHAPTER 15
Extending the Provider Model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 559
 PART V ASP.NET FEATURES
CHAPTER 16
Working with Master Pages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 591
CHAPTER 17
Site Navigation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 621
CHAPTER 18
Personalization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 671
CHAPTER 19
Membership and Role Management . . . . . . . . . . . . . . . . . . . . . . . . . . . . 701
CHAPTER 20
Security . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 759
Continues
ffirs.indd iffirs.indd i 4/10/2013 9:08:25 PM4/10/2013 9:08:25 PM
www.it-ebooks.info
 PART VI APPLICATION STATE
CHAPTER 21 State Management . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 791
CHAPTER 22 Caching . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 823
 PART VII CLIENT-SIDE DEVELOPMENT
CHAPTER 23 ASP.NET AJAX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 857
CHAPTER 24 Ajax Control Toolkit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 895
CHAPTER 25 jQuery. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 957
CHAPTER 26 Real-Time Communication . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 983

CHAPTER 27 Developing Websites with Mobile in Mind . . . . . . . . . . . . . . . . . . . . . . 1001
 PART VIII APPLICATION CONFIGURATION AND DEPLOYMENT
CHAPTER 28 Confi guration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1027
CHAPTER 29 Debugging and Error Handling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1075
CHAPTER 30 Modules and Handlers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .1105
CHAPTER 31 Asynchronous Communication . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1121
CHAPTER 32 Building Global Applications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .1135
CHAPTER 33 Packaging and Deploying ASP.NET Applications . . . . . . . . . . . . . . . . .1163
 PART IX ADDITIONAL ASP.NET TECHNOLOGIES
CHAPTER 34 ASP.NET MVC . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .1187
CHAPTER 35 ASP.NET Web Pages and Razor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .1215
 PART X APPENDIXES
APPENDIX A Migrating Older ASP.NET Projects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1239
APPENDIX B COM Integration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1247
APPENDIX C ASP.NET Ultimate Tools . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1257
APPENDIX D Administration and Management . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1269
APPENDIX E Dynamic Types and Languages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1295
APPENDIX F ASP.NET Online Resources . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1305
APPENDIX G Visual Studio Extensibility with NuGet . . . . . . . . . . . . . . . . . . . . . . . . . . 1309
INDEX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1333
ffirs.indd iiffirs.indd ii 4/10/2013 9:08:26 PM4/10/2013 9:08:26 PM
www.it-ebooks.info
PROFESSIONAL
ASP.NET 4.5 in C# and VB
ffirs.indd iiiffirs.indd iii 4/10/2013 9:08:26 PM4/10/2013 9:08:26 PM
www.it-ebooks.info
ffirs.indd ivffirs.indd iv 4/10/2013 9:08:26 PM4/10/2013 9:08:26 PM
www.it-ebooks.info
PROFESSIONAL
ASP.NET 4.5 in C# and VB

Jason N. Gaylord
Christian Wenz
Pranav Rastogi
Todd Miranda
Scott Hanselman
ffirs.indd vffirs.indd v 4/10/2013 9:08:26 PM4/10/2013 9:08:26 PM
www.it-ebooks.info
Professional ASP.NET 4.5 in C# and VB
Published by
John Wiley & Sons, Inc.
10475 Crosspoint Boulevard
Indianapolis, IN 46256
www.wiley.com
Copyright © 2013 by John Wiley & Sons, Inc., Indianapolis, Indiana
Published simultaneously in Canada
ISBN: 978-1-118-31182-0
ISBN: 978-1-118-42438-4 (ebk)
ISBN: 978-1-118-33205-4 (ebk)
ISBN: 978-1-118-33534-5 (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 speci cally disclaim all warranties, including
without limitation warranties of  tness for a particular purpose. No warranty may be created or extended by sales or pro-

motional 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 pub-
lisher 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 stan-
dard 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://book
support.wiley.com. For more information about Wiley products, visit www.wiley.com.
Library of Congress Control Number: 2013933610
Trademarks: Wiley, Wrox, the Wrox logo, Wrox Programmer to Programmer, and related trade dress are trademarks or
registered trademarks of John Wiley & Sons, Inc. and/or its af liates, 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.
ffirs.indd viffirs.indd vi 4/10/2013 9:08:26 PM4/10/2013 9:08:26 PM
www.it-ebooks.info
To my loving wife, Lisa, and our four awesome
children.
—J N. G
I would like to dedicate this book with the following
quote, to my mother who has been instrumental in my
upbringing and is an integral part of my life. Abraham
Lincoln said, “All that I am, or hope to be, I owe to
my angel mother.”
—P R

To Kelly, Amber, and Sydney
—T M
To my wife and the two little boys I work for.
—S H
ffirs.indd viiffirs.indd vii 4/10/2013 9:08:27 PM4/10/2013 9:08:27 PM
www.it-ebooks.info
ffirs.indd viiiffirs.indd viii 4/10/2013 9:08:27 PM4/10/2013 9:08:27 PM
www.it-ebooks.info
ABOUT THE AUTHORS
JASON N. GAYLORD is an application developer and Director of Technology at United
One Resources in Wilkes-Barre, Pennsylvania. His programming career started with a
simple GOTO statement and continued over 15 years of developing Windows and web
applications. During this time, Jason has had the opportunity to plan, build, test, and
deploy several enterprise applications including integrations with accounting and operating
systems and B2B web applications used by some of the top banks in the United States.
Outside of the normal day to day activities, Jason blogs, speaks, and organizes technology
events including TECHbash and NEPA GiveCamp. He’s the group leader for the .NET
Valley technology user group. Recently, Jason was recognized as a Microsoft MVP for 10 consecutive years.
He remains a member of the ASPInsiders, a group that provides product feedback directly to the ASP.NET
team. You can  nd out more about Jason by visiting his website at
jasongaylord.com. You can follow him
on Twitter at @jgaylord.
CHRISTIAN WENZ started working almost exclusively with web technologies in 1993 and
has not looked back since. As a developer and project manager he has been responsible
for websites from medium–sized companies up to large, international enterprises. As an
author, he has written or co-written over 100 books that have been translated into 10 lan-
guages. As a consultant, he helped companies and teams of developers to make their appli-
cations more reliable, better performing, and more secure. Christian frequently writes for
various IT magazines, is a sought-after speaker at developer conferences around the world,
and is always keen on sharing technologies he is excited about with others. He contributes

to various open source projects, is a Microsoft ASP.NET MVP and an ASPInsider, and co-author of several
developer certi cations. You can follow him on Twitter at @chwenz.
PRANAV RASTOGI is a member of the Microsoft ASP.NET product team and is based in
Seattle. Over the past years, Pranav has worked on a variety of areas such as ASP.NET
Web Forms, Dynamic Data, and more recently ASP.NET SignalR. He is passionate about
understanding web developer woes with the Microsoft stack and helps champion tools,
packages, or libraries that will enhance the web development experience on the Microsoft
stack. Pranav is also a regular speaker on a variety of ASP.NET related topics at various
worldwide conferences. He blogs about his experiences at />pranav_rastogi/
. Before joining Microsoft, Pranav graduated from the University Of
Florida with a masters in Computer Science. 
Pranav is also a food junkie who sometimes works as a Chef de Cuisine at home. He is an adventure seeker
and can be often found backpacking in the wilderness. In this spare time Pranav regularly practices his bar-
tending skills on his friends. If you know anyone who is looking to throw a party and needs a bartender,
then get in touch with him. You can follow Pranav on Twitter at
@rustd.
ffirs.indd ixffirs.indd ix 4/10/2013 9:08:27 PM4/10/2013 9:08:27 PM
www.it-ebooks.info
TODD MIRANDA is an active proponent of .NET technologies and software craftsman-
ship as a whole. He has been developing on various platforms for over 20 years and has
been involved with .NET since it was previewed in 2000. He has worked with many of
the .NET platforms but focuses primarily on ASP.NET. He is the owner of NxtDimension
Solutions and provides consulting and training on the Microsoft stack. As a Microsoft
MVP, Todd works closely with Microsoft. He is the co-founder of the Birmingham .NET
User Group and remains active in the development community. He has recorded hundreds
of online training videos on Microsoft development topics including security, JavaScript,
Silverlight, WPF, Expression, and ASP.NET. He is an INETA speaker and regularly
presents at user groups, community events, and technical conferences. You can follow
Todd on Twitter @tmiranda.
SCOTT HANSELMAN is a web developer who has been blogging at http://hanselman

.com
for over a decade. He works on Azure and ASP.NET for Microsoft out of his home
of ce in Portland, Oregon. Scott has three podcasts,
for
tech talk, on developers’ lives and loves, and http://
ratchetandthegeek.com
for pop culture and tech media. He’s written a number of books
and spoken in person to almost a half million developers worldwide. You can follow Scott
on Twitter at @shanselman.
ABOUT THE TECHNICAL EDITORS
KEN COX has been creating web applications with Microsoft technologies since the earliest betas of ASP
Classic in the mid-1990s. He has worked on dozens of ASP.NET sites, large and small, and is the author
of ASP.NET 3.5 For Dummies published by John Wiley & Sons. Ken was honored as a Microsoft Most
Valuable Professional (MVP) for ASP.NET for 15 consecutive years prior to his retirement and remains an
ASPInsider. Ken does consulting work from his home in Nipissing Township, Ontario, Canada.
BIPIN JOSHI is an independent blogger and author who writes about apparently unrelated topics — yoga
and technology. A former software consultant and trainer by profession, Bipin has been programming
since 1995 and has worked with the .NET Framework since its inception. He is a published author and has
authored or co-authored more than a half dozen books and numerous articles on .NET technologies. Bipin
was a Microsoft Most Valuable Professional (MVP) and a Microsoft Certi ed Trainer (MCT) during his
tenure as a software consultant and trainer. He has also penned a few books on yoga. Having embraced the
yoga way of life, he enjoys the intoxicating presence of God and writes about yoga, life, and technology.
He can be reached at
www.bipinjoshi.com.
JOHN PETERSEN, an early adopter of the Microsoft .NET Platform, has over 20 years of experience archi-
tecting and developing software. Currently, John is a practice director in the Technology Platform Group at
Neudesic, LLC, a Microsoft National Systems Integrator and Gold ISV Partner and is a 10-timerecipient
of Microsoft’s Most Valuable Professional Award.John is a currentASP.NET/IIS MVP.John has spoken at
numerous industry events, including VSLive and DevConnections, as well as at many regional events and
Code Camps. John is also a regular contributor to CODE Magazine.

ffirs.indd xffirs.indd x 4/10/2013 9:08:27 PM4/10/2013 9:08:27 PM
www.it-ebooks.info
JEFFERY TAY has been developing and designing .NET solutions since 2002, specializing in solutions
for the education and medical sectors. He was awarded Microsoft Most Valuable Professional for the last
3 years. He has experience in Java and most Microsoft technologies such as Silverlight, SQL Server, and
Windows Server. He is an associate director at the National University of Singapore where he leads a team
that manages the university’s Learning Management System and comes up with new solutions where the use
of IT can enhance teaching. You can reach Jeffery at
DEEPAK VERMA has been developing and designing web solutions for the last 10 years. During these years,
Deepak has been involved in technologies such as Flash Scripting, Flex, ColdFusion, Silverlight, Java, and
PHP and now is mostly working on Microsoft stack. He is currently working as a Software Development
Engineer in Test for Microsoft. At Microsoft he has contributed to Visual Studio Dynamic data tooling,
multi-targeting, Razor editor and tooling, Azure SDK for Java, PHP & .NET, Visual Studio HTML and
CSS editors, Azure Mobile Services, and NuGet. He lives in Redmond, Washington, with his wife, Madhu,
and children, Adya and Kian. You can reach him at

ffirs.indd xiffirs.indd xi 4/10/2013 9:08:27 PM4/10/2013 9:08:27 PM
www.it-ebooks.info
ffirs.indd xiiffirs.indd xii 4/10/2013 9:08:27 PM4/10/2013 9:08:27 PM
www.it-ebooks.info
ACQUISITIONS EDITOR
Mary James
SENIOR PROJECT EDITOR
Kevin Kent
TECHNICAL EDITORS
Ken Cox
Bipin Joshi
John Petersen
Je ery Tay
Deepak Verma

SENIOR PRODUCTION EDITOR
Kathleen Wisor
COPY EDITORS
Kim Cofer
Kezia Endsley
EDITORIAL MANAGER
Mary Beth Wakefi eld
FREELANCER EDITORIAL MANAGER
Rosemarie Graham
ASSOCIATE DIRECTOR OF MARKETING
David Mayhew
MARKETING MANAGER
Ashley Zurcher
BUSINESS MANAGER
Amy Knies
PRODUCTION MANAGER
Tim Tate
VICE PRESIDENT AND EXECUTIVE GROUP
PUBLISHER
Richard Swadley
VICE PRESIDENT AND EXECUTIVE PUBLISHER
Neil Edde
ASSOCIATE PUBLISHER
Jim Minatel
PROJECT COORDINATOR, COVER
Katie Crocker
PROOFREADER
Nancy Carrasco
INDEXER
Robert Swanson

COVER DESIGNER
Elizabeth Brooks
COVER IMAGE
© Marilyn Nieves / iStockphoto
BACKGROUND
© PhotoAlto Images/Fotosearch
CREDITS
ffirs.indd xiiiffirs.indd xiii 4/10/2013 9:08:27 PM4/10/2013 9:08:27 PM
www.it-ebooks.info
ffirs.indd xivffirs.indd xiv 4/10/2013 9:08:27 PM4/10/2013 9:08:27 PM
www.it-ebooks.info
ACKNOWLEDGMENTS
I’D LIKE TO THANK the time and dedication of the team at Wrox Press and John Wiley & Sons. Without
their support, this book would not be possible. Thanks to Scott Hunter, Scott Hanselman, Damian
Edwards, and the rest of the ASP.NET team for their current leadership to bring ASP.NET 4.5 to market.
Thanks to Scott Guthrie, Steve Smith, and Rob Howard for allowing me to join the ASPInsiders program
and helping me get my content off the ground. Thanks to all of the ASPInsiders and Microsoft MVPs for
their support over the years. Thanks to Frank Sorokach in allowing me to spend the time necessary to get
my development career off the ground. Thanks to Louis Cesare and Sean Higgins for supporting my com-
munity initiatives and giving me time when I’ve needed to provide feedback to the ASP.NET team. Thanks
to Luzerne County Community College and Penn State University for allowing our user group to prosper in
Northeastern Pennsylvania. Finally, thanks to my family, speci cally my parents, Deb and Tom Gaylord; my
grandparents; my aunts and uncles; my wife, Lisa; and my children for their support. It’s not easy writing a
book and they’ve certainly understood the time and dedication needed to get this book to ship.
—J N. G
EVERY BOOK IS A TEAM EFFORT with all the editors (acquisition, development, technical, etc.) involved,
but with this title, the authors are quite a large team, too! So thank you to Jason, Pranav, Scott, and Todd, it
has been an honor working with you. Also we are indebted to the authors of the previous editions, to every-
one involved at Wiley, and to our tech editors.
About 7 years ago, I was working on another book for the Wrox imprint of Wiley, and promised Yvonne

that I would invite her to a really sumptuous dinner at one of Europe´s top restaurants after it has been pub-
lished. Things did not turn out as planned, including co-authors dropping out, so the book (and the dinner)
never materialized. So let´s try again—would June 13 work?
—C W
I WOULD LIKE TO THANK all the coffee shops owners in Seattle. The relaxing environment and a good cof-
fee was my inspiration in writing the book.
—P R
THANKS to Kelly for putting up with more late nights than normal and to my two princesses, Amber and
Sydney, for putting up with less time and attention! I love you all. Thanks to Kevin, Mary, and the rest of
the team at Wiley for their help and support on this book project. Also a big thank you to my co-authors. It
has been great working with you on this book.
—T M
BIG THANKS to Pranav Rastogi, Scott Hunter, Damian Edwards, Eilon Lipton, and the whole ASP.NET
team that does the real work.
—S H
ffirs.indd xvffirs.indd xv 4/10/2013 9:08:27 PM4/10/2013 9:08:27 PM
www.it-ebooks.info
ffirs.indd xviffirs.indd xvi 4/10/2013 9:08:27 PM4/10/2013 9:08:27 PM
www.it-ebooks.info
CONTENTS
FOREWORD xli
INTRODUCTION xliii
PART I: ASP.NET FUNDAMENTALS
CHAPTER 1: ONE ASP.NET 3
Introducing One ASP.NET 3
Evolution of ASP.NET 4
The Web Evolves and We Evolve with It 5
Simplifying a Complex Ecosystem 6
Web Ecosystem 7
Getting Started Is Easy 8

Integrating the Ecosystem Is Easy 9
Real-World Example 11
How Do You Benefi t? 14
ASP.NET Makes Getting Started Easy 14
ASP.NET Supports the Web Ecosystem 14
ASP.NET Makes Finding, Adding, and Updating Lego
Blocks to Your Site Easy 15
ASP.NET Helps You Apply Concepts from One Framework to Another 15
ASP.NET Moves as Fast as the Web 15
Summary 16
CHAPTER 2: HTML5 AND CSS3 DESIGN WITH ASP.NET 17
Caveats 18
HTML5 Overview 18
New Elements, Attributes, and Values in HTML5 19
Using the New HTML5 Markup 22
New APIs in HTML5 23
CSS3 Overview 24
Creating Style Sheets 25
CSS Rules 27
CSS Inheritance 34
Element Layout and Positioning 35
New Features in CSS3 41
HTML and CSS Compatibility 45
Working with HTML and CSS in Visual Studio 46
ftoc.indd xviiftoc.indd xvii 4/10/2013 9:09:22 PM4/10/2013 9:09:22 PM
www.it-ebooks.info
xviii
CONTENTS
Working with CSS in Visual Studio 46
Styling ASP.NET Controls 48

HTML and CSS Improvements in Visual Studio 2012 50
Page Inspector 51
Summary 52
CHAPTER 3: ASP.NET WEB FORMS STRUCTURE 53
Application Location Options 53
Using File System (IIS Express) 54
Using IIS 55
Using FTP 56
The ASP.NET Page Structure Options 57
Inline Coding 57
Code-Behind Model 59
ASP.NET 4.5 Page Directives 61
@Page 62
@Master 64
@Control 66
@Import 67
@Implements 69
@Register 69
@Assembly 70
@PreviousPageType 70
@MasterType 70
@OutputCache 71
@Reference 71
ASP.NET Page Events 71
Dealing with Postbacks 73
Cross-Page Posting 73
ASP.NET Application Folders 78
App_Code Folder 78
App_Data Folder 82
App_GlobalResources Folder 82

App_LocalResources Folder 83
App_WebReferences Folder 83
App_Browsers Folder 83
Compilation 83
Build Providers 87
Using the Built-In Build Providers 88
Using Your Own Build Providers 89
Global.asax 93
Working with Classes through Visual Studio 2012 96
Summary 99
ftoc.indd xviiiftoc.indd xviii 4/10/2013 9:09:23 PM4/10/2013 9:09:23 PM
www.it-ebooks.info
xix
CONTENTS
PART II: CONTROLS
CHAPTER 4: ASP.NET SERVER CONTROLS
AND CLIENT-SIDE SCRIPTS 103
ASP.NET Server Controls 104
Types of Server Controls 104
Building with Server Controls 105
Working with Server Control Events 107
Applying Styles to Server Controls 108
HTML Server Controls 111
Looking at the HtmlControl Base Class 112
Looking at the HtmlContainerControl Class 113
Looking at All of the HTML Classes 113
Using the HtmlGenericControl Class 114
Identifying ASP.NET Server Controls 115
Manipulating Pages and Server Controls
with JavaScript 117

Using Page.ClientScript.RegisterClientScriptBlock 119
Using Page.ClientScript.RegisterStartupScript 120
Using Page.ClientScript.RegisterClientScriptInclude 122
Summary 122
CHAPTER 5: ASP.NET WEB SERVER CONTROLS 123
An Overview of Web Server Controls 124
The Label Server Control 124
The Literal Server Control 126
The TextBox Server Control 127
Using the Focus() Method 127
Using AutoPostBack 128
Using AutoCompleteType 129
The Button Server Control 130
The CausesValidation Property 130
The CommandName Property 130
Buttons That Work with Client-Side JavaScript 131
The LinkButton Server Control 133
The ImageButton Server Control 134
The HyperLink Server Control 134
The DropDownList Server Control 135
Visually Removing Items from a Collection 137
The ListBox Server Control 139
Allowing Users to Select Multiple Items 139
ftoc.indd xixftoc.indd xix 4/10/2013 9:09:23 PM4/10/2013 9:09:23 PM
www.it-ebooks.info
xx
CONTENTS
An Example of Using the ListBox Control 139
Adding Items to a Collection 141
The CheckBox Server Control 142

How to Determine Whether Check Boxes Are Checked 143
Assigning a Value to a Check Box 143
Aligning Text around the Check Box 144
The CheckBoxList Server Control 144
The RadioButton Server Control 146
The RadioButtonList Server Control 148
Image Server Control 149
Table Server Control 151
The Calendar Server Control 153
Making a Date Selection from the Calendar Control 153
Choosing a Date Format to Output from the Calendar 154
Making Day, Week, or Month Selections 155
Working with Date Ranges 155
Modifying the Style and Behavior of Your Calendar 157
AdRotator Server Control 160
The Xml Server Control 161
Panel Server Control 162
The PlaceHolder Server Control 164
BulletedList Server Control 165
HiddenField Server Control 171
FileUpload Server Control 172
Uploading Files Using the FileUpload Control 172
Giving ASP.NET Proper Permissions to Upload Files 175
Understanding File Size Limitations 176
Uploading Multiple Files from the Same Page 177
Placing the Uploaded File into a Stream Object 180
Moving File Contents from a Stream Object to a Byte Array 181
MultiView and View Server Controls 181
Wizard Server Control 185
Customizing the Side Navigation 186

Examining the AllowReturn Attribute 187
Working with the StepType Attribute 187
Adding a Header to the Wizard Control 188
Working with the Wizard’s Navigation System 189
Utilizing Wizard Control Events 190
Using the Wizard Control to Show Form Elements 191
ImageMap Server Control 196
Summary 198
ftoc.indd xxftoc.indd xx 4/10/2013 9:09:23 PM4/10/2013 9:09:23 PM
www.it-ebooks.info
xxi
CONTENTS
CHAPTER 6: VALIDATION SERVER CONTROLS 199
Understanding Validation 199
Client-Side versus Server-Side Validation 200
ASP.NET Validation Server Controls 201
Validation Causes 202
Unobtrusive Validation in ASP.NET 4.5 203
The RequiredFieldValidator Server Control 205
The CompareValidator Server Control 209
The RangeValidator Server Control 212
The RegularExpressionValidator Server Control 216
The CustomValidator Server Control 217
The ValidationSummary Server Control 222
Turning O Client-Side Validation 224
Using Images and Sounds for Error Notifi cations 225
Working with Validation Groups 227
Summary 230
CHAPTER 7: USER AND SERVER CONTROLS 231
User Controls 232

Creating User Controls 232
Interacting with User Controls 234
Loading User Controls Dynamically 235
Server Controls 240
Server Control Projects 240
Control Attributes 244
Control Rendering 245
Styling HTML 251
Adding Client-Side Features 253
Browser Capabilities 261
Using ViewState 263
Raising Postback Events 267
Handling Postback Data 270
Composite Controls 273
Templated Controls 275
Design-Time Experiences 279
Summary 296
PART III: DATA ACCESS
CHAPTER 8: DATA BINDING 299
Data Source Controls 299
SqlDataSource Control 301
ftoc.indd xxiftoc.indd xxi 4/10/2013 9:09:23 PM4/10/2013 9:09:23 PM
www.it-ebooks.info
xxii
CONTENTS
AccessDataSource Control 307
LinqDataSource Control 307
EntityDataSource Control 307
Using the QueryExtender for Complex Filters 309
XmlDataSource Control 310

ObjectDataSource Control 311
SiteMapDataSource Control 313
Data Source Control Caching 313
Data-Bound Controls 314
GridView 314
Editing GridView Row Data 323
Deleting GridView Data 329
DetailsView 330
Inserting, Updating, and Deleting Data Using
DetailsView 333
ListView 334
FormView 340
Other Data-Bound Controls 343
TreeView 343
Menu 344
Chart 344
Inline Data-Binding Syntax 347
Data-Binding Syntax 348
XML Data Binding 348
Using Expressions and Expression Builders 349
Summary 353
CHAPTER 9: MODEL BINDING 355
Model Binding 355
Selecting Data 356
Paging 357
Filtering 357
Using Value Providers 358
Inserting Data 359
Updating Data 360
Using Strongly Typed Controls 362

Extending Model Binding 363
Custom Value Providers 363
Custom Model Binders 365
Custom ModelDataSource 367
Summary 369
ftoc.indd xxiiftoc.indd xxii 4/10/2013 9:09:23 PM4/10/2013 9:09:23 PM
www.it-ebooks.info
xxiii
CONTENTS
CHAPTER 10: QUERYING WITH LINQ 371
LINQ to Objects 372
Understanding Traditional Query Methods 372
Replacing Traditional Queries with LINQ 376
Grouping Data Using LINQ 384
Using Other LINQ Operators 385
Making LINQ Joins 386
Paging Using LINQ 387
LINQ to XML 388
A Simple LINQ to XML Example 389
Joining XML Data 392
LINQ to SQL 393
Using the O/R Mapper 393
Accessing and Querying Data 395
Using Other SQL Query Methods 399
Making Insert, Update, and Delete Queries through LINQ 401
LINQ to Entities 405
Creating an Entity Framework Data Model 406
Accessing Data 406
Writing LINQ Queries 407
Summary 409

CHAPTER 11: ENTITY FRAMEWORK 411
Can We Speak the Same Language? 412
Development Workfl ow Options 413
The Entity Data Model 414
Creating Your First Entity Data Model 414
Working through the EDM Wizard 416
Using the Entity Framework Designer 418
Building an ASP.NET Web Form Using Your EDM 420
Understanding Relationships 422
One-to-One and One-to-Many Relationships 422
Many-to-One and Many-to-Many Relationships 425
Performing Inheritance within the EDM 427
Using the EntityDataSource Control 430
Creating the Base Page 430
Confi guring the Data Source Control 431
Entity Framework Code First 433
Creating a Code First Model 433
Convention over Confi guration 437
ftoc.indd xxiiiftoc.indd xxiii 4/10/2013 9:09:23 PM4/10/2013 9:09:23 PM
www.it-ebooks.info

×