24 votes
closed
Should be able to pass additional data to the autocomplete webservice

Description

 
http://forums.asp.net/thread/1588828.aspx

Users would like to pass additional data from the form to the webservice. Currently it only uses the text in the autocomplete text box.

File Attachments


No files are attached


Closed May 10 2007 at 3:48 AM  by tedglaza

Comments

most recent at top (show oldest at top)
mwaldrop wrote May 24 2007 at 9:43 PM
I've downloaded this fix from change set 22124 and am a little confused on it's usage. I can set the contextkey to a hardcoded string and it's passed to the webmethod...all works fine. But how do I dynamically populate this. I believe someone on this thread gave an example very close to mine. I have two text boxes, one city and one state, based on what they type in for the city I would like the contextkey to be the value that is currently in the state text box. How do I do this? I've tried using javascript to dynamically populate the contextkey attribute based on what is in the state text box, but that does not seem to work, it just treats the java as a string.

Any help would be greatly appreciated.

Mike Waldrop
mwaldrop@landstar.com

wrote May 10 2007 at 3:48 AM
Resolved with changeset 22124.

glebne wrote Apr 26 2007 at 6:08 AM
The ability to pass more information seems to me almost essential to most applications of AutoComplete. It surprises me that this is listed only as "Low" impact.

I'm using "SmartAutoCompleteExtender" which can use a callback to the page to populate the list. It works nicely. See the URL below.

http://weblogs.asp.net/infinitiesloop/archive/2007/01/02/an-autocompleteextender-does-not-an-advanced-dropdownlist-make.aspx

ormico wrote Apr 15 2007 at 8:24 PM
it would be nice if you could return extra data that wasn't part of the display string. This could be placed in a seperate control like a hidden field if the item was selected.

ac931274 wrote Apr 1 2007 at 12:39 AM
How about adding a second value for each string sent back from the web service very much like the combo box with a text/value pair. The text would be what was shown and the value (let say was placed in to a hidden filed on the page on select) could then be parsed to fill other fields or just contain an id that would be passed back to the server on form submission.

aboreham wrote Mar 11 2007 at 2:25 AM
I suggest modifying the Autocomplete Extender to allow the developer to fill multiple fields in one hit.

Consider this situation:
* You have three controls: City (textbox), State (dropdown) and ZIP/postcode (textbox).
* The user starts typing the city name and the Autocomplete kicks in, showing them a list of matching cities.
* They select one of the cities from the list (which also has State and ZIP stored/displayed with it)
* Wham - the city, state AND ZIP controls get set all at once.

You could then have the option of adding an Autocomplete extender to the ZIP textbox which would do the same: show a list of cities that match the ZIP the user is entering.

wmcclure wrote Mar 8 2007 at 10:22 PM
Also, if you are running this inside of an update panel you need to run the alterTickHandler function every time the update panel is updated... I do this by tapping into the End Request event:

Sys.WebForms.PageRequestManager.getInstance().add_endRequest(EndRequest);

EndRequest is my handler where I call this code (actually I pass javascript back from the server in headers and execute it)... yeah its not elegant but it works... if anyone knows of a better way let me know.

wmcclure wrote Mar 8 2007 at 10:05 PM
I concur... what is the point of this control if page state cannot be combined? Very few uses of a drop down like this that I can think of without combining form data or at least customizing the data to the service.

I poked around the AutoCompleteBehavior javascript and made this modification that I place at the end of my page to work around this for now until the control is modified to support extra data parameters. All the code does is attempt to find the autocomplete extender object (in this case autoComplete1) and modify it's timer which is what fetches new values from the server. First I remove the tick handler that exists already and then I add a new one for my custom handler. My custom handler is just a modification of the one that comes with AutoCompleteBehavior.js. I replace all instances of this with _this as the scope of this no longer applies in the context I am using. Finally, I just modify the call to the webservice as you can see here I have added another parameter "addText" and set its value to "asdf". I tested this in IE and FF and it works great. Make sure to modify your web service appropriately if you want to use this. It's not very elegant but it works for now and the few 100 extra bytes isn't that big of a deal for now.

window.setTimeout("alterTickHandler('autoComplete1');",250);
var retryCount = 10;
function alterTickHandler(name){
//Retry this 10x in the event of slow rendering/loading
if($find(name) == null & retryCount-- > 1){
window.setTimeout("alterTickHandler('" + name + "');",250);
return;
}
//Remove tick handler
$find(name)._timer.remove_tick($find(name)._tickHandler);

//Add new tick handler and modify the webservice invoke to add all the parameters you want... this replaced with _this
$find(name)._timer.add_tick(
function(sender, eventArgs) {
var _this = $find(name);

if (_this._servicePath && _this._serviceMethod) {
var text = _this.get_element().value;

if (text.trim().length < _this._minimumPrefixLength) {
_this._currentPrefix = null;
_this._update('', null, /* cacheResults */ false);
return;
}

if (_this._currentPrefix !== text) {
_this._currentPrefix = text;
if (_this._cache && _this._cache[text]) {
_this._update(text, _this._cache[text], /* cacheResults */ false);
return;
}

Sys.Net.WebServiceProxy.invoke(_this.get_servicePath(), _this.get_serviceMethod(), false,
{ prefixText : _this._currentPrefix, addText: 'asdf', count: _this._completionSetCount },
Function.createDelegate(_this, _this._onMethodComplete),
Function.createDelegate(_this, _this._onMethodFailed),
text);
}
}
}
);
}

born2code wrote Feb 24 2007 at 9:21 PM
ditto that. I would like to limit the autocomplete suggestions based on data from other fields on the form. This would make it super useful.

Updating...
© 2006-2009 Microsoft | About CodePlex | Privacy Statement | Terms of Use | Code of Conduct | Advertise With Us | Version 2009.10.27.15987