How to get an Autocomplete script control using a script control (ASP.Net)
Problem How to capture and assign events in an Ajax Toolkit autocomplete control using a script control in a script file?
Explanation
Basically I created a script control to combine the textbox and autocomplete control so that I can have a working generic autocomplete control. The next step was to add things like the processing image while it searches for its elements. It seemed simple enough.
protected override IEnumerable<ScriptDescriptor> GetScriptDescriptors()
{
ScriptControlDescriptor desc = new ScriptControlDescriptor
("NDI.WebControls.Client.GenericAutoComplete", this.ClientID);
desc.AddProperty("autoCompleteID", autoComplete.ClientID);
return new ScriptDescriptor[] { desc };
}
And then in javascript normal:
initialize: function()
{
this._autoComplete = $get(this._autoCompleteID);
//this._autoCompleteID does have a value
this._autoCompleteClientPopulating =
Function.createDelegate(this, this.handleAutoCompleteClientPopulating);
$addHandler(this._autoComplete, "clientPopulating",
this._autoCompleteClientPopulating);
NDI.WebControls.Client.GenericAutoComplete.callBaseMethod(this, 'initialize');
},
This should work now, BUT it doesn't. What for? Because, apparently, the page will not have an autocomplete control like a normal control. So when it hits the $ get part, it is null, despite the ID property having the text property. (IE control does not exist)
Is it possible to do this or should I use the OnXyz property server to assign the method? How in:
autocomplete.OnClientPoplating = someScript;
source to share
ANSWER
Boya found it. It turns out autocomplete has a built-in way to access events in javascript:
On the server side, you have to set the BehaviorID:
autoComplete.BehaviorID = "autoCompleteBehavior";
And then set it in the GetScriptDescriptors method:
protected override IEnumerable<ScriptDescriptor> GetScriptDescriptors()
{
ScriptControlDescriptor desc = new
ScriptControlDescriptor("NDI.WebControls.Client.GenericAutoComplete", ClientID);
desc.AddProperty("autoCompleteID", autoComplete.BehaviorID);
return new ScriptDescriptor[] { desc };
}
Of course, you need to add a script property to capture that value, and once you've done that, you must use Find to get it. Then you need to create an event handler:
this._autoComplete = $find(this._autoCompleteID);
this._onAutoCompletePopulating =
Function.createDelegate(this, this.handleOnAutoCompletePopulating);
Finally, use the built-in event setter in your autocomplete control (Behavior object):
this._autoComplete.add_populating(this._onAutoCompletePopulating);
And the arrow, it's installed.
source to share