ASP.NET Extenders

Injecting script code into existing ASP.NET controls


June 09, 2008
URL:http://www.drdobbs.com/windows/aspnet-extenders/208402764


ASP.NET has a stock of built-in controls that covers all major needs of a Web developer. I'm far from saying that you don't need to look elsewhere; let alone am I saying that third-party vendors have had their day. However, the basic programming needs of every developer can be satisfied by stock ASP.NET controls. Then, if you can't still find the control you are looking for, then you typically write one yourself or buy a new specialized control that extends the original control and adds the desired behavior. Object orientation encourages this approach.

So if you need a numeric textbox, you write it one that emits some markup and script. And if you one day feel the need of an auto-completion textbox, you write yet another control. But what if, at some point, you need a control with both auto-completion and numeric input? Should you write a third class that incorporates both "behaviors"?

ASP.NET extenders just serve the purpose of defining a new breed of controls that do not provide any markup, but just inject some script code within existing ASP.NET controls.

ASP.NET extenders are server controls that implement a cross-cutting behavior that can be applied to one or more control types to extend their base capabilities. Extenders decouple controls from behaviors and make possible to extend existing controls with new behaviors.

A good deal of extenders is implemented in the AJAX Control Toolkit -- a shared-source library of Web controls specifically designed for ASP.NET. It is not included in the ASP.NET 3.5 platform and should be downloaded separately.

As an example, consider a really useful extender -- the Calendar extender. In spite of the name, the extender offers date-picking capabilities and can be used to extend a text box control.

By using the calendar extender, you make it virtually impossible for users to type anything other than a date in the target text box. ASP.NET comes with a server Calendar control, but no date picker facilities. Compared to the Calendar control, a calendar extender is really different, as it builds its user interface entirely on the client, works entirely on the client, and generates no postbacks at all as the user navigates to find month and day.

<asp:TextBox runat="server" ID="TextBox1" />
<act:CalendarExtender ID="CalendarExtender1" runat="server"
     TargetControlID=" TextBox1" Format="dd/MM/yyyy" />

The preceding code snippet is sufficient to display a popup calendar as the associated text box receives the focus. As an alternative, you can display the popup when the user clicks a page button. The ID of the button is set through the PopupButtonID property. The Format property indicates the format of the date as it will be written to the text box when the user dismisses the calendar popup.

What if a given browser doesn't support JavaScript? In this case, the extender control is blissfully ignored and the old textbox is displayed.

In the AJAX Control Toolkit, many extenders have dependencies on other extenders which would make it difficult to extract just one extender to incorporate it into a custom library. Likewise, an extender can be used also on non-ASP.NET AJAX platforms as long as you can extract out of the toolkit just the pieces that make it work.

Terms of Service | Privacy Statement | Copyright © 2024 UBM Tech, All rights reserved.