About the Book
The complete guide to developing
professional, reusable ASP.NET server controls and components—direct from
the insiders who developed the technology
Web Forms—the Page and Control Framework
at the heart of ASP.NET—makes it easier to develop dynamic Web applications.
But you can go beyond the controls that ship with ASP.NET—and power up
your Web sites and applications—by creating your own server controls. You
can also develop and distribute your own controls for commercial use. This
comprehensive guide, direct from key insiders who really know the technology,
combines conceptual and architectural details with practical, how-to information
and real-world code samples to show exactly how to create custom, reusable,
professional-quality server controls with rich design-time functionality.
It also provides essential information about configuration and the HTTP
runtime, packaging, deployment, debugging, and developing controls that
incorporate XML Web services, plus other vital topics. And it introduces
the ASP.NET Web Matrix tool for creating Web applications and for using
server controls. Topics covered include:
OVERVIEW
• ASP.NET page programming
• Component programming
SERVER CONTROLS
• User controls
• Implementing properties
• View state management
• Rendering
• Control life cycle
• Events and Postback
• Styles
• Composite, validator, and data-bound
controls
• Client-side behavior
• Design-time functionality
• Localization, licensing, and deployment
SERVER COMPONENTS
• XML Web services
• HTTP handlers
SERVER CONTROL CASE STUDIES
• Templated data-bound controls
• DHTML-based server controls
“[This book] is a must-have for all serious
ASP.NET component developers.”
–Scott Guthrie, Product Unit Manager,
Microsoft ASP.NET Product Team
Related Books
Introducing ASP.NET 2.0
Understanding Web Services Specifications
and the WSE
Microsoft® ASP.NET Coding Strategies
with the Microsoft ASP.NET Team
Table of Contents
Foreword
xvii
Acknowledgments
xix
Introduction
xxi
PART I OVERVIEW
1 ASP.NET Overview
3
The .NET
Framework 3
The Common Language Runtime 4
The .NET Framework Class Library 5
A Quick
Tour of ASP.NET 6
HTTP
Runtime—Request Processing in an ASP.NET Application 8
Extending
ASP.NET 10
2 Page Programming Model
13
A Sample
Page 14
Server
Controls 15
ASP.NET Server Control Hierarchy 18
ASP.NET Web Controls 18
Why Write Server Controls? 19
From
Text to Controls 21
Code
Model 22
Page
Execution Model 23
ASP.NET
Pages in a Visual Designer 26
3 Component Programming Overview
29
Managed
Component Overview 30
Properties
31
The Property Construct 32
Naming Guidelines for Properties 36
Advantages of Properties 36
Methods
37
Implementing Methods Instead of Properties 37
Naming Guidelines for Methods and Method Parameters 38
Events
39
Delegates 39
Event Delegates 42
Raising an Event 44
Event Sample 46
Metadata
Attributes 51
Applying Attributes 52
Designable
Components 53
Controls 54
PART II SERVER CONTROLS—FIRST
STEPS
4 User Controls: From Page to
Control 59
Converting
a Page to a User Control 60
Accessing a User Control from a Page 62
Reusable
User Controls: The SiteHeader and SiteFooter Examples 64
The User
Control Model 67
User Controls in Visual Studio .NET 68
Deploying
a User Control 73
An Application-Specific
User Control: The CruiseSelector Example 73
Relative
URLs in User Controls 77
Programmatically
Instantiating a User Control 79
Caching
a User Control 80
VaryByControl Example 83
How User Control Caching Works 85
5 Developing a Simple Custom
Control 87
Server
Control Overview 88
A Trivial
Server Control Example 89
Compiling
and Deploying a Server Control 90
Using a Custom Control on a Page 92
Exposing
a Property: The PrimeGenerator Control Example 93
Deriving from WebControl 96
Test Page for the PrimeGenerator Control 97
Choosing
the Base Class for Your Control 99
Applying
Design-Time Attributes 100
Applying TagPrefixAttribute 101
Custom
Controls in Visual Studio .NET 102
Adding a Custom Control to the Toolbox 102
Using Custom Controls from the Toolbox 103
Customizing the Toolbox Icon 105
Debugging
a Server Control 106
6 Custom Controls vs. User Controls
109
Deployment
109
Authoring
110
Content
and Layout 111
Design-Time
Behavior 112
Performance
112
PART III SERVER CONTROLS—NUTS
AND BOLTS
7 Simple Properties and View
State 117
View
State and State Management 118
Using ViewState as the Property Store—The ViewStateDemoLabel Example
118
More About View State 122
Declarative
Persistence of Simple Properties 125
Declarative Syntax for Enumeration Properties 126
Properties
Inherited from Control and WebControl 127
Control Properties 127
WebControl Properties 128
Design-Time
Attributes for Properties 130
Overriding an Attribute 132
Putting
It Together—The PageTracker Example 132
Page That Uses the PageTracker Control 138
8 Rendering
141
Base
Classes and Rendering 142
Rendering
Methods of the Control Class 143
Page Rendering 144
Overriding
Render—The MetaTag Control Example 145
HtmlTextWriter
and Related Enumerations 148
HtmlTextWriterTag Enumeration 149
HtmlTextWriterAttribute Enumeration 149
HtmlTextWriterStyle Enumeration 149
Rendering
Methods of WebControl 149
Rendering
a Web Control—The HoverLabel Example 151
Exercising
HtmlTextWriter—The LoginUI Example 155
Downlevel
Rendering 167
9 Control Life Cycle, Events,
and Postback 173
Control
Life Cycle 174
Events
in Server Controls 183
Declarative Syntax for Event Wiring 184
Postback
Architecture 185
Mapping
a Postback Event to a Server Event—The IPostBackEventHandler Interface
186
Implementing IPostBackEventHandler—The SimpleButton Example 187
Optimizing
Event Implementation 191
Generating
Client-Side Script for Postback 194
Rendering Multiple Elements That Use Client Script for Postback—The NavButtons
Example 199
IPostBackDataHandler
and Postback Data 203
Processing Postback Data—The SimpleTextBox Example 204
Putting
It Together—The Login Example 207
Exposing
Other Semantic Events—The HitTracker Example 212
10 Complex Properties and State
Management 217
Subproperties
218
Subproperties Persisted on a Control's Tag 219
Inner Property Persistence 220
Properties
and Type Converters 222
Implementing a Type Converter 223
Putting It Together—The MapDemo Example 236
State
Management, IStateManager, and the ViewState Dictionary 245
How a Page Serializes and Restores Its View State 245
The IStateManager Interface 246
The ViewState Property and Default State Management 247
Implementing
IStateManager in a Custom Type 249
Collection
Properties—The ImageMap Example 255
Implementing State Management in a Collection Type—The HotSpotCollection
Example 264
Expando
Attributes—The IAttributeAccessor Interface 271
11 Styles in Control
275
Styles
Overview 275
Overriding
Style Properties—The Spreadsheet Example 279
Implementing
a Custom Typed Style—The MyPanelStyle Example 283
12 Composite Controls
293
Composite
Controls—Key Concepts 294
Implementing
a Composite Control—The CompositeLogin Example 296
APIs
Related to Composite Controls 308
View
State and Child Controls 310
Event
Bubbling 311
Styles
in Composite Controls—The StyledCompositeLogin Example 313
Templated
Controls Overview 320
Implementing
a Templated Control—The ContactInfo Example 321
Control
Parsing and Control Builders 331
13 Client-Side Behavior
339
Browser
Capabilities and Client-Side Behavior 340
Client
Script-Related API 341
Using
Client Script and DHTML 342
Deploying
Client Files 343
Putting
It Together—The HtmlEditor Example 345
14 Validator Controls
359
Validation
Architecture—IValidator, BaseValidator, and CustomValidator 360
The Validation
Framework 362
Validator Controls Provided by ASP.NET 362
Using Validation in a Page 362
How the Page Performs Validation 363
Client-Side Validation 364
The ValidationSummary Control 364
Validating
Text Entry—The StringValidator Example 365
15 Design-Time Functionality
375
.NET
Framework Design-Time Architecture 376
Type
Converters 380
TypeConverterAttribute 381
Designers
381
Composite Control Designers—The CompositeControlDesigner Example 384
Templated Control Designers—The ContactInfoDesigner Example 386
Read-Write Control Designers—The ScrollablePanelDesigner Example 394
Designer Verbs 399
DesignerAttribute 399
UI Type
Editors 401
String Editor Example 401
Collection Editor Examples 406
EditorAttribute 407
Component
Editors 408
The MyLabelComponentEditor Example 408
Designer Verb That Initiates Component Editing 415
Debugging
Design-Time Code 418
16 Data-Bound Controls
421
Data
Binding Overview 422
BindableAttribute and the DataBindings Property 424
Implementing
a Data-Bound Control 424
The DataSource Property and Related Members 433
Creating the Control Hierarchy—DataBind and CreateChildControls 434
Styles and Rendering 436
Implementing
a Data-Bound Control Designer 442
17 Localization, Licensing, and
Other Miscellany 451
Localization
451
Using Resources in Controls 452
Embedding and Accessing Resources 456
Localizing Metadata Attributes 460
Licensing
463
ServerLicenseProvider 467
ExpiringLicenseProvider 473
EncryptedLicenseProvider 475
Configurable
Properties 477
Deployment
483
PART IV SERVER COMPONENTS
18 XML Web Services
489
XML Web
Services Overview 489
Developing
an XML Web Service—The StockWebService Example 490
Implementing the Web Service 491
Deploying the Web Service 497
Developing Web Service Client Proxies 499
XML Web
Service-Based Server Controls—The StockGrid Example 500
Implementing the Server Control 502
Implementing the Control Designer 515
19 HTTP Handlers
519
HTTP
Handler Overview 519
The IHttpHandler Interface 521
The IHttpHandlerFactory Interface 522
The "Hello,
World" HTTP Handler Example 522
Handling
a New Extension—The XmlHandler Example 525
Dynamic
Images—The ImageLabelHandler Example 532
The ImageLabel Control and Designer 538
HTTP
Handlers and Session State 548
PART V SERVER CONTROL CASE STUDIES
20 Data-Bound Templated Controls
553
The ListView
Control 554
ListView
Specification 557
ListView
Implementation 558
Data-Bound Controls 559
Templates 568
Styles and State Management 571
Rendering 575
Events 580
Client Script Functionality 585
Design-Time Attributes 587
Designer Implementation 590
21 DHTML-Based Server Controls
603
The DatePicker
Control 604
DatePicker
Specification 607
DatePicker
and DatePickerDesigner Implementation 608
Composite Control 608
Delegated Properties 610
Styles and State Management 612
Client Script Functionality 615
Rendering 620
Events 622
Validation 624
Design-Time Attributes 625
Designer Implementation 627
PART VI APPENDIXES
A Metadata Attributes
633
B Object Model for
Common Classes 643
C Microsoft ASP.NET
Web Matrix 653
INDEX 661 |