Online Book Reader

Home Category

Programming Microsoft ASP.NET 4 - Dino Esposito [267]

By Root 5633 0
Toolbox. Because of their compiled nature, custom controls can be installed in a single copy in the global assembly cache (GAC), making them available to all applications, or they can simply be deployed to the \Bin directory for use by a single application.

A custom control is a class and inherits from a base control class. The logic already available in the base class determines how much, and what, code you have to write. There are basically two ways of creating custom controls. If you find that an existing control meets your requirements only partially and lacks some key features, the simplest thing you can do is extend the control by deriving a new class from it. You can override specific properties, methods, and events as well as add new features. If none of the existing Web server controls meet your requirements, consider creating a custom control from scratch by deriving from one of the base control classes—Control and WebControl. These classes provide only the basic functionality of ASP.NET server controls, and they require that you take care of some of the control’s operational aspects yourself, such as rendering, styling, view state, and state management.

Note

Custom controls are not to be confused with user controls (ASCX files). Web user controls are dynamic-compile components and cannot be added to the Toolbox. In addition, user controls should be deployed as source code unless the application that incorporates them is precompiled. In this case, you can extract the dynamic assembly that contains the user control and share it between applications. However, this technique is not supported by Microsoft and, well, requires a lot of familiarity with the ASP.NET internals.

Extending Existing Controls


When you realize you need a custom control to accomplish a certain task, first pause and make sure the feature you devised can really be obtained with HTML, literals, and JavaScript code. If you know how to do that in pure HTML, you can start planning an ASP.NET control and then architect and engineer the feature for the best reusability and efficiency.

Choosing a Base Class


A custom server control is a Microsoft .NET Framework class that inherits—either directly or indirectly—from Control. Control is the root class for all server controls in ASP.NET applications. It should be noted, though, that very few controls that you commonly use in ASP.NET applications really inherit directly from Control. For the most part, ASP.NET controls inherit from intermediate classes that encapsulate a given predefined behavior.

Inheriting from a Base Class


Each ASP.NET server control that is not marked as sealed can be further inherited and specialized. Table 12-1 lists all the classes in ASP.NET that represent some sort of base functionality. Each class in the list represents the root of a family of controls.

Table 12-1. Base Control Classes Available in ASP.NET

Class

Description

BaseDataBoundControl

Incorporates the basic mechanism and object model for data binding. It inherits from WebControl.

BaseDataList

Adds grid capabilities such as advanced rendering, templates, and paging. It inherits from WebControl. This is considered deprecated in ASP.NET 4.

CompositeControl

Incorporates the mechanics of composite controls with regard to the building of the control’s tree. It inherits from WebControl.

CompositeDataBoundControl

Incorporates the mechanics of composite data-bound controls with regard to view-state management and the building of the control’s tree. It inherits from DataBoundControl.

DataBoundControl

Adds support for data source controls, and overrides some methods marked as abstract in the parent class. It inherits from BaseDataBoundControl.

HierarchicalDataBoundControl

Adds support for data hierarchical data source controls, and overrides some methods marked as abstract in the parent class. It inherits from BaseDataBoundControl.

ListControl

Adds support and an object model tailor-made for list controls, such as CheckBoxList and DropDownList.

WebControl

Adds an array

Return Main Page Previous Page Next Page

®Online Book Reader