Dr. Dobb's is part of the Informa Tech Division of Informa PLC

This site is operated by a business or businesses owned by Informa PLC and all copyright resides with them. Informa PLC's registered office is 5 Howick Place, London SW1P 1WG. Registered in England and Wales. Number 8860726.

Channels ▼


ASP.NET Extenders

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.

Related Reading

More Insights

Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.