AutoCompleteExtender控件的使用 (读取数据库中的内容,搜索实现自动完成功能)

 AutoCompleteExtender控件的属性:
               TargetControlID:指定将被辅助完成自动输入的控件ID,这里的控件只能是TextBox;
               ServicePath:指出提供服务的WEB服务路径,若不指出则ServiceMethod表示本页面对应的方法名;
               ServiceMethod:指出提供服务的方法名;
               MinimumPrefixLength:指出开始提供提示服务时,TextBox控件应有的最小字符数,默认为3;
               CompletionSetCount:显示的条数,默认为10;
               EnableCaching:是否在客户端缓存数据,默认为true;
               CompletionInterval:从服务器读取数据的时间间隔,默认为1000,单位:毫秒。

前台代码:<asp:ScriptManager ID="ScriptManager1" runat="server" />

         < h3 >
            在线图书查询
</ h3 >
        请输入查询的关键字:
< asp:TextBox  ID ="keyword"  runat ="server" ></ asp:TextBox >
        
< cc1:AutoCompleteExtender  ID ="AutoCompleteExtender1"  TargetControlID ="keyword"  ServicePath ="BookService.asmx"
            ServiceMethod
="GetTextString"  CompletionInterval ="100"  EnableCaching ="true"  CompletionSetCount ="12"
            MinimumPrefixLength
="1"  runat ="server" >

        </cc1:AutoCompleteExtender> 

后台代码:using System;

using  System.Collections.Generic;
using  System.Linq;
using  System.Web;
using  System.Web.Services;
using  System.Data;
using  System.Data.SqlClient;
using  System.Configuration;

///   <summary>
/// BookService 的摘要说明
///   </summary>
[WebService(Namespace  =   " http://tempuri.org/ " )]
[WebServiceBinding(ConformsTo 
=  WsiProfiles.BasicProfile1_1)]
// 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消对下行的注释。 
[System.Web.Script.Services.ScriptService]
public   class  BookService : System.Web.Services.WebService
{

    
public  BookService()
    {

        
// 如果使用设计的组件,请取消注释以下行 
        
// InitializeComponent(); 
    }

    [WebMethod]
    
public   string [] GetTextString( string  prefixText,  int  count)
    {
      
   // prefixText表示用户输入的前缀,count表示返回的个数 string。 prefixText, int count 两个参数必须原封不动照写,包括大小写也是一样 返回参数只能是字符串数组
        
//  Database db = new Database();  
        //获取结果
        //System.Data.DataTable dt = db.SelectBook2(prefixText, count).Tables[0];
        //string[] result = new string[dt.Rows.Count];
        //for (int i = 0; i < dt.Rows.Count; i++)
        //{
            result.SetValue(dt.Rows[i][0].ToString(), i);
        //}
        //return result;

        //************************************************************************************* 

       
  string  connStr  =   " Server=.\\sqlexpress;Initial Catalog=bookshop;Integrated Security=True " ;
        SqlConnection conn 
=   new  SqlConnection(connStr);
        conn.Open();
        SqlCommand cmd 
=   new  SqlCommand( " select top  " + count + "  [Name] from Book where [Name] like @name " , conn);
        
// cmd.Parameters.Add("@count", SqlDbType.NVarChar).Value = count;
        cmd.Parameters.Add( " @name " , SqlDbType.NVarChar).Value  =   " % "   +  prefixText  +   " % " ;
        SqlDataAdapter da 
=   new  SqlDataAdapter(cmd);
        DataSet ds 
=   new  DataSet();
        da.Fill(ds);
        DataTable dt 
=  ds.Tables[ 0 ];
        List
< string >  result  =   new  List < string > (dt.Rows.Count);
        
for  ( int  i  =   0 ; i  <  dt.Rows.Count; i ++ )
        {
            result.Add(dt.Rows[i][
0 ].ToString());
        }
        conn.Close();
        
return  result.ToArray();

    }

} 

你可能感兴趣的:(autocomplete)