53 PART II CONTROLS CHAPTER 4 ASP.NET Server Controls and Client-Side Scripts.. 103 CHAPTER 5 ASP.NET Web Server Controls.. 559 PART V ASP.NET FEATURES CHAPTER 16 Working with Maste
Trang 3PROFESSIONAL ASP.NET 4.5 IN C# AND VB
INTRODUCTION xliii
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
Trang 4CHAPTER 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
Trang 5ASP.NET 4.5 in C# and VB
Trang 7ASP.NET 4.5 in C# and VB
Jason N Gaylord Christian Wenz Pranav Rastogi Todd Miranda Scott Hanselman
Trang 810475 Crosspoint Boulevard
Indianapolis, IN 46256
www.wiley.com
Copyright © 2013 by John Wiley & Sons, Inc., Indianapolis, Indiana
Published simultaneously in Canada
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 specifi cally disclaim all warranties, including
without limitation warranties of fi 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 affi 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
Trang 9To my loving wife, Lisa, and our four awesome
children.
—Jason N Gaylord
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
Trang 11ABOUT 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 fi 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 certifi 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 http://blogs.msdn.com/b/
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
Trang 12the 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 offi ce in Portland, Oregon Scott has three podcasts, http://hanselminutes.com for tech talk, http://thisdeveloperslife.com 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 Certifi 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.
Trang 13JEFFERY 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 taykwama@hotmail.com
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 deepu_verma@yahoo.com
Trang 15SENIOR PRODUCTION EDITOR
Kathleen Wisor
COPY EDITORS Kim Cofer
Kezia Endsley
EDITORIAL MANAGER
Mary Beth Wakefi eld
FREELANCER EDITORIAL MANAGER
Trang 17I’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 munity initiatives and giving me time when I’ve needed to provide feedback to the ASP.NET team Thanks
com-to Luzerne County Community College and Penn State University for allowing our user group com-to prosper in Northeastern Pennsylvania Finally, thanks to my family, specifi 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
—Jason N Gaylord
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?
—Todd Miranda
BIG THANKS to Pranav Rastogi, Scott Hunter, Damian Edwards, Eilon Lipton, and the whole ASP.NET team that does the real work
—Scott Hanselman
Trang 19FOREWORD xli
INTRODUCTION xliii
PART I: ASP.NET FUNDAMENTALS
ASP.NET Makes Finding, Adding, and Updating Lego
Summary 16
Caveats 18
Trang 20Working with CSS in Visual Studio 46
Summary 52
Global.asax 93
Summary 99
Trang 21CONTENTS
PART II: CONTROLS
CHAPTER 4: ASP.NET SERVER CONTROLS
Manipulating Pages and Server Controls
Trang 22An Example of Using the ListBox Control 139
Summary 198
Trang 23CONTENTS
Summary 230
Summary 296 PART III: DATA ACCESS
Trang 24DetailsView 330 Inserting, Updating, and Deleting Data Using
ListView 334 FormView 340
TreeView 343 Menu 344 Chart 344
Summary 369
Trang 25CONTENTS
Trang 26Relationships in Code First 437
Summary 442
Summary 453
Trang 27CONTENTS
Summary 525 PART IV: PROVIDERS
Setting Up Your Provider to Work with Microsoft SQL Server
Summary 557
Not Implementing Methods and Properties
Implementing Methods and Properties
Trang 28Using the XmlMembershipProvider for User Login 579
Summary 588 PART V: ASP.NET FEATURES
Summary 620
Trang 29CONTENTS
ShowStartingNode 654 StartFromCurrentNode 655
Summary 669
Trang 30Programmatic Access to Personalization 685
Trang 31Summary 787 PART VI: APPLICATION STATE
QueryStrings 813 Cookies 814
Using HttpContext.Current.Items for Very Short-Term Storage 820 Summary 821
Trang 32CHAPTER 22: CACHING 823 Caching 824
Summary 854 PART VII: CLIENT-SIDE DEVELOPMENT
Trang 33CONTENTS
Summary 894
AlwaysVisibleControlExtender 900 AnimationExtender 902 AutoCompleteExtender 903 BalloonPopupExtender 906 CalendarExtender 907 CollapsiblePanelExtender 908 ColorPickerExtender 910
DragPanelExtender 913 DropDownExtender 914 DropShadowExtender 916 DynamicPopulateExtender 919 FilteredTextBoxExtender 922 HoverMenuExtender 923 ListSearchExtender 925
MutuallyExclusiveCheckBoxExtender 928 NumericUpDownExtender 929 PagingBulletedListExtender 930 PopupControlExtender 931 ResizableControlExtender 933 RoundedCornersExtender 935
SlideShowExtender 937 TextBoxWatermarkExtender 939 ToggleButtonExtender 941
Trang 34UpdatePanelAnimationExtender 942 ValidatorCalloutExtender 944
Summary 1000
Trang 35CONTENTS
CHAPTER 27: DEVELOPING WEBSITES WITH MOBILE IN MIND 1001
Summary 1023 PART VIII: APPLICATION CONFIGURATION AND DEPLOYMENT
Authentication 1041
Authorization 1044
Trang 36Confi guring the ASP.NET Worker Process 1051
Summary 1074
Trang 37CONTENTS
HttpModules 1108 HttpHandlers 1113
Summary 1133
Trang 38ASP.NET 4.5 Resource Files 1152
Summary 1161 CHAPTER 33: PACKAGING AND DEPLOYING ASP.NET
APPLICATIONS 1163
Summary 1184 PART IX: ADDITIONAL ASP.NET TECHNOLOGIES
Controllers 1203
Views 1207
Trang 39CONTENTS
Summary 1214
Summary 1235 PART X: APPENDIXES
Summary 1246
Trang 40APPENDIX C: ASP.NET ULTIMATE TOOLS 1257
Firebug 1257 YSlow 1258
References 1261
Visibone 1262 www.asp.net 1262 www.webdevchecklist.com 1262 SlowCheetah 1262
Summary 1267