views:

1067

answers:

1
    [System.Web.Services.WebMethod]
    [System.Web.Script.Services.ScriptMethod]
    public string[] GetRowsByFilter(string prefixText, int count)
    //public static List<string> GetRowsByFilter(string prefixText)
    {
        DataTable table = ds.Tables[0];
        string filter = "stShortName LIKE '" + prefixText.Replace("'", "''") + "%'";
        DataRow[] foundRows;
        List<string> items = new List<string>(count);

        foundRows = table.Select(filter);
        if (foundRows.Length > 0)
        {
             for (int i = 0; i < foundRows.Length; i++)
            {
                items.Add((string)foundRows[i]["stShortName"]);
            }
            return items.ToArray();
        }
        else
        {
            items.Add("No '" + prefixText + "' items found");
            return items.ToArray();
        }
    }

and

<ajaxToolkit:AutoCompleteExtender 
 id="AutoCompleteExtenderTxtSite" 
 BehaviorID="AutoCompleteEx"
 Runat="server" 
 Targetcontrolid="txtSiteASP"
 ServiceMethod="GetRowsByFilter"
 MinimumPrefixLength="1" 
 CompletionInterval="1000"
 EnableCaching="false"
 CompletionSetCount="10" 
 CompletionListCssClass="autocomplete_completionListElement" 
 CompletionListItemCssClass="autocomplete_listItem" 
 CompletionListHighlightedItemCssClass="autocomplete_highlightedListItem"
 DelimiterCharacters=";, :"
 ShowOnlyCurrentWordInCompletionListItem="true" 
  >
 <Animations>
  <OnShow>
   <Sequence>
    <OpacityAction Opacity="0" />
    <HideAction Visible="true" />
    <ScriptAction Script="
     // Cache the size and setup the initial size
     var behavior = $find('AutoCompleteEx');
     if (!behavior._height) {
      var target = behavior.get_completionList();
      behavior._height = target.offsetHeight - 2;
      target.style.height = '0px';
     }" />
    <Parallel Duration=".4">
     <FadeIn />
     <Length PropertyKey="height" StartValue="0" EndValueScript="$find('AutoCompleteEx')._height" />
    </Parallel>
   </Sequence>
  </OnShow>
  <OnHide>
   <Parallel Duration=".4">
    <FadeOut />
    <Length PropertyKey="height" StartValueScript="$find('AutoCompleteEx')._height" EndValue="0" />
   </Parallel>
  </OnHide>
 </Animations>
</ajaxToolkit:AutoCompleteExtender>

Most of this is directly out of the Toolkit sample website. I have also done it using the web service exactly as from sample except with filling the array from the database. Both fill the array flawlessly, and both work sometimes. Lack of performance wise, they appear to be identical.

I use a couple of the calendar controls on another page and they work flawlessly, but have wasted way too much time trying to make this work consistently.

+1  A: 

I've found that if the server side code takes a long time to execute, the client-side AJAX request seems to time out.

I have not attempted to prove this because the effort seemed more than worthwhile, when I could just tune the server side code to improve performance.

Check if your DataSet is cached and if not, how long does it take to retrieve it? Check database indexes. Sql LIKE queries are better optimised in SQL server 2005.

In case it is not a time-out and actually a server-side error, either test the webservice locally by accessing it via a browser and typing queries that you would use in the AutoComplete textbox. Alternatively, check the server eventlog just after you get no results and look for ASP.NET warning events.

Neil Fenwick