Aug 6, 2007

Adding Client Behaviors to Web Server Controls by Using ASP.NET AJAX Extensions

A behavior adds functionality to client Document Object Model (DOM) elements. The client behavior can be encapsulated in a Web server control as an extender control. The extender control is then associated with one or more types of ASP.NET server controls to add the behavior to those server controls. You can associate more than one extender control with an ASP.NET server control. An extender control is a web server control that inherites the ExtenderControl. GetScriptDescripters returns a collection of ScriptDescriptor objects that contain information about instances of client components that are used with web server control. This includes the client type to create, the properties to assign, and the events to add handlers for. See the script example below, they will be generated according to this information. GetScriptReferences() returns a collection of ScriptReference objects that contains information about the client-script libraries to be include with then control. The client-script libraries define the client types and include any other javascript code that is required for the control.

            protected override IEnumerable GetScriptDescriptors(Control targetControl)
            ScriptBehaviorDescriptor descriptor = new ScriptBehaviorDescriptor("Samples.FocusBehavior", targetControl.ClientID);
            descriptor.AddProperty("highlightCssClass", this.HighlightCssClass);
            descriptor.AddProperty("nohighlightCssClass", this.NoHighlightCssClass);

            return new ScriptDescriptor[] { descriptor };