This tutorial examines the new Visual Studio 2008 Server Control and Server Control Extender. A compendium of tips, tricks and gotchas, it is a comprehensive tutorial that will provide readers with the skills necessary to start building advanced AJAX-enabled custom controls with Visual Studio.
When you open up Visual Studio 2008 to create a project, you will notice that it has two new web templates designed specifically for building AJAX controls: ASP.NET AJAX Server Control and ASP.NET AJAX Server Control Extender. You'll also find an old friend, the ASP.NET Server Control project template.
What are the differences between the Server Control, the ASP.NET AJAX Server Control and the ASP.NET AJAX Extender, and when should each be used?
At first glance, it would seem that the ASP.NET Server Control differs from the other two controls in that it doesn't support AJAX. This isn't completely true, however, and in the first part of this tutorial I will demonstrate just how far you can go in developing an AJAX-enabled control based on the Server Control alone. While the ASP.NET Server Control does not provide direct access to AJAX scripts, it can implement AJAX scripts encapsulated in other controls, such as the UpdatePanel
or the AJAX Extensions Timer
Control, to provide AJAX functionality. For control developers who are not all that keen on delving into the intracacies and pitfalls of JavaScript, the Server Control offers an excellent and clean development path.
The AJAX Server Control and the AJAX Server Control Extender differ from the regular ASP.NET Server Control by coupling themselves with javascript files, and allowing mapping between properties of a control class and properties of a javascript class. When you need functionality not provided by other AJAX Server controls, or simply want to customize your control using client-side script in order to avoid the ASP.NET control life-cycle, then this is the best option.
Finally, while the AJAX Server Control Extender is primarily used to add behavior (that is, javascript) to other controls on your ASP.NET page, the AJAX Server Control is a self-contained control in which any client-side script you write will apply, for the most part, only to the control itself, or to its children. In other words, an AJAX Extender will be aware of other controls on your page, while an AJAX Server Control will not.
Of some interest is the fact that the ASP.NET AJAX Server Control template, like the ASP.NET Server Control template, implements a ScriptControl
class that derives from System.Web.UI.WebControls.WebControl
, while the ASP.NET AJAX Server Control Extender template implements an ExtenderControl
class that derives directly from System.Web.UI.Control
. This means that using the first two kinds of templates, your control will include some built in properties like Enabled
, Height
and Width
, while this is not true of the Extender Control. For all practical purposes, however, this is not a significant difference. For a somewhat fuller treatment of the distinction between the WebControl
and Control
classes, please see Dino Esposito's article on the topic at msdn2.microsoft.com.
Read more (Code Project)