ProfessionalWeb Parts and Custom Controls with ASP.NET 2.0 - Pdf 10


Professional
Web Parts and Custom Controls
with ASP.NET 2.0
Peter Vogel
01_57860x ffirs.qxd 10/4/05 9:29 PM Page iii
Professional
Web Parts and Custom Controls
with ASP.NET 2.0
01_57860x ffirs.qxd 10/4/05 9:29 PM Page i
01_57860x ffirs.qxd 10/4/05 9:29 PM Page ii
Professional
Web Parts and Custom Controls
with ASP.NET 2.0
Peter Vogel
01_57860x ffirs.qxd 10/4/05 9:29 PM Page iii
Professional Web Parts and Custom Controls
with ASP.NET 2.0
Published by
Wiley Publishing, Inc.
10475 Crosspoint Boulevard
Indianapolis, IN 46256
www.wiley.com
Copyright © 2006 by Wiley Publishing, Inc., Indianapolis, Indiana
Published simultaneously in Canada
ISBN-13: 978-0-7645-7860-1
ISBN-10: 0-7645-7860-X
Manufactured in the United States of America
10 9 8 7 6 5 4 3 2 1
1B/SR/RQ/QV/IN
Library of Congress Cataloging-in-Publication Data

READERS SHOULD BE AWARE THAT INTERNET WEBSITES LISTED IN THIS WORK MAY HAVE CHANGED OR DIS-
APPEARED 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 (800) 762-2974, outside the United States at (317) 572-3993 or fax (317) 572-4002.
Trademarks: Wiley, the Wiley logo, Wrox, the Wrox logo, Programmer to Programmer, and related trade dress are trade-
marks or registered trademarks of John Wiley & Sons, Inc. and/or its affiliates, 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. Wiley
Publishing, Inc., is not associated with any product or vendor mentioned in this book.
Wiley also publishes its books in a variety of electronic formats. Some content that appears in print may not be available in
electronic books.
01_57860x ffirs.qxd 10/4/05 9:29 PM Page iv
About the Author
Peter Vogel (MBA, MCSD) is a principal in PH&V Information Services. PH&V provides consulting ser-
vices in client/server and Web development. Its clients include Volvo, Christie Digital, the Canadian
Imperial Bank of Commerce, the Government of Ontario, and Microsoft. Peter’s white papers appeared
in the Visual Studio .NET and Office 2003 release package. Peter is the editor of the Smart Access news-
letter from Pinnacle Publishing, and wrote The Visual Basic Object and Component Handbook, which has
been called “The definitive guide to ‘thinking with objects.’” Peter was the founding editor of the XML
Developer newsletter. In addition to teaching for Learning Tree International, Peter wrote their ASP.NET
1.1, ASP.NET 2.0, and Technical Writing courses. His articles have appeared in every major magazine
devoted to VB-based development and can be found in the Microsoft Developer Network libraries.
Peter lives in Goderich, Ontario, Canada, and presents at conferences all over the world, frequently as
the keynote speaker.
01_57860x ffirs.qxd 10/4/05 9:29 PM Page v
01_57860x ffirs.qxd 10/4/05 9:29 PM Page vi
Credits
Senior Acquisitions Editor
Jim Minatel
Development Editor
Sara Shlaer

TECHBOOKS Production Services
01_57860x ffirs.qxd 10/4/05 9:29 PM Page vii
01_57860x ffirs.qxd 10/4/05 9:29 PM Page viii
This book is for my beautiful sons, in order of appearance:
Christopher, Jamie, and Jason.
01_57860x ffirs.qxd 10/4/05 9:29 PM Page ix
01_57860x ffirs.qxd 10/4/05 9:29 PM Page x
Contents
Acknowledgments xix
Introduction xxi
Part I: Introducing Controls 1
Chapter 1: Creating Your Own Controls 3
The Three Kinds of Controls 3
User Controls 4
Custom Controls 5
Web Parts 5
The Benefits of Reusable Controls 6
Beyond Reusability with Web Parts 7
Allowing Customization with Web Parts 7
Implementing Reusability with Controls 9
Controls in Action 10
Exploring the Different Kinds of Controls 13
When to Use a User Control 15
When to Use a Custom Control 15
Web Parts in Action: Customization 16
Providing for Personalization 18
Understanding the Personalization Framework 19
Summary 21
Chapter 2: Creating Customizable Pages 23
The Web Part Framework Controls 24

Breaking up the Render Method 87
Supporting AutoPostback 88
Handling Returned Data 92
Controlling Related HTML 96
Design-Time HTML 96
Run-Time HTML 98
Extracting and Controlling WebControl Properties 101
Server-Side Properties 102
Determining the Tag 102
Managing Your Custom Control’s Style 103
Themes and Skins 104
Using Cascading Stylesheet Classes 104
Updating the Style 105
Dealing with Design Mode 113
Managing Code at Design Time 113
Controlling How Visual Studio Handles Your Control 114
Summary 115
02_57860x ftoc.qxd 10/4/05 9:30 PM Page xii
xiii
Contents
Chapter 4: Building User Controls 117
User Controls, WebForms, Custom Controls 117
Starting a User Control 118
Writing Code for Your Control 122
Simulating a Constructor 123
Dynamic Interfaces 124
Using Web Parts 125
Working with a User Control 126
Accessing the Control 129
User Control HTML 130

Contents
Chapter 6: Maintaining State with the ViewState 159
Using the ViewState 160
Accessing the ViewState Directly 160
Managing ViewState Efficiently 162
Managing State for Controls 166
Clearing State 170
Creating Case-Sensitive Keys 171
Integrating with the Cache 172
Serializing Objects Efficiently 175
Using Your TypeConverter 178
Associating a TypeConverter with a Class 179
Summary 180
Chapter 7: Developer Tools 181
Debugging Your Controls at Design Time 181
Deploying Controls 183
Deployed Web Site Formats 184
Updating User Controls 186
Updating Custom Controls and Web Parts 187
Sharing Custom Controls and Web Parts 189
Strong Names 192
Licensing Your Control 195
Managing the Personalization Subsystem 200
Identifying the User 200
Setting up Personalization Providers 203
Summary 204
Part III: Extending Controls 205
Chapter 8: Adding Business Functionality 207
Factoring Your Code 208
Methods and Events 208

Static and Dynamic Client-Side Code 249
Triggering Client-Side Code 250
Dynamically Generating Code 253
Support for Client-Side Script 256
Building CallBack Functions 260
Specialized Controls 270
Validator Controls 271
Templated Controls 278
Databinding to Multiple Records 283
Databinding Properties 283
Retrieving Data 285
Displaying Data 288
Performing Updates 290
Design-Time Support 292
Handling Complex Properties 292
Designers 310
Summary 318
02_57860x ftoc.qxd 10/4/05 9:30 PM Page xv
xvi
Contents
Chapter 10: Communicating Between Web Parts 319
Using Connectable Parts 319
Setting up the Page 320
Making Connections 320
Managing Connections 323
Creating Connectable Web Parts 324
Creating a Provider Web Part 324
Creating a Consumer Part 327
A Two-Way Street 328
Predefined Interfaces 330

Chapter 12: A Custom Control Case Study 385
Designing the Control 386
Setting Up the Solution 386
Adding Controls 389
Setting the Default Style 394
Positioning the Controls 395
Overriding the Render Method 395
Using a Table 397
Using Absolute Positioning 400
Switching Between Display and Update Modes 403
Tailoring the Control for the Developer 407
Saving State 409
Defining a Data Structure for Saving State 409
Saving to the ControlState 410
Retrieving User Data 412
Raising an Event 415
Defining a Custom Event Arguments Object 416
Defining the Event 418
Raising the Event 418
Supporting the Next Control Developer 419
Displaying User Data on Postback 421
Exposing and Accepting Data 423
Supporting Customization 425
Adding Verbs 427
Communication 429
Defining an Interface 429
Implementing the Provider 430
Implementing the Consumer 431
Summary 432
Index 433

the information you need to create custom controls, user controls, and Web Parts using any text editor is
also included). Visual Studio 2005 provides you with the best support for creating ASP.NET applications
and creating your own controls.
The approach in this book is simple: “If you want this, code this; if you code this, you’ll get this.”
Throughout the book, the emphasis is on the code that you need to create controls that you can use
when building Web applications. There’s no code in here, for instance, on how to calculate the square
root of every number less than 2,000. The code in this book is based on business-related applications.
This also means that this book does not provide much background on how ASP.NET works, unless it
makes a difference to you when you’re writing code. If you’re familiar with how ASP.NET works “under
the hood,” that knowledge will deepen your understanding of the material in this book. On the other
hand, if you’ve been able to create applications without knowing that material — well, you won’t need
to know it to understand the examples in this book, either.
Where there is more than one way to accomplish a goal, I describe the costs and benefits of each method
and identify the typical scenarios in which you would use each technique so that you can make an
informed decision about what you want to do.
There’s probably material in this book that you won’t need, at least not initially. But when you do need
that information, it’s in here with lots of examples (in Visual Basic 2005 and C#) to show you how to do
it. For instance, when you first build a custom control you may not intend to distribute that control out-
side of your company. As a result, you won’t need to read the section on licensing. If, later on, you need
that material, it’s here (in Chapter 7, to be exact).
03_57860x flast.qxd 10/4/05 9:19 PM Page xxi
xxii
Introduction
Whom This Book Is For
This book is written for the professional Web developer. I assume that you know how to create a
WebForm and now want to take advantage of the features to create your own controls. I do not, how-
ever, assume that you are an experienced object developer. If you do have some experience with creating
objects, you’ll find a few pages of review material in here covering basic object-oriented (OO) concepts
and how to implement them in Visual Basic 2005 and C#. I also assume that you’re new to creating
objects in Visual Studio 2005, so I’ve included some information at the start of Chapter 3 on how to set

you will use with your Web Part.
03_57860x flast.qxd 10/4/05 9:19 PM Page xxii
xxiii
Introduction
❑ Chapter 3, “Creating Custom Controls”: This chapter covers the essentials of implementing a
custom control. You see both how to create a control that adds HTML to a page, and how to cre-
ate a control that incorporates other ASP.NET controls. This chapter also shows you how to set
up your custom control project in Visual Studio 2005.
❑ Chapter 4, “Building User Controls”: If you know how to build a WebForm, you know most of
what you need to know to create a user control. This chapter builds on your knowledge of creat-
ing WebForms so that you can create user controls for your Web application.
❑ Chapter 5, “Building Web Parts”: Web Parts are a new feature in ASP.NET 2.0. This chapter
shows you how to create a Web Part by leveraging your knowledge of creating custom controls,
and how to extend the framework by adding new functions to your Web Part. You also learn
how to enable customization for properties on custom controls.
❑ Chapter 6, “Maintaining State with the ViewState”: A key issue in any Web application is
keeping track of information between a user’s requests for pages. ASP.NET 2.0 provides some
new features for managing state in your controls. This chapter introduces you to all of the
ASP.NET tools — both the tools available in previous versions of ASP.NET and the new features
of ASP.NET 2.0.
❑ Chapter 7, “Developer Tools”: Of course, building a control is only the beginning of your con-
trol’s life — you still have to deploy it and update it. In this chapter you see how to deploy your
control both publicly and privately. You also find out how you can update your controls without
having to redeploy your application. This chapter also discusses how to program the personal-
ization system that manages Web Parts.
❑ Chapter 8, “Adding Business Functionality”: Now that you’ve built a custom control, you’ll
want to incorporate some of your application’s functionality into it. This chapter shows you
how to add properties, methods, and events to your custom control. You also see where in the
life cycle of a control you should put your application-related code.
❑ Chapter 9, “Adding Advanced Functionality”: This chapter covers a variety of specialized top-


Nhờ tải bản gốc

Tài liệu, ebook tham khảo khác

Music ♫

Copyright: Tài liệu đại học © DMCA.com Protection Status