Professional ASP.NET Web Forms Techniques

Summary Of Contents

Introduction1
Chapter 1: Getting Started9
Chapter 2: Browser Compatibility with ASP.NET Server Controls45
Chapter 3: Laying the Foundations83
Chapter 4: The Home Page125
Chapter 5: Banners, Footers, and E-Mail169
Chapter 6: The Ancillary Pages213
Chapter 7: Creating Graphs and Charts265
Chapter 8: Interactive Web Forms317
Chapter 9: The Interactive Model Details353
Chapter 10: Car Prices and Quotations395
Chapter 11: The Login and Register Page435
Chapter 12: Quotations and Orders481
Index521

Table of Contents

Introduction1
        What Is This Book About?1
        Who Is This Book For?3
            What This Book Is Not3
            What This Book Is4
        What Do You Need To Use This Book?4
    Conventions4
    Customer Support and Feedback6
        Source Code and Updates6
        Errata6
        Technical Support6
        p2p.wrox.com7
Chapter 1: Getting Started9
    Why ASP.NET?10
        A Note on Terminology10
            Is It a Bird? ... Is It a Plane? ... No, It's a Wapplisite!10
        ASP.NET Fundamentals11
            Event-Driven Applications11
            The ASP.NET Page as a Compiled Class13
            The ASP.NET Postback Architecture16
            ASP.NET Server Controls25
            Other Useful ASP.NET Features26
    Designing an Interactive Web Site28
        The Overall Site Design28
            Requirements Analysis28
            The Wrox Car Company29
        The Home Page32
            The Ingredients for the Home Page33
        The Car Model Details Page34
            The Color Options Section34
            The Engine Options Section36
            The Optional Extras Section37
            The Model Details Section38
            The Finance Options Section39
        The Compare Features Page40
            The Ingredients for the Compare Features Page40
        The Order Pages41
        Other "Peripheral" Pages41
            Is It All Actually Achievable?41
    Summary42
Chapter 2: Browser Compatibility with ASP.NET Server Controls45
    Built-In Intelligence46
    A Test Application47
        Use a Selection of Browsers49
    Viewing the Test Pages49
        The Hyperlink Server Control51
        But Navigator Understands the Style Attribute!54
        The Calendar Control55
    Specifying Up-level or Down-Level Clients56
        The Definition of 'Up-level' and 'Down-level'58
    List Control Formatting and Templates59
        The Output from the DataList Control60
        Adding Templates to the DataList Control62
        More Complexity - the DataGrid Control64
        Specifying Column Information in a DataGrid Control65
            Formatting Within a TemplateColumn in a DataGrid Control67
    What If There's No Client-Side Script Support?68
        Example: a LinkButton in Amaya69
        Solving the 'No Script' Problem71
            When Scripting Is Supported but Disabled71
        The ASP.NET Validation Controls72
            The Validation Controls in Down-Level Clients74
    The ASP.NET Panel Control74
        The Panel Control in Down-Level Clients76
    Don't Get Caught Out by Output Caching77
        Caching by Browser Version78
        Caching by Client Target79
    Summary80
Chapter 3: Laying the Foundations83
    Obtaining the Example Files84
        Installing the Wrox Car Company Site84
    Wrox Cars' Client-Side Technical Requirements85
        What 'Standards' Do We Support or Require?86
        ASP.NET Sessions87
            Requirements for Session Support87
            Maximizing Performance When Using Sessions90
        Client-Side Scripting91
        Users and User IDs in the Wrox Car Company Site91
    Checking for Sessions and Scripting Support92
        Client Capabilities and User ID Detection93
        The Code in default.aspx94
        The HTML and Content in default.aspx95
        The no-script.htm Page97
            Enabling and Disabling Client-side Scripting98
        The Detection Code in home.aspx99
        The no-sessions.htm Page100
            Cookie Based or Cookie-less Sessions?101
            Enabling and Disabling Cookie Support102
    Cross-Browser Compatibility Issues103
        Browsers for Testing Your Pages104
            Other User Agents104
    Solving Browser Compatibility Issues106
        Techniques for Maximizing Compatibility106
            Declaring and Linking to CSS Stylesheets107
            Font Styles, Types, and Sizes108
            ASP.NET Server Controls and the CssClass Property111
            Layout of Page Sections and Individual Elements114
            Script-Driven Animations and Other 'Actions'116
            Coping with Navigator/Communicator 4.x.119
    Making Sites Accessible to All Visitors119
        Why Provide Universal Accessibility?120
            A Summary of Techniques for Accessibility120
    Summary122
Chapter 4: The Home Page125
    The Overall Page Structure126
        Creating the Page Structure128
            The HTML Table for the Outline Structure128
    Setting the Styles for the Page132
        Specifying the CSS Style Sheet132
            The Standard Style Sheet132
            The Netscape/Mozilla Stylesheet134
            Detecting the Client Type134
            Linking to the Correct Stylesheet136
    Providing Accessibility Navigational Aids137
        Implementing 'Skip To' Links138
            The Source Code for the 'Skip To' Links140
        Remember those 'title' and 'alt' Attributes141
    The Links to Other Pages142
        The ASP.NET Hyperlink Control144
            Setting the 'title' and 'alt' Attributes144
    The 'News and Special Offers' Section145
        The Data Access Code for the 'News' List145
            The GetNewsList Stored Procedure145
            The GetNewsListDR Function146
            Data Binding the Repeater Control147
        Choosing the Appropriate ASP.NET 'List' Control148
        The Static Section of the 'News' List149
        The ASP.NET Repeater Control for the 'News' List150
            Data Binding Syntax in a List Control Template151
    The List of Car Models153
        Choosing the Appropriate List Control153
        Changing the Behavior for Script-Enabled Clients154
        The Data Access Code for the 'Car Models' List154
            The GetModelList Stored Procedure155
            The GetModelListDR Function155
            Data Binding the DataGrid Control155
        The DataGrid Control for the 'Car Models' List156
            Specifying Styles and Templates157
            Specifying Column Information in a DataGrid Template157
            Handling the ItemDataBound Event for a DataGrid162
    Summary165
Chapter 5: Banners, Footers, and E-Mail169
    About ASP.NET User Controls169
        Reusable Code and Content170
        Building a User Control171
            The Contents of a User Control172
        Using a User Control in an ASP.NET Page174
            Setting Properties174
    The Animated Page Banner175
        Building the Animated Banner Control175
            The Structure of the 'Page Banner' User Control176
            The ASP.NET Literal Control178
            The Properties of our User Control179
            The Page_Load Event Handler180
            The Animation Content for CSS-Enabled Clients182
            The Animation Content for Internet Explorer 4.x and Above185
            The Animation Content for Netscape 4.x Only187
        Using the Animated Banner Control189
            Setting the Banner Control Properties189
    The Page Footer Section190
        Dynamic Hyperlinks in the Footer191
        Building the Page Footer Control191
            The HtmlAnchor Control192
            The Code for the Page Footer Control193
        Using the Page Footer Control195
    Adding Users to the Mailing List195
        The Controls to Submit an E-Mail Address197
            ASP.NET Form Controls and the <form> Element198
            ASP.NET Validation Controls198
            The Validation Controls in the 'Home' Page201
            The ValidationSummary Control in the 'Home' Page202
            Online Server Controls Examples203
        The Code to Handle E-Mail Addresses204
            Using the Page.IsValid Property204
            The UpdateMailingList Function205
            The SendMail User Control207
            The SendEmailConfirmation Routine208
    Summary210
Chapter 6: The Ancillary Pages213
    The Structure of the Ancillary Pages214
        The Overall Layout214
        'Terms & Conditions' and 'Contact' Pages214
            The HTML Table for the Ancillary Pages Structure215
            The Code for the Ancillary Pages216
        The MainLink User Control218
            The Script Section220
            Using the MainLink Control220
    Choosing Static or Dynamic Content221
        Making Static Content Dynamic221
            Dynamic Content Without a Database222
        Making Dynamic Content Static223
            Generating Static Pages with ASP.NET223
            Using ASP.NET Caching to Expose 'Static' Pages224
    The 'Read News Item' Page225
        Inserting Dynamic Content into Pages227
        The HTML and Code for the 'Read News' Page228
            Getting the Content from the Data Store228
            Inserting the Content into the Page229
            Extracting Values from a DataReader231
    The 'Compare Models' Page233
        Accessibility Issues with Complex Images234
            Using a Long Description235
            Providing Alternative Content Formats237
        Building the 'Compare Models' Page238
            The 'Skip To' Links238
            The Main Page Structure Table238
            The Controls to Select the Comparison Type239
            The Controls to Display the Results of the Comparison241
            The Code To Make It All Work244
            Getting the Car and Engine Data250
            Generating the Comparison Results252
        Testing for Accessibility259
            Using a Text-Only Browser259
            Using an Aural Page Reader260
    Summary262
Chapter 7: Creating Graphs and Charts265
    Dynamically Generated Graphics and Images266
        Performance and Caching267
            Generating Graphics on the Client267
            ASP.NET Caching for Images and Graphics267
            Writing Images to Disk Files268
        Using Dynamic Images in Web Pages268
            Alternative Elements for Inserting Images269
            Using Multiple Static Image Files269
            Passing Values to an Image Generation Page270
    The .NET Framework Drawing Classes271
        The System.Drawing Namespace271
            It's All Very Simple272
    Creating a Simple Pie Chart274
        Providing the Data Source274
            Creating the DataSet for the Pie Chart Example275
        The Pie Chart Example Page276
            Adding the Import Directives276
            The Page_Load Event Handler277
            Drawing Text on a Bitmap278
        Creating and Using Colors in Images280
            The Browser-Safe and Standard Colors283
        Our Generic Pie Chart Routine289
            Calculating the Position of the Main Chart Elements290
            Creating Rectangles and Drawing Shadows291
            Declaring More Variables and Totaling the Rows292
            Drawing the Pie Slices293
            Adding the Key and Caption294
            Completing the Loop295
            A Note About Saving a Bitmap295
    The Vehicle Comparison Charts296
        Getting the Values and Data for the Chart296
            Creating the SQL Statement297
        Drawing the Appropriate Chart298
        Drawing the Line Chart300
            Declaring the Variables We'll Need301
            Creating Arrow Caps for the Axes301
            Drawing the Y-Axis Values302
            Drawing the X-Axis Values303
            Drawing the Value Lines304
            Drawing the 'Key' Boxes and Descriptions306
        Drawing the Bar Chart307
            The Routine Parameters and Variable Declarations308
            Drawing the Axes and 'Key' Boxes308
            Calculating the X-Axis Values309
            Drawing the X-Axis Values311
            Drawing the Y-Axis Values311
    Summary314
Chapter 8: Interactive Web Forms317
    Meeting the Design Requirements318
        The Original Design318
            Selecting a Color for the Vehicle318
            Selecting the Engine for the Vehicle319
            Selecting the Optional Extras for the Vehicle320
            Viewing the Standard Features of the Vehicle322
            Selecting a Finance Package for the Vehicle323
        Supporting 'No-Script' Clients324
        Supporting 'Text-Only' Clients325
        Supporting 'Page-Reader' Clients325
        Saving Quotations and Closing the Window326
            The Navigation Issue in Lynx327
    The 'Model Details' Page in Outline327
        Opening the 'Model Details' Page327
        Using the Postback Architecture328
            A Schematic of the Whole Process328
        What Happened to the Frames?330
        Implementing a 'Tabbed Dialog'331
            About the IE Web Controls331
        Coping Without Client-Side Scripting Support333
            Client-Side Versus Server-Side Page Updates334
        Saving Quotations and Orders336
    The Structure of the Model Details Page337
        Importing and Registering the IE Web Controls337
        The HTML Section of the Page338
            The Model Description, Price, and Image339
            The 'Tabbed Dialog' Section340
            The TabStrip Control341
            The MultiPage Control342
            Linking the TabStrip and MultiPage Controls343
            What Output Do the TabStrip and MultiPage Controls Create?344
            The Information and Error Messages Label347
            The Instructions, Save, and Close Buttons347
    Summary351
Chapter 9: The Interactive Model Details353
    The Data for the 'Model Details' Page354
        The Vehicle Details Database Tables354
            But in the Real World...355
        The Stored Procedures for the 'Model Details' Page355
            Extracting the Vehicle Details355
            Extracting the List of Available Colors356
            Extracting the List of Available Extras357
        The Finance PMT Values Table357
            Using global.asax to Handle Application-Level Events357
            Calculating the Payment Amounts360
            Writing Status Messages to the Event Log and a Text Log File361
            Running Code when an Application Error Occurs363
            The GetFinancePMTRates Stored Procedure363
        Fetching and Storing the Data for the Page363
            Accessing the Database and Filling the DataSet364
    The Code in the 'Model Details' Page366
        The Page_Load Event Handler367
            Checking for an Active Session368
            Collecting the DataSet369
            Collecting the Price and Finance Terms369
    The 'Model Details' 'Tab' Pages in Detail371
        The Color Selection Section372
            Displaying the Color Blocks373
            Where Do the Colors Come From?374
            Populating the Page Heading and Colors Section375
            Reacting to Events in the Colors Section376
        The Engine Selection Section377
            The Engine Details (DataGrid) Table378
            Populating the Engine Section379
            Reacting to Events in the Engine Section379
        The 'Optional Extras' Selection Section380
            Populating the 'Extras' Section381
            Reacting to Events in the 'Extras' Section381
        The 'Standard Features' (Details) Section381
            The IE Web Controls TreeView Control382
            The HTML for the 'Details' Section384
            Populating the 'Details' Section386
            Reacting to Events in the 'Details' Section388
        The Finance Package Selection Section389
            Enabling and Disabling Buttons and Other Controls391
            Populating the 'Finance' Section391
            Reacting to Events in the Finance Section391
    Summary393
Chapter 10: Car Prices and Quotations395
    Calculating the Price of the Vehicle395
        The CalculateCarPrice Routine396
            Clearing Any Existing Finance Terms397
            Getting the Price for the Current Car/Engine Combination397
            Adding the Extra Cost for a Metallic Color399
            Adding the Cost of Optional Extras401
            Storing the Total Price in the Prices Array402
    Calculating the Finance Terms403
        The ShowFinanceTerms Routine404
            Handling an Error in the Calculation405
            The CalculateFinanceTerms Function405
            The GetNumberPayments Function409
        The ClearFinanceTerms Routine411
    Saving the Configuration as a Quotation412
        The Saved Quotes and Orders Tables412
        Updating the Data within a Transaction413
        Saving a Quotation414
            The Stored Procedures for the SaveCarDetails Routine414
            The SaveCarDetails Routine415
    The No-Script Version of the Page422
        Avoiding the TabStrip and MultiPage Controls423
            Custom Multi-Page Effects424
            Our Solution - Use Hyperlinks and Horizontal Rules425
        Working Around the Lack of AutoPostBack425
        Replacing the TreeView Control426
            The ASP.NET Xml Web Forms Control426
        Managing Without the setOptionButtons Function429
        Managing Without the mainWindowPage Function430
        Managing Without a Close Button431
    Summary431
Chapter 11: The Login and Register Page435
    Web Site Users and Identities436
        Authentication436
        Authorization437
        Impersonation438
            Permissions for the Anonymous Accounts439
        ASP.NET Authentication Methods439
            Windows Authentication440
            Passport Authentication441
            Forms Authentication441
            Specifying Users and Groups444
            Avoiding Nested ASP.NET Applications445
    Wrox Cars Login and Register Process446
        The Root Folder web.config File447
        The "secure" Sub-folder web.config File447
        Authentication for Cookie-Less Clients448
            Custom Authentication and Authorization Techniques448
        The Process Step-By-Step450
        Why Use Anonymous User IDs?452
        Why Use a Combined Login and Register Page?453
    The Login and Register Page454
        The HTML Page Declarations454
            The ASP.NET Validation Controls455
            The 'Login' Section of the Page455
            The ValidationSummary Control459
            The 'Register' Section of the Page459
        The Client-Side Script Functions462
            Disabling the Submit Buttons463
        The ASP.NET Server-Side Code464
            The Page_Load Event Handler464
            The DoLogin Event Handler466
            The DoRegister Event Handler475
    Summary479
Chapter 12: Quotations and Orders481
    Binding List Controls to Related Data481
        Binding to a Single Data Source482
        Binding Nested List Controls482
            An Example of Nested Bound Controls483
        Binding to Hierarchical Data492
            Hierarchical Binding in the 'My Cars' Page493
    Building the 'My Cars' Page494
        The HTML for the 'My Cars' Page495
            The Name and Address Details, and Log Off Button496
            The Messages Label497
            The Caption for the Quotations Section497
            The List Control for the Quotations Section498
            The 'Orders' Section501
        The Code for the 'My Cars' Page502
            Generating the DataSet of User Details502
            The Page_Load Event Handler505
            Displaying Details of the User507
            Displaying Details of the User's Vehicles508
            The ItemDataBound Event Handlers509
            Logging Out of the Application510
            Placing an Order511
            Sending Order Confirmation E-mail Messages514
    Summary518
Index521