
Implementing a Client Component Derived from the Component Class
To implement a custom client component derived from Component, you follow these steps:
Define a component class using the prototype design pattern.
Initialize the component's base Component instance.
Expose any property accessors, and optionally raise a propertyChanged notification event.
Override the dispose method to release resources, such as clearing event handlers.
The following sections provide details about implementation steps.
Defining a Component Class by Using the Prototype Design Pattern
An ASP.NET AJAX client class, which includes a component class, is defined in JavaScript using the prototype design pattern. To define a component class by using the prototype design pattern, you do the following:
Register the namespace for your component class.
Create the component's constructor function, and in the constructor function, define any private fields and set their initial values.
Define the component's prototype.
Register the component function as a class derived from Component.
For more information, see Creating a Client Component Class Using the Prototype Model.
Initializing the Base Class
In the component's constructor function, you invoke the inherited Type.initializeBase method to initialize the base type of your registered class. A non-visual component class is registered as a class that has a base type of Component. When the Component base class is initialized, its methods are available to the component, and it automatically registers the component as a disposable object with the AJAX-enabled ASP.NET application. For more information, see Sys.IDisposable Interface.
Any component class that derives from Component must initialize its base class from the constructor. You typically call initializeBase before any other code runs in the constructor. The following example shows the constructor function of a non-visual component that derives from Component.
Samples.SimpleComponent = function()
{
Samples.SimpleComponent.initializeBase(this);
}
Defining Properties and Raising Property-change Notifications
Initializing Properties and Event Listeners
If your custom component must initialize any properties or event listeners, you should override the Sys.Component.initialize method in the component's prototype. For example, a non-visual component that derives from Component might assign a delegate to an event such as window.onFocus. As a final step, you call the base initialize method to enable the component's base class to complete initialization.
ASP.NET provides classes and methods to provide standard event management for components and for DOM elements. To manage your component's events, use the Sys.EventHandlerList class. For example, bind events by using the Sys.EventHandlerList.addHandler method, and release them by using the Sys.EventHandlerList.removeHandler method. For more information, see Sys.EventHandlerList Class.
To manage event handlers for DOM elements or for the window object, use the Sys.UI.DomEvent class. For example, you can bind and unbind event handlers by using the Sys.UI.DomEvent addHandler and Sys.UI.DomEvent removeHandler methods. For more information, see Sys.UI.DomEvent Class.
Releasing Resources
If your custom component must release resources before the component is disposed, override the dispose method and release the resources in the overridden method. This makes sure that the resources are released immediately before the component is disposed. Resources that should be released might include handlers for DOM events. By verifying that any possible circular references between DOM elements and the component object are removed, you make sure that the object can be removed from memory. For more information, see Releasing Component Resources.