如何使用自动完成功能创建Searchtextbox? [英] How Can I Create A Searchtextbox With Autocomplete?

查看:104
本文介绍了如何使用自动完成功能创建Searchtextbox?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在那里,



我正在尝试在文本框中创建一个AutoCompleteExtender但是当我在其上写东西时没有任何反应。



我的asp.net代码就是这样:


I there,

I'm trying to create a AutoCompleteExtender in a textbox but nothing happens when i write something on it.

My asp.net code its this:

<<pre lang="xml">asp:UpdatePanel ID="UpdatePanel1" runat="server">
            <ContentTemplate>
                <asp:TextBox ID="TextBox1" runat="server" Height="21px" Width="477px" AutoPostBack="true"></asp:TextBox>
                <asp:AutoCompleteExtender ID="TextBox1_AutoCompleteExtender" runat="server" ServicePath="~/AutoComplete.asmx"

                    ServiceMethod="AutoCompleteSearch" TargetControlID="TextBox1" MinimumPrefixLength="1" CompletionSetCount="10" >
                </asp:AutoCompleteExtender>
            </ContentTemplate>
        </asp:UpdatePanel></pre>





和我的网络服务是一个:





and my web service its this one:

[WebMethod]
  public string[] AutoCompleteSearch(string prefixText, int count)
  {

      DataSet ds = new DataSet();
      SqlDataAdapter sda = new SqlDataAdapter();
      sda.SelectCommand = new SqlCommand();
      sda.SelectCommand.Connection = new SqlConnection("PHCConnectionString");
      if (sda.SelectCommand.Connection.State == ConnectionState.Closed)
          sda.SelectCommand.Connection.Open();
      sda.SelectCommand.CommandType = CommandType.Text;
      sda.SelectCommand.CommandText = "Select (nome,' ',bino) as u_nome From cve ";
      sda.Fill(ds, "data");
      sda.SelectCommand.Connection.Close();

      string[] main = new string[0];
      for (int i = 0; i < ds.Tables[0].Rows.Count - 1;i++ )
      {
          if (ds.Tables[0].ToString().ToLower().StartsWith(prefixText.ToLower()))
          {
              Array.Resize(ref main, main.Length + 1);
              main[main.Length - 1] = ds.Tables[0].Rows[i].ItemArray[0].ToString();
              if (main.Length == 20)
                  break;
          }
      }
      Array.Sort(main);
      return main;
  }





有人能帮助我理解我做错了吗?



can someone help me understand what i did wrong??

推荐答案

选择(nome,'',bino)为u_nome来自cve
"Select (nome,' ',bino) as u_nome From cve "



SQL查询没有'看起来正确 - 如果你正在使用MS SQL,你不能只是在括号中包含多个值并期望SQL连接它们。


That SQL query doesn't look right - if you're using MS SQL, you can't just wrap multiple values in brackets and expect SQL to concatenate them.

if(ds.Tables) [0] .ToString()。ToLower()。StartsWith(prefixText.ToLower()))
if (ds.Tables[0].ToString().ToLower().StartsWith(prefixText.ToLower()))



如果第一个的名称,你也只返回任何数据表以前缀开头 - 您应该比较该值。



而不是将表中的每条记录读入内存然后使用C#过滤并对它们进行排序,你应该使用SQL:


You're also only returning any data if the name of the first table starts with the prefix - you should be comparing the value instead.

Rather than reading every record from the table into memory and then using C# to filter and sort them, you should use SQL:

[WebMethod]
public string[] AutoCompleteSearch(string prefixText, int count)
{
    using (SqlConnection connection = new SqlConnection("YOUR CONNECTION STRING HERE"))
    using (SqlCommand command = new SqlCommand("SELECT TOP (@Count) nome + ' ' + bino As u_nome From cve Where nome + ' ' + bino Like @Prefix Order By nome, bino", connection))
    {
        command.Parameters.AddWithValue("@Prefix", prefixText + "%");
        command.Parameters.AddWithValue("@Count", count);
        
        connection.Open();
        using (SqlDataReader reader = command.ExecuteReader(CommandBehavior.CloseConnection | CommandBehavior.SingleResult))
        {
            List<string> result = new List<string>(count);
            while (reader.Read())
            {
                result.Add(reader.GetString(0));
            }
            
            return result.ToArray();
        }
    }
}


这篇关于如何使用自动完成功能创建Searchtextbox?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆