TEAM LinG Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. Alex Homer Dan Kent Dave Sussman Dan Whalin 800 East 96th Street, Indianapolis, Indiana 46240 ASP.NET 1.1 00 0672326744 FM 5/4/04 12:25 PM Page i TEAM LinG Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. ASP.NET 1.1 Insider Solutions Copyright © 2004 by Sams Publishing All rights reserved. No part of this book shall be reproduced, stored in a retrieval system, or transmitted by any means, electronic, mechanical, photocopying, recording, or otherwise, without written permission from the publisher. No patent liability is assumed with respect to the use of the information contained herein. Although every precaution has been taken in the preparation of this book, the publisher and author assume no responsibility for errors or omissions. Nor is any liability assumed for damages resulting from the use of the information contained herein. International Standard Book Number: 0-672-32674-4 Library of Congress Catalog Card Number: 2004091341 Printed in the United States of America First Printing: June 2004 07 06 05 04 4321 Trademarks All terms mentioned in this book that are known to be trademarks or service marks have been appropriately capitalized. Sams Publishing cannot attest to the accuracy of this information. Use of a term in this book should not be regarded as affecting the validity of any trademark or service mark. Warning and Disclaimer Every effort has been made to make this book as complete and as accurate as possible, but no warranty or fitness is implied. The information provided is on an “as is” basis. The author and the publisher shall have neither liability nor responsibility to any person or entity with respect to any loss or damages arising from the information contained in this book. Bulk Sales Sams Publishing offers excellent discounts on this book when ordered in quantity for bulk purchases or special sales. For more information, please contact U.S. Corporate and Government Sales 1-800-382-3419 corpsales@pearsontechgroup.com For sales outside of the U.S., please contact International Sales 1-317-428-3341 international@pearsontechgroup.com Associate Publisher Michael Stephens Acquisitions Editor Neil Rowe Development Editor Mark Renfrow Managing Editor Charlotte Clapp Project Editor Dan Knott Copy Editor Kitty Jarrett Indexer Heather McNeill Proofreader Katie Robinson Technical Editors Dan Maharry Justin Rogers Team Coordinator Cindy Teeters Designer Gary Adair Page Layout Bronkella Publishing 00 0672326744 FM 5/4/04 12:25 PM Page ii Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. Contents at a Glance Introduction .1 Part IWeb Form User Interfaces 1 Web Forms Tips and Tricks .7 2 Cross-Page Posting 51 3 Loading Progress and Status Displays 75 4 Working with Nested List Controls 109 Part II Reusability 5 Creating Reusable Content .155 6 Client-Side Script Integration 197 7 Design Issues for User Controls .243 8 Building Adaptive Controls 297 9 Page Templates .353 Part III Data Techniques 10 Relational Data-Handling Techniques .385 11 Working with XML Data 429 Part IV Hosting and Security 12 Side-by-Side Execution in ASP.NET 479 13 Taking Advantage of Forms Authentication .499 14 Customizing Security 537 Index 561 00 0672326744 FM 5/4/04 12:25 PM Page iii Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. Table of Contents Introduction 1 Part IWeb Form User Interfaces 1 Web Forms Tips and Tricks 7 Getting More from ASP.NET Validation Controls 8 Validating a RadioButtonList Control .9 Validating a CheckBoxList Control .11 Validating Nonstandard Control Values .12 Using List and Validation Controls in a DataGrid Control .14 Taking Control of Content Layout in a DataGrid Control .31 Controlling the Width of Columns in a DataGrid Control 32 Using Multiple Edit Controls in a DataGrid Control Column .33 Controlling the Width of Edit Controls in a DataGrid Control 35 Providing Scrollable Content in a DataGrid Control .36 Loading Controls Dynamically at Runtime .38 The ASP.NET Control Tree .38 Creating a DataGrid Control Dynamically at Runtime 41 Loading User Controls Dynamically at Runtime 46 Summary .49 2 Cross-Page Posting 51 Techniques for Passing Values Between Pages .52 Accessing Request Values in Another Page .52 Changing the action Attribute of a Form 53 Redirecting Postbacks to the Target Page 57 Client-Side Versus Server-Side Redirection .60 Exposing Values to Another Page via References 62 The Event Handlers That Call the Server.Transfer Method .63 The Public Properties in the Main Page .64 The Target Page for the Server.Transfer Method .65 Changing the Method and Clearing the Request Collections .67 The Server.Execute Method .68 Capturing Output from the Server.Execute Method .69 The Target Page for the Server.Execute Method .70 Summary .72 00 0672326744 FM 5/4/04 12:25 PM Page iv Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. 3 Loading Progress and Status Displays 75 Displaying a “Please Wait” Page 76 A Simple “Please Wait” Example 77 Displaying a Progress Bar Graphic .85 The Progress Bar Animated Graphic Files 86 Displaying the Progress Bar Graphic .87 Implementing a Staged Page Load Process 92 The Steps in Implementing a Staged Page Load Process .92 Status Information in ASP.NET and the XMLHTTP Object .93 The Staged Process Operation Page .94 The Staged Process Main Page in the Staged Loading Example 98 Summary .107 4 Working with Nested List Controls 109 Displaying Related Data in Nested DataGrid Controls 110 Declarative Nested Binding to a DataSet Instance .110 Filling Nested DataGrid Controls with a DataSet Instance .119 Declarative Nested Binding to a Custom Function 125 Filling Nested DataGrid Controls from a DataReader Instance .128 A Master/Detail Display with DataList and DataGrid Controls 134 Declaring the DataList and DataGrid Controls 135 Populating the DataList Control 140 Populating the DataGrid Control 143 Selecting a Row in the DataList Control 143 Editing a Row in the DataGrid Control .145 Updating the Original Data in the Database .149 Summary .150 Part II Reusability 5 Creating Reusable Content 155 Techniques for Creating Reusable Content .156 Server-Side Include Files 156 ASP.NET User Controls 158 Custom Master Page and Templating Techniques .162 ASP.NET Server Controls Built As .NET Assemblies .163 Using COM or COM+ Components via COM Interop 166 Building a ComboBox User Control .169 Design Considerations .169 The HTML for a Drop-Down Combo Box .170 00 0672326744 FM 5/4/04 12:25 PM Page v Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. ASP.NET 1.1 Insider Solutions vi The Structure and Implementation of the ComboBox User Control .173 Outputting the Appropriate HTML 175 The ShowMembers Method 176 Public Property Accessor Declarations .176 The Property Accessors for the ComboBox User Control .178 The Page_Load Event Handler for the ComboBox Control 183 Using the ComboBox Control .189 Populating the ComboBox Controls from an ArrayList Instance 191 Displaying the Members of the ComboBox User Control .192 Displaying Details of the Selected Item .192 Setting the Properties of the ComboBox User Control 193 Populating the ComboBox Control 194 Summary .196 6 Client-Side Script Integration 197 Client-Side Interaction on the Web .198 Client-Side Scripting in the Browser 199 CSS2 and Dynamic HTML .199 Selecting Your Target .200 Version 6 Browser-Compatible Code Techniques 201 The Client-Side Code in the ComboBox User Control 203 Useful Client-Side Scripting Techniques 207 Buttons, Grids, and Client-Side Script 208 Detecting and Trapping Keypress Events .211 Creating a MaskedEdit Control .218 Using the MaskedEdit Control .224 Creating a One-Click Button 230 Summary .240 7 Design Issues for User Controls 243 The Effect of User Controls on Design and Implementation .244 Converting the MaskedEdit Control Page to a User Control .245 Adding Validation Controls to the MaskedEdit Control 251 Building a SpinBox User Control 254 The User Interface Declaration for the SpinBox Control 255 The Private and Public Members of the Control 256 The Server-Side Code Within the SpinBox Control 261 Integrating Client-Side Script Dialogs .267 How the Client Dialogs Example Works .269 The clientdialog.ascx User Control 269 00 0672326744 FM 5/4/04 12:25 PM Page vi Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. vii Contents Browser-Adaptive Script Dialogs .274 How the Adaptive Client Dialogs Example Works 276 Integrating Internet Explorer Dialog Windows 283 How the Modal Dialog Window Example Works 285 The Internet Explorer showModalDialog Method .285 Browser-Adaptive Dialog Windows 290 How the Browser-Adaptive Dialog Window Example Works 291 Summary .294 8 Building Adaptive Controls 297 The Advantages of Server Controls 298 The Basics of Building Server Controls 298 The Process of Building a Server Control .299 The Life Cycle of ASP.NET Controls 299 The Life Cycle of a Server Control .300 Creating a Class for a Server Control 301 Choosing and Extending a Base Class 302 Building a MaskedEdit Server Control 305 The MaskedEdit Control Class File 305 Compiling and Testing the MaskedEdit Control .312 Building a SpinBox Server Control 315 The Standard SpinBox Control Class File .316 Using an Adaptive SpinBox Control .334 Making the SpinBox Control Adaptive .335 Coping with Older and Nonstandard Browsers .337 Adaptability Changes to the SpinBox Control Class 339 Testing and Using an Adaptive SpinBox Control .346 Installing a SpinBox Control in the GAC 348 Changes to the SpinBox Control Class File for GAC Installation 349 Compiling the SpinBox Control Class File 349 Installing the SpinBox Assembly into the GAC 350 Testing the GAC-Installed Control 351 Summary .352 9 Page Templates 353 Designing for Consistency .354 Templating Solutions .355 A Simple Layout Server Control 355 Custom Layout Control Output 357 Creating Content from a Custom Control .358 Creating a Custom Layout Control .360 00 0672326744 FM 5/4/04 12:25 PM Page vii Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. ASP.NET 1.1 Insider Solutions viii A Server Control That Uses Templates .365 Creating a Templated Server Control .366 Creating Default Content for Templates 371 Creating Dynamic Regions for Page Content .372 Using a Custom Page Class for a Page Template 373 Creating the Content and ContentPlaceHolder Controls 373 Creating a Custom Page Class .374 Creating a Master Page 378 Using a Custom Page Class .379 Using Custom Controls in Visual Studio .NET .380 Summary .381 Part III Data Techniques 10 Relational Data-Handling Techniques 385 Using Parameters with SQL Statements and Stored Procedures 386 Using Submitted Values in a SQL Statement .386 Ordering of Stored Procedures and Query Parameters 392 Using Default Values in a Stored Procedure .393 Filling a DataSet Instance With and Without a Schema .400 Loading the Schema for a DataSet Instance 400 The Sample Page for Filling a DataSet Instance .401 Writing Provider-Independent Data Access Code .410 Dynamically Instantiating a .NET Framework Class .410 The Code in the Provider-Independent Data Access Sample Page .411 Updating Multiple Rows by Using Changed Events 415 The Edit and Cancel Buttons 418 Populating the DataGrid Control 419 Handling the ItemDataBound Event .420 Handling the Changed Events .422 Updating the Source Data 424 Creating the Client-Side Script to Highlight a Control 426 Summary .427 11 Working with XML Data 429 The Role of XML in ASP.NET .430 XML API Pros and Cons 430 The Forward-Only API: XmlTextReader 431 The DOM API: XmlDocument .431 The Cursor-Style API: XPathNavigator .432 The XML Serialization API: XmlSerializer 432 00 0672326744 FM 5/4/04 12:25 PM Page viii Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. Combining the XmlTextReader and XmlTextWriter Classes 433 Parsing XML Strings 437 Accessing XML Resources by Using the XmlResolver Class .438 XmlResolver, Evidence, and XslTransform 439 Searching, Filtering, and Sorting XML Data .442 Searching and Filtering XML Data .442 Sorting XML Data 446 Creating a Reusable XML Validation Class .456 Converting Relational Data to XML .460 Customizing XML by Using the DataSet Class 461 Adding CDATA Sections into XML Documents .464 Simplifying Configuration by Using XML 466 Accessing Configuration Settings by Using XPathNavigator 467 Using XML Serialization 470 Summary .474 Part IV Hosting and Security 12 Side-by-Side Execution in ASP.NET 479 How Version 1.1 of the .NET Framework Is Distributed 480 How Installing a New Version of the .NET Framework Affects Existing Applications .481 Configuration Settings in machine.config .481 The ASP.NET State Service and SQL Server State Service .481 The ASP.NET Process Account .482 Windows Performance Counters .482 Running Version 1.0 Applications on Version 1.1 of the .NET Framework 482 Running Version 1.1 Applications on Version 1.0 .488 How ASP.NET Selects the Runtime Version .488 How to Specify the ASP.NET Version for Individual Applications 489 Installing ASP.NET Without Updating Script Mappings .489 Using the aspnet_regiis.exe Tool to Configure Runtime Versions .490 ASP.NET and IIS 6.0 on Windows Server 2003 .492 IIS 6.0 Web Service Extensions 493 IIS 6.0 Application Pools 494 Summary .497 13 Taking Advantage of Forms Authentication 499 Building a Reusable Sign-in Control .500 Hashing Passwords 506 ix Contents 00 0672326744 FM 5/4/04 12:25 PM Page ix Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. [...]... Wahlin Consulting and founded the XML for ASP.NET Developers Web site (www.XMLforASP.NET), which focuses on using XML and Web services in the NET platform In addition to consulting, Dan is also a corporate trainer/speaker, and he teaches XML and NET training courses around the United States Dan coauthored ASP.NET: Tips, Tutorials, and Code and authored XML for ASP.NET Developers (both from Sams Publishing)... concerned with installing, setting up, and using ASP.NET This includes side-by-side execution of different versions, ASP.NET forms authentication, and general security configuration issues Who This Book Is For This book is for developers who are using ASP.NET and have a reasonable grasp of the basic topics for building Web pages and Web applications in ASP.NET It is not designed to act as a 2 Introduction... properties and methods, how to expose functionality from a control, and how to use that control in Web pages and applications What You Need to Use This Book This book covers ASP.NET 1.1, and you must be running this version of ASP.NET to use the sample code that is available for download The examples are not designed for use in Visual Studio NET, which means that you can use them (and edit them to... I, “Web Form User Interfaces,” is a combination of many useful techniques for solving issues that ASP.NET developers regularly face The chapters in this part include tips and tricks with Web forms and information on cross-page posting, displaying progress and status information, and working with nested ASP.NET list controls n Part II, “Reusability,” demonstrates how you can create reusable content for... approach of presenting readers with real-world solutions in the context of real applications He also worked with the Microsoft ASP.NET team to help programmers learn more about the fantastic technology they created and contributed as an author to the highly respected Professional ASP.NET Security, now sadly out of print Two years, four job titles, and far too many books later, Dan decided to leave Wrox... applications in ASP.NET It is not designed to act as a 2 Introduction beginner’s guide or as a comprehensive reference to all the techniques available in ASP.NET However, the topics that it does cover are introduced in sufficient depth that a reasonably experienced ASP.NET user will be able to learn and take advantage of the techniques described For example, Chapter 5, “Creating Reusable Content,” explains what... www.samspublishing.com Type the ISBN (0672326744) or the title of the book into the Search field to find the page you’re looking for Introduction Are you getting the most from ASP.NET? While it’s easy to build quite complex pages quickly and easily with ASP.NET, if you acquire a more intimate knowledge of the NET Framework as a whole, you can really take advantage of the great features it provides This book explores... Displays 4 Working with Nested List Controls 1 Web Forms Tips and Tricks We start this chapter by looking at some of the more unusual ways you can use the ASP.NET validation controls, such as within a list control—something that comes up regularly on ASP.NET mailing lists and forums Next, we take a brief look at creating something other than the standard layout in a DataGrid control We show a couple examples... Setting the Width of All the Columns 33 Loading Controls Dynamically at Runtime 38 Summary 49 8 1 Web Forms Tips and Tricks Getting More from ASP.NET Validation Controls Many developers do not realize just how versatile the validation controls provided with ASP.NET are The common scenario is to use them to validate the contents of a text box, a task that they are ideally suited to However, you can... appear below the list control rather than next to it Specifying a meaningful message, rather than just an asterisk, makes it easier to see where the error is, as shown in Figure 1.1 9 10 1 Web Forms Tips and Tricks FIGURE 1.1 Validating String and Numeric Values If you use the comparison type “String” when values are numeric, you’ll get inaccurate results For example, if the MinimumValue property of . ASP. NET 1. 1 00 0672326744 FM 5/4/04 12 :25 PM Page i TEAM LinG Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. ASP. NET 1. 1. to remove this watermark. ASP. NET 1. 1 Insider Solutions vi The Structure and Implementation of the ComboBox User Control .17 3 Outputting the Appropriate