1

I have a textbox with AutoCompleteExtender, instead of using a webservice I just have a method in code behind that pulls a list of string names from an xml. Now every time you start in the text box , the AutoCompleteExtender shows up with all 500 names in it. There is no order either ( ex. if I type "Riha" to start typing "Rihana" you'd think only strings that start with "Riha" would show up, but all 500 show up, not even in any order. I tried setting CompletionSetCount="5" , but no luck. Is there an easy fix to this?

This Part of the Code I believe is functioning properly...

[System.Web.Script.Services.ScriptMethod()]
[System.Web.Services.WebMethod]
public static List<string> GetNames(string prefixText, int count)
{
    XmlDocument xmlArtist = new XmlDocument();        
    xmlArtist.Load(string.Format(" http://ws.audioscrobbler.com/2.0/?method=chart.gettopartists&api_key={0}&limit=500",  key));
    List<string> topartists = new List<string>();
             foreach (XmlNode node in xmlArtist.SelectNodes("lfm/artists/artist"))
             {                     
                 topartists.Add(node.SelectSingleNode("name").InnerText.ToString());
             }
             return topartists;            
}

Here is .aspx code

    <asp:TextBox ID="txtEnterBand" runat="server" CssClass="txtbox" Width="400px"  >     </asp:TextBox>
<asp:AutoCompleteExtender ID="txtEnterBand_AutoCompleteExtender" runat="server" TargetControlID="txtEnterBand" ServiceMethod="GetNames" UseContextKey="true" ServicePath="" MinimumPrefixLength="1" CompletionSetCount="5"></asp:AutoCompleteExtender>
Scott Selby
  • 9,420
  • 12
  • 57
  • 96
  • Why not limit what gets returned in your code behind? instead of foreach, do a for (i = 0; i<5; i++) ? or some such? – Prescott May 05 '12 at 02:23
  • 1
    Btw, CompletionSetCount is passed to your web service, it doesn't limit anything on it's own – Prescott May 05 '12 at 02:26
  • I thought that the AutoCompleteExtender automatically compared whats in the text box to the list , I thought there was just a property that I was missing. Do I have to pass whats in the textbox and compare it to all 500 names manually each time? – Scott Selby May 05 '12 at 02:26
  • It makes calls continuous calls to your webservice with more and more values, something like prefix = "mysea", then prefix = "mysear", prefix = "mysearc" etc etc. So you should be comparing and limiting in your method – Prescott May 05 '12 at 02:29
  • Ok, that's easy I guess, I just thought it compared on it's own - Also 1 more thing , how can I store the List topartists in memory somewhere so it doesn't have to request it every time. it won't let me access Session State from that GetNames method. Any suggestions on how to store that List temporarly? – Scott Selby May 05 '12 at 02:36
  • I think you can access an application variable from your static methods – Prescott May 05 '12 at 02:44
  • I cannot access variables from static methods – Scott Selby May 05 '12 at 04:02

1 Answers1

2
GetNames(string prefixText, int count, string contextKey)
{
   return topartists.Take(count).ToList();
}

In Source page:

<asp:AutoCompleteExtender ID="AutoCompleteExtender1" runat="server" 
UseContextKey="True"  TargetControlID="txtAuto" ServiceMethod="SearchCustomers" 
MinimumPrefixLength="2" CompletionInterval="100" EnableCaching="false" 
CompletionSetCount="10" FirstRowSelected="false" 
OnClientItemSelected="ClientItemSelected">
</asp:AutoCompleteExtender>

Add this propertyUseContextKey="True" in the <asp:AutoCompleteExtender> tag.

SchmitzIT
  • 9,227
  • 9
  • 65
  • 92