Apress.beginning.asp.net.4.in.c sharp.2010.aug.2010

40,876 views
40,791 views

Published on

C# Book

Published in: Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
40,876
On SlideShare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
3,136
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Apress.beginning.asp.net.4.in.c sharp.2010.aug.2010

  1. 1. THE EXPERT’S VOICE ® IN .NET CompanioneBook Available Beginning ASP.NET 4 in C# 2010 Beginning ASP.NET 4 in C# 2010 Start your journey with the fundamentals of building ASP.NET websites MacDonald Matthew MacDonald
  2. 2. Beginning ASP.NET 4 in C# 2010■■■Matthew MacDonald
  3. 3. Beginning ASP.NET in C# 2010Copyright © 2010 by Matthew MacDonaldAll rights reserved. No part of this work may be reproduced or transmitted in any form or by anymeans, electronic or mechanical, including photocopying, recording, or by any informationstorage or retrieval system, without the prior written permission of the copyright the publisher.ISBN-13 (pbk): 978-1-4302-2608-6ISBN-13 (electronic): 978-1-4302-2609-3Printed and bound in the United States of America 9 8 7 6 5 4 3 2 1Trademarked names, logos, and images may appear in this book. Rather than use a trademarksymbol with every occurrence of a trademarked name, logo, or image we use the names, logos, andimages only in an editorial fashion and to the benefit of the trademark owner, with no intention ofinfringement of the trademark.The use in this publication of trade names, trademarks, service marks, and similar terms, even ifthey are not identified as such, is not to be taken as an expression of opinion as to whether or notthey are subject to proprietary rights. President and Publisher: Paul Manning Lead Editor: Ewan Buckingham Technical Reviewer: Damien Foggon Editorial Board: Clay Andres, Steve Anglin, Mark Beckner, Ewan Buckingham, Gary Cornell, Jonathan Gennick, Jonathan Hassell, Michelle Lowman, Matthew Moodie, Duncan Parkes, Jeffrey Pepper, Frank Pohlmann, Douglas Pundick, Ben Renow-Clarke, Dominic Shakeshaft, Matt Wade, Tom Welsh Coordinating Editor: Anne Collett Copy Editor: Kim Wimpsett Compositor: Mary Sudul Indexer: BIM Indexing & Proofreading Services Artist: April Milne Cover Designer: Anna IshchenkoDistributed to the book trade worldwide by Springer Science+Business Media, LLC., 233 SpringStreet, 6th Floor, New York, NY 10013. Phone 1-800-SPRINGER, fax (201) 348-4505, e-mail orders-ny@springer-sbm.com, or visit www.springeronline.com.For information on translations, please e-mail rights@apress.com, or visit www.apress.com.Apress and friends of ED books may be purchased in bulk for academic, corporate, or promotionaluse. eBook versions and licenses are also available for most titles. For more information, referenceour Special Bulk Sales–eBook Licensing web page at www.apress.com/info/bulksales.The information in this book is distributed on an “as is” basis, without warranty. Although everyprecaution has been taken in the preparation of this work, neither the author(s) nor Apress shallhave any liability to any person or entity with respect to any loss or damage caused or alleged to becaused directly or indirectly by the information contained in this work.
  4. 4. For my wonderful family,Faria, Maya, and Brenna
  5. 5. ■ CONTENTSContentsAbout the Author .................................................................................................. xxvAbout the Technical Reviewer ............................................................................. xxviAcknowledgments .............................................................................................. xxviiIntroduction ....................................................................................................... xxviiiPart 1: Introducing .NET ...........................................................................................1■ Chapter 1: The .NET Framework ..........................................................................3 The Evolution of Web Development .................................................................................3 HTML and HTML Forms ..........................................................................................................................3 Server-Side Programming ......................................................................................................................6 Client-Side Programming .......................................................................................................................7 The .NET Framework .......................................................................................................9 C#, VB, and the .NET Languages ..........................................................................................................11 The Common Language Runtime .........................................................................................................13 The .NET Class Library..........................................................................................................................14 Visual Studio.........................................................................................................................................15 The Last Word................................................................................................................16■ Chapter 2: The C# Language ..............................................................................17 The .NET Languages ......................................................................................................17 C# Language Basics ......................................................................................................18 Case Sensitivity ....................................................................................................................................18 Commenting .........................................................................................................................................18 Statement Termination.........................................................................................................................19 v
  6. 6. ■ CONTENTS Blocks...................................................................................................................................................20 Variables and Data Types ..............................................................................................20 Assignment and Initializers ..................................................................................................................22 Strings and Escaped Characters ..........................................................................................................24 Arrays ...................................................................................................................................................24 Enumerations .......................................................................................................................................26 Variable Operations .......................................................................................................27 Advanced Math.....................................................................................................................................29 Type Conversions .................................................................................................................................29 Object-Based Manipulation ...........................................................................................31 The String Type ....................................................................................................................................32 The DateTime and TimeSpan Types .....................................................................................................34 The Array Type......................................................................................................................................36 Conditional Logic ...........................................................................................................37 The if Statement ...................................................................................................................................38 The switch Statement...........................................................................................................................39 Loops .............................................................................................................................40 The for Loop..........................................................................................................................................40 The foreach Loop..................................................................................................................................41 The while loop ......................................................................................................................................42 Methods.........................................................................................................................43 Parameters ...........................................................................................................................................44 Method Overloading .............................................................................................................................45 Optional and Named Parameters..........................................................................................................46 Delegates..............................................................................................................................................47 The Last Word................................................................................................................48 ■ Chapter 3: Types, Objects, and Namespaces .....................................................49 The Basics About Classes..............................................................................................49 Static Members ....................................................................................................................................50 A Simple Class......................................................................................................................................51vi
  7. 7. ■ CONTENTS Building a Basic Class ...................................................................................................51 Creating an Object ................................................................................................................................53 Adding Properties .................................................................................................................................54 Automatic Properties ............................................................................................................................56 Adding a Method ..................................................................................................................................56 Adding a Constructor............................................................................................................................57 Adding an Event....................................................................................................................................59 Testing the Product Class.....................................................................................................................60 Value Types and Reference Types.................................................................................63 Assignment Operations ........................................................................................................................63 Equality Testing ....................................................................................................................................64 Passing Parameters by Reference and by Value ..................................................................................64 Reviewing .NET Types ..........................................................................................................................65 Understanding Namespaces and Assemblies................................................................67 Using Namespaces ...............................................................................................................................68 Importing Namespaces.........................................................................................................................69 Assemblies ...........................................................................................................................................70 Advanced Class Programming.......................................................................................71 Inheritance............................................................................................................................................71 Static Members ....................................................................................................................................73 Casting Objects.....................................................................................................................................74 Partial Classes ......................................................................................................................................76 Generics................................................................................................................................................77 The Last Word................................................................................................................79Part 2: Developing ASP.NET Applications...............................................................81■ Chapter 4: Visual Studio.....................................................................................83 The Promise of Visual Studio .........................................................................................83 Creating Websites..........................................................................................................84 Creating an Empty Web Application .....................................................................................................85 vii
  8. 8. ■ CONTENTS Websites and Web Projects ..................................................................................................................89 The Hidden Solution Files .....................................................................................................................90 The Solution Explorer ...........................................................................................................................91 Adding Web Forms ...............................................................................................................................92 Migrating a Website from a Previous Version of Visual Studio.............................................................93 Designing a Web Page...................................................................................................95 Adding Web Controls ............................................................................................................................96 The Properties Window ........................................................................................................................98 The Anatomy of a Web Form .......................................................................................100 The Web Form Markup .......................................................................................................................100 The Page Directive..............................................................................................................................101 The Doctype........................................................................................................................................102 The Essentials of XHTML ....................................................................................................................104 Writing Code ................................................................................................................110 The Code-Behind Class ......................................................................................................................110 Adding Event Handlers .......................................................................................................................110 Outlining .............................................................................................................................................112 IntelliSense .........................................................................................................................................113 Code Formatting and Coloring ............................................................................................................117 Visual Studio Debugging..............................................................................................118 The Visual Studio Web Server ............................................................................................................118 Single-Step Debugging.......................................................................................................................119 Variable Watches................................................................................................................................124 The Last Word..............................................................................................................125 ■ Chapter 5: Web Form Fundamentals................................................................127 The Anatomy of an ASP.NET Application .....................................................................127 ASP.NET File Types.............................................................................................................................128 ASP.NET Application Directories.........................................................................................................129 Introducing Server Controls .........................................................................................130 HTML Server Controls.........................................................................................................................131viii
  9. 9. ■ CONTENTS Converting an HTML Page to an ASP.NET Page..................................................................................131 View State ..........................................................................................................................................134 The HTML Control Classes..................................................................................................................135 Adding the Currency Converter Code .................................................................................................138 Event Handling....................................................................................................................................140 Behind the Scenes with the Currency Converter................................................................................142 Error Handling.....................................................................................................................................144Improving the Currency Converter...............................................................................144 Adding Multiple Currencies ................................................................................................................145 Storing Information in the List ............................................................................................................146 Adding Linked Images ........................................................................................................................147 Setting Styles .....................................................................................................................................150A Deeper Look at HTML Control Classes......................................................................151 HTML Control Events ..........................................................................................................................151 Advanced Events with the HtmlInputImage Control ...........................................................................152 The HtmlControl Base Class ...............................................................................................................154 The HtmlContainerControl Class.........................................................................................................155 The HtmlInputControl Class ................................................................................................................155The Page Class ............................................................................................................156 Sending the User to a New Page ........................................................................................................157 HTML Encoding...................................................................................................................................158Application Events .......................................................................................................160 The global.asax File............................................................................................................................161 Additional Application Events .............................................................................................................162ASP.NET Configuration ................................................................................................163 The web.config File ............................................................................................................................163 Nested Configuration ..........................................................................................................................164 Storing Custom Settings in the web.config File .................................................................................166 The Website Administration Tool (WAT) .............................................................................................168The Last Word..............................................................................................................170 ix
  10. 10. ■ CONTENTS ■ Chapter 6: Web Controls ..................................................................................173 Stepping Up to Web Controls . .....................................................................................173 Basic Web Control Classes . .................................................................................................................174 The Web Control Tags...........................................................................................................................175 Web Control Classes . ..................................................................................................176 The WebControl Base Class..................................................................................................................177 Units . ....................................................................................................................................................179 Enumerations .......................................................................................................................................179 Colors . ..................................................................................................................................................180 Fonts. ....................................................................................................................................................181 Focus . ...................................................................................................................................................183Download from Wow! eBook <www.wowebook.com> The Default Button................................................................................................................................183 List Controls. ...............................................................................................................184 Multiple-Select List Controls . ..............................................................................................................185 The BulletedList Control .......................................................................................................................187 Table Controls. ............................................................................................................188 Web Control Events and AutoPostBack . .....................................................................193 How Postback Events Work. ................................................................................................................197 The Page Life Cycle ..............................................................................................................................198 A Simple Web Page. ....................................................................................................201 Improving the Greeting Card Generator. ..............................................................................................206 Generating the Cards Automatically . ...................................................................................................208 The Last Word. ............................................................................................................211 ■ Chapter 7: Error Handling, Logging, and Tracing.............................................213 Common Errors. ..........................................................................................................213 Exception Handling . ....................................................................................................215 The Exception Class ....