Using the code
ASP.NET AJAX extender controls add features (usually AJAX or JavaScript support) to existing controls already declared on a page.
With VS 2005 you had to manually wire-up control extenders yourself (either via source-view or via the property grid).
This is not necessary with the newly released VS 2008 and can be done easily using the “Add Extender” feature.
The following example demonstrates the addition of AJAX support to a TextBox in VS 2008.
Step1:
Create a simple ASP.Net website using the VS 2008 templates by accepting defaults at a suitable location.
Step2:
Add a new project using the File->Add->New Project->VisualC# -> ASP.Net AJAX Server Control Extender as shown :
Introduction
Step3:
Modify ExtenderControl1.cs as following:
Add the following attributes to the ExtenderControl1 class:
[
TargetControlType(typeof(TextBox))
]
[
DefaultProperty("Contacts"),
ParseChildren(true, "Contacts"),
ToolboxData(
"<{0}:QuickContacts runat=\"server\"> {0}:QuickContacts>")
]
Step4:
Add a property as follows to the ExtenderControl1 class:
private ArrayList contactsList;
[
Category("Behavior"),
Description("The contacts collection"),
DesignerSerializationVisibility(
DesignerSerializationVisibility.Content),
Editor(typeof(ContactCollectionEditor), typeof(UITypeEditor)),
PersistenceMode(PersistenceMode.InnerDefaultProperty)
]
public ArrayList Contacts
{
get
{
if (contactsList == null)
{
contactsList = new ArrayList();
}
return contactsList;
}
}
Step5:
Modify the GetScriptDescriptors method as follows:
protected override IEnumerable<ScriptDescriptor>
GetScriptDescriptors(System.Web.UI.Control targetControl)
{
ScriptBehaviorDescriptor descriptor = new ScriptBehaviorDescriptor(
"AjaxTextWithTableExtender.ClientBehavior1",
targetControl.ClientID);
descriptor.AddProperty("contactsList", this.Contacts);
return new ScriptBehaviorDescriptor[] { descriptor };
}
Step6:
Modify the ClientBehavior1.initializeBase function by adding the following lines:
this.contactsList=null;
this.strtext=null;
this.flag=true;
Add the following lines to ClientBehavior1.prototype,
initialize: function
Note: Be careful with the commas
$addHandler(this.get_element(), 'keyup',
Function.createDelegate(this, this._onkeyup));
this._onkeyup();
and also add the following:
get_contactsList : function() {
return this.contactsList;
},
set_contactsList : function(value) {
this.contactsList=value;
this.raisePropertyChanged('contactsList');
},
_onkeyup : function() {
this.strtext=document.getElementById("TextBox1").value;
var links = new Array ("link1", "link2", "link3");
var links_url = new Array ("link1.htm", "link2.htm",
"link3.htm");
var loc=String(this.location);
loc=loc.split("/");
loc=loc[loc.length-1].split(".");
loc=loc[loc.length-2];
var v=new Array(this.contactsList);
var t = document.getElementById("TextBox1");
if (t) {
if(this.flag)
{
var mybody = document.getElementsByTagName("body")[0];