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

本文关键字:程序 扩展 选择 项目 | 更新日期: 2023-09-27 18:13:41

在我的项目中,我使用自动完成扩展程序。使用这个,我只选择一个项目。但是我想选择多个项目。我该怎么做。请帮帮我。下面是我的代码:

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;

            }

,但它只给出一个值。我想选择多个项目。

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

我终于得到了上面问题的答案。在AutoCompleteExtender中添加DelimiterCharacters="," and ShowOnlyCurrentWordInCompletionListItem="true"。为AutoCompleteExtender添加这两项。这对我有用。如果有人想要这种类型的问题,我希望这个答案是帮助你,这就是为什么我张贴在这里。谢谢你。