如何在c#中使用自动完成扩展器选择多个项目?

时间:2015-09-20 13:17:09

标签: c# asp.net asp.net-ajax autocompleteextender

在我的项目中,我正在使用autocomplete extender。使用此我只选择一个项目。但我想选择多个项目。我该怎么做。请帮助我。下面是我的代码:

aspx页面:

<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页面:

[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;


            }

但它只提供一个值。我想选择多个项目。请帮助我。

1 个答案:

答案 0 :(得分:1)

最后我得到了上述问题的答案。在AutoCompleteExtender中添加DelimiterCharacters="," and ShowOnlyCurrentWordInCompletionListItem="true"。为AutoCompleteExtender添加这两项。它对我有用。如果有人想要这类问题,我希望这个答案对你有帮助,这就是为什么我发布在这里。谢谢你。