locked
AutoCompleteExtender Works great the first time, then it doesnt. RRS feed

  • Question

  • User1324895001 posted

    So I implemented the AutoCompleteExtender as I have done on a few other forms within our site.. but this recent one doesn't seem to work the same as the others.. which is weird seeing that I merely copied and pasted into the new form and changed the control names to fit the page.

     

    What I have is a auto complete tied to a textbox that is used for "new" string value inserts. I set it up so that if you enter a string/value that already exists, it will show it obviously in the suggested popup that opens after 3 characters. So that you are not allowed to duplicate a value.

     

    When I go to the page for the first time, I can enter 3 letters and it returns the list of existing values, IF you chose one that exists or enter one that does, when you go to submit it validates again(validation controls) it tells you that it already exists and disables the submission, so that no duplicates are submitted. This works great, NOW, if you go back to the page for a 2nd time, enter the same 3 letters, it will return the appropriate list, but if you pay attention you will notice that the list that was originally returned has been returned again, so now you have 2 sets of values returned, so in other words if your suggestions returned are say :

    ABB
    RABBIT
    CRABBY

    Then you return to the page and enter the same 3 letters, now the suggestions displayed is like so:

    ABB
    RABBIT
    CRABBY
    ABB
    RABBIT
    CRABBY

     

    And if you do the search again and again, the suggestions list continues to grow in length, all with the same results, just seems to append the previous with the new.

    No matter how many times I visit the page and search for the same thing, it should always just return the 3 suggestions(in this example) not a growing appended list of the same values.

    How can this be resolved? I ask, because like I said, I am using the exact same code and logic(just different control names) and the other pages work no matter how many times you visit the page and search for the same thing.

     

     

    Monday, June 15, 2015 5:04 PM

Answers

  • User177399542 posted

    Hi cubangt

    You need to create instance of your List<String> object inside your method OR simply empty your List<String> before databinding:

    Method1:

            public static string[] GetCompletionList(string prefixText, int count, string contextKey)
            {
                public static List<string> vNames = new List<string>();
                string pfx = prefixText.ToUpper();
                return (from m in (vNames as IEnumerable<string>) where m.Contains(pfx) select m).Take(count).ToArray();
            }

    Method2:

            public static string[] GetCompletionList(string prefixText, int count, string contextKey)
            {
                vNames.Clear();
                string pfx = prefixText.ToUpper();
                return (from m in (vNames as IEnumerable<string>) where m.Contains(pfx) select m).Take(count).ToArray();
            }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, June 17, 2015 1:20 AM

All replies

  • User177399542 posted

    Hi cubangt

    Kindly post  method which you have used to check values for Ajax control toolkit autocomplete extender.

    Tuesday, June 16, 2015 3:42 AM
  • User61956409 posted

    Hi cubangt,

    Thanks for your post.

    You could set breakpoint in your ServiceMethod and debug the code to check the returned list.

    Best Regards,

    Fei Han

    Tuesday, June 16, 2015 4:56 AM
  • User1324895001 posted

    So after posting and doing some more testing, still no closer to why its doubling and tripling the data in the auto suggest list.

    I figured it was due to the method that I use to populate the list being in the pageload, but even if I wrap it in

    if (!Page.IsPostBack)
    {

    }

    Here is what I have and where its setup in the code:

            The list
            public static List<string> vNames = new List<string>();
    
            The place where it all happens ;)
    
            protected void Page_Load(object sender, EventArgs e)
            {
    
                if (!Page.IsPostBack)
                {
                    gvListBind();
                }
    	}
    
            The method to populate the list
              
            private void gvListBind()
            {
                DataTable dtList = dac.getNameList();
    
                if ((dtList.Rows.Count > 0) && (dtList != null))
                {
                    gvList.DataSource = dtList;
                    gvList.DataBind();
    
                    foreach (DataRow row in dtList.Rows)
                    {
                        string part = row["Name"].ToString().ToUpper();
                        vNames.Add(part);
                    }
                }
            }
    
            The service call
    
            public static string[] GetCompletionList(string prefixText, int count, string contextKey)
            {
                string pfx = prefixText.ToUpper();
                return (from m in (vNames as IEnumerable<string>) where m.Contains(pfx) select m).Take(count).ToArray();
            }

    So even if I remove the !Page.IsPostBack it still does the same thing.. there has to be something else that im missing or overlooking..

    When I run the page the first time, vNames has the correct number of records (1100), I can then try again, and it becomes 2200 and so on..

    Tuesday, June 16, 2015 10:49 AM
  • User177399542 posted

    Hi cubangt

    You need to create instance of your List<String> object inside your method OR simply empty your List<String> before databinding:

    Method1:

            public static string[] GetCompletionList(string prefixText, int count, string contextKey)
            {
                public static List<string> vNames = new List<string>();
                string pfx = prefixText.ToUpper();
                return (from m in (vNames as IEnumerable<string>) where m.Contains(pfx) select m).Take(count).ToArray();
            }

    Method2:

            public static string[] GetCompletionList(string prefixText, int count, string contextKey)
            {
                vNames.Clear();
                string pfx = prefixText.ToUpper();
                return (from m in (vNames as IEnumerable<string>) where m.Contains(pfx) select m).Take(count).ToArray();
            }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, June 17, 2015 1:20 AM
  • User1324895001 posted

    Well I cant really do that from what I can tell, since the list is being populated on page load, before the service is called, if I clear it, then it will be empty.

    Wednesday, June 17, 2015 4:01 PM
  • User1324895001 posted

    I was able to just do the clear in the binding method before it gets populated and seems to be working..

     

    thank you

    Wednesday, June 17, 2015 4:03 PM