Creating a new extender
The following steps walk you through the process of creating a new ASP.NET AJAX extender from scratch.
When you've completed this walkthrough, you'll have written a completely new extender control that you can customize and use in your own ASP.NET web pages.
Prerequisites
- If you haven't already done so, follow the steps to setup your environment
Creating the foundation
- Using Visual Studio 2005, create a new web site from the ASP.NET AJAX web site template by opening the "File" menu,
clicking "New", "Web Site...", and picking "ASP.NET AJAX Web Site" under "My Templates"
- Open the "File" menu, click "Add", and "New Project..." from the menu
- Chose C# or VB and then select the "ASP.NET AJAX Control Project" item from the
"My Templates" section.
- Name the new project DisableButton
- This will create you the default template project. Four files are automatically
created:
- DisableButtonBehavior.js - This file is where you will add all
of your client script logic.
- DisableButtonExtender.cs - This file is the server-side control
class that will manage creating your extender and allow you to set the properties
at design-time. It also defines the properties
that can be set on your extender. These properties are accessible via code and at
design time and match properties defined in the DisableButtonBehavior.js file.
- DisableButtonDesigner.cs - This class enables
design-time functionality. You should not need to modify it.
Ensuring the Client Behavior is Setup Properly
- Open DisableButtonBehavior.js by double-clicking it
- Add the test code
alert("Hello World!");
immediately below the line
this._myPropertyValue = null;
- Open Default.aspx by double-clicking it in the Solution Explorer
- Switch to design view by clicking the Design tab
- Add a TextBox (TextBox1) by dragging one over from the Toolbox
- Add a Button (Button1) by dragging one over from the Toolbox
- Build your solution by opening the "Build" menu and choosing "Build Solution" from the menu
- At the top of the Toolbox, you'll find a tab called "DisableButton Components".
Inside will be an item called "DisableButtonExtender". Drag this onto your page.
Note: If you do not find the Toolbox item in the Toobox, you can
add the item manually with the following steps:
- Switch to Source view
- Add a reference to the assembly by right clicking the website, choosing "Add Reference...", "Projects", and "DisableButton"
- Register the reference in your web page by adding the following code at the top of
the page:
<%@ Register Assembly="DisableButton"
Namespace="DisableButton" TagPrefix="cc1"%>
Note: If you are using Visual Basic, the namespace will be "DisableButton.DisableButton"
- Add the control to the page:
<cc1:DisableButtonExtender
ID="DisableButtonExtender1" runat="server"/>
- You can now switch back to Design view
- Hook up the extender by clicking on the DisableButtonExtender, going to the Properties panel, expanding the TargetControlID
property, choosing "TextBox1"
- Run the page by pressing F5 (enable debugging if prompted) and verify that the page
brings up a "Hello World!" dialog when it loads
- Close the browser window
Adding functionality
- Remove the test code
alert("Hello World!");
added to DisableButtonBehavior.js earlier
- Limit the extender to TextBoxes by changing "Control" to "TextBox" in the TargetControlType attribute in DisableButtonExtender.cs:
[TargetControlType(typeof(TextBox))]
- Create a better property name by renaming as follows (case is important):
- "MyProperty" to "TargetButtonID" in DisableButtonExtender.cs (3 locations)
- "MyProperty"/"myProperty" to "TargetButtonID" in DisableButtonBehavior.js (5 locations)
- Add a new DisabledText property by following the example of the TargetButtonID property just modified (case is important):
- Add a new public string property to DisableButtonExtender.cs:
[ExtenderControlProperty]
public string DisabledText {
get {
return GetPropertyStringValue("DisabledText");
}
set {
SetPropertyStringValue("DisabledText", value);
}
}
- Add a new member variable to the behavior in DisableButtonBehavior.js below the existing member variable for TargetButtonIDValue:
this._DisabledTextValue = null;
- Add property get/set methods to the behavior in DisableButtonBehavior.js *above* the existing get/set methods for TargetButtonID:
get_DisabledText : function() {
return this._DisabledTextValue;
},
set_DisabledText : function(value) {
this._DisabledTextValue = value;
},
- Because TargetButtonID should always refer to a control, add the attribute
[IDReferenceProperty(typeof(Button))]
to the property in DisableButtonExtender.cs so the
framework will know to automatically resolve the control ID at render time
- Add a keyup handler to the behavior by adding the following code *above* the existing property definitions in DisableButtonBehavior.js:
_onkeyup : function() {
var e = $get(this._TargetButtonIDValue);
if (e) {
var disabled = ("" == this.get_element().value);
e.disabled = disabled;
if (this._DisabledTextValue) {
if (disabled) {
this._oldValue = e.value;
e.value = this._DisabledTextValue;
} else {
if (this._oldValue) {
e.value = this._oldValue;
}
}
}
}
},
- Hook up the keyup handler by adding the following code to end of the behavior's initialize function:
$addHandler(this.get_element(), 'keyup',
Function.createDelegate(this, this._onkeyup));
this._onkeyup();
(Note: This walkthrough doesn't handle clean up in order to keep things easy understand - refer to any of the samples, and refer to the "dispose" method in the script files for examples of how to properly clean up your controls)
- Switch back to Default.aspx by double-clicking it in the Solution Explorer
- Switch to source view by clicking the Source tab
- Remove the old property setting by removing the following code from the DisableButtonExtender:
MyProperty="property"
- Rebuild the solution
- Switch to design view by clicking the Design tab
- Modify the extender's properties by clicking the TextBox, going to the Properties panel, expanding the DisableButtonExtender item, and specifying "Button1" for TargetButtonID and "Enter text" for DisabledText
- Run the page by pressing F5, enter text into the TextBox, and note the behavior of the Button control - notice that it is disabled and says "Enter text" whenever the TextBox is empty
Congratulations, you've written your first ASP.NET AJAX extender!!
Please refer to the individual sample pages (at the left) for examples of other behaviors,
and how to use them, and to the AjaxControlToolkit project for their source.