A lot of features are packed into the ASP.NET AJAX framework. As a result, many common tasks can be accomplished using existing controls that handle making asynchronous calls to ASP.NET pages or web services. Many of the complexities associated with making asynchronous calls are hidden so that you can focus on building the application rather than on the message processing that happens behind the scenes. A perfect example of this is the UpdatePanel control, which makes it easy to add AJAX functionality into an ASP.NET page without writing a lot of code. Simply add an UpdatePanel to an ASP.NET page and you have instant AJAX functionality available even if you’ve never made an asynchronous call before in your life.
No matter how many features are built into a framework, there will always be times when you need to build custom controls that wrap complex functionality or refactor code that may have previously been duplicated in multiple places. Writing custom ASP.NET AJAX controls is quite different from writing standard ASP.NET controls because the bulk of a control’s functionality is defined in a JavaScript file as opposed to VB.NET or C# files. However, many of the same concepts apply, such as fields, properties, methods/functions, events, and event handlers.
In this chapter, you’ll learn how to build custom ASP.NET AJAX controls capable of generating dynamic output, handling end user interactions, and connecting to web services. Topics covered include the overall structure of client-side controls as well as how constructors, properties, functions, events, and event handlers can be defined and used. You’ll also see how client-side scripts can be encapsulated in server controls and exposed using familiar ASP.NET server control syntax. Once this is done, you can simply drop a server control on a form and set some properties to inject client script into the response stream at runtime. Let’s get started by analyzing the structure of custom client-side controls and discussing key classes provided by the ASP.NET AJAX script library.