Search code examples
c#asp.netajaxcontroltoolkit

Ajax Toolkit AutoCompleteExtender


I'm wondering if there is a way using the ajax toolkit autocompleteextender to hook into multiple textbox controls.

The reason is we have multiple textbox controls and I don't fancy using 8 autocompleteextenders to perform the same thing, as all 8 of them would need to be initialized properly and so on, one feels more maintainable.

Edit:

I constructed a method that initializes the autocompleteextender object and pass each object to that method, so the only thing I need to set seperate on each one is the ID and the TargetElementID. At least better than nothing, but I would still appreciate a better solution to this.


Solution

  • Not an answer specific to the question but I had always used the Ajax Control toolkit autocomplete extender but then I discovered the Jquery one, which in my opinion is much better, and I think you could easily do what you want, although you'd need to assign it to each control in jQuery, but you'd only need 2 functions, one to get the data and one to process the results.

    You do something similar to follows:

      $(document).ready(function () {
    
        $('#<%=txtSearchBox.ClientID%>').autocomplete('/Search.ashx');
        $('#<%=txtSearchBox.ClientID%>').result(function (event, data, formatted) {
            if (data) {
    
                // Extract the data values
                var name = data[0]; // appears in textbox
                var dataval1= data[1];
                var dataval2= data[2];
    
    
                $("#<%=hdndataval1.ClientID%>").val(dataval1);
                $("#<%=hdndataval2 .ClientID%>").val(dataval2);
            }
        });
    
    });
    

    You do have to use hidden fields to store an id, but it autocompletes nicely using a handler.

        public void ProcessRequest(HttpContext context)
         {
              string prefixText = context.Request.QueryString["q"]; 
              //do your thing here and return as a bar separated list
              StringBuilder sb = new StringBuilder();
              foreach(Results res in results )
                {
                    sb.Append(String.Format("{0}|{1}|{2}", +res.Val1, res.Val2, res.Val3));
                    sb.Append(Environment.NewLine);
                }
              context.Response.Write(sb.ToString());
         }
    

    The JQuery is here (I think)

    Must admit I used to swear by that control toolkit, but having been introduced to Jquery I have found loads of controls that I think are much better!

    Just thought I'd throw a different option in your direction!