Search code examples
c#asp.netasp.net-ajaxautocompleteextender

How to select multiple items using Autocomplete extender in c#?


In my project I am using autocomplete extender.using this I am selecting only one item.But I want to select multiple items.what I should I do.please help me. below is my code:

aspx page:

<asp:TextBox ID="TextBox1" runat="server"  CssClass="autosuggest  ui-timepicker-input" Width="300px"></asp:TextBox>

        <ajaxToolkit:AutoCompleteExtender ID="TextBox1_AutoCompleteExtender" runat="server" DelimiterCharacters=""
            Enabled="True" ServiceMethod="GetListofCountries" MinimumPrefixLength="2" EnableCaching="true" CompletionSetCount="10" CompletionInterval="10"  FirstRowSelected="false"
            TargetControlID="TextBox1">
        </ajaxToolkit:AutoCompleteExtender>

aspx.cs page:

[System.Web.Script.Services.ScriptMethod()]
        [System.Web.Services.WebMethod]
        public static List<string> GetListofCountries(string prefixText, int count)
        {
            //using (SqlConnection sqlconn = new SqlConnection(ConfigurationManager.ConnectionStrings["con2"].ConnectionString))
            //{
            //    sqlconn.Open();
            //    SqlCommand cmd = new SqlCommand("select UserEmail from [User]  where Useremail like '%" + prefixText + "%'", sqlconn);
            //    cmd.Parameters.AddWithValue("@prefixText", prefixText);
            //    SqlDataAdapter da = new SqlDataAdapter(cmd);
            //    DataTable dt = new DataTable();
            //    da.Fill(dt);
            //    List<string> Emailid = new List<string>();
            //    for (int i = 0; i < dt.Rows.Count; i++)
            //    {
            //        Emailid.Add(dt.Rows[i]["UserEmail"].ToString());
            //    }
            //    return Emailid;
            //}





            List<string> customers = new List<string>();
            using (SqlConnection conn = new SqlConnection())
            {
                List<string> terms = prefixText.Split(',').ToList();
                terms = terms.Select(s => s.Trim()).ToList();

                //Extract the term to be searched from the list
                string searchTerm = terms.LastOrDefault().ToString().Trim();

                //Return if Search Term is empty
                if (string.IsNullOrEmpty(searchTerm))
                {
                  //  return 
                }

                //Populate the terms that need to be filtered out
                List<string> excludeTerms = new List<string>();
                if (terms.Count > 1)
                {
                    terms.RemoveAt(terms.Count - 1);
                    excludeTerms = terms;
                }

                conn.ConnectionString = ConfigurationManager
                        .ConnectionStrings["con2"].ConnectionString;
                using (SqlCommand cmd = new SqlCommand())
                {
                    string query = "select UserEmail from [User]  where Useremail like '%" + prefixText + "%'";

                    //Filter out the existing searched items
                    if (excludeTerms.Count > 0)
                    {
                        query += string.Format(" and UserEmail not in ({0})", string.Join(",", excludeTerms.Select(s => "'" + s + "'").ToArray()));
                    }
                    cmd.CommandText = query;
                    cmd.Parameters.AddWithValue("@prefixText", prefixText);
                    cmd.Connection = conn;
                    conn.Open();
                    using (SqlDataReader sdr = cmd.ExecuteReader())
                    {
                        while (sdr.Read())
                        {
                            //customers.Add(string.Format("{0}-{1}", sdr["Userid"], sdr["CustomerId"]));
                            customers.Add(string.Format("{0}", sdr["UserEmail"]));
                        }
                    }
                    conn.Close();
                }
                return customers;


            }

but it gives only one value.I want to select multiple items.please help me.


Solution

  • finally I got an answer for the above question.in the AutoCompleteExtender add DelimiterCharacters="," and ShowOnlyCurrentWordInCompletionListItem="true" . add this two items for the AutoCompleteExtender. It will work for me.if any one want this type of question, I hope this answer is help you,thats why I posted here.thank you.