Ajax:AutoCompleteExtender自动输入完成(转载)

AutoCompleteExtender控件可以帮你自动填写TextBox控件(在数据库中查找).   
属性:   
TarGetControlID:指定要让
"自动输入完成"扩展器要扩展的TextBox控件ID.   
ServicePath:Web服务的位置路径.   
ServiceMehod:要调用的Web服务的方法名.方法签名如下:   
  
[System.Web.Services.WebMethod]   
[System.Web.Script.Service.ScriptMethod]   
public string[] GetCompetionList(string prefixText,int count){......}   
  
  
前台代码:   
  
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>   
  
<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="cc1" %>   
  
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">   
<html xmlns="http://www.w3.org/1999/xhtml">   
<head runat="server">   
    
<title>Untitled Page</title>   
</head>   
<body>   
    
<form id="form1" runat="server">   
        
<asp:ScriptManager ID="ScriptManager1" runat="server" />   
        
<div>   
            
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>   
            
<cc1:AutoCompleteExtender ID="AutoCompleteExtender1" runat="server" TargetControlID="TextBox1"  
             MinimumPrefixLength
="1" ServiceMethod="GetProductName" ServicePath="WebService.asmx">   
            
</cc1:AutoCompleteExtender>   
        
</div>   
    
</form>   
</body>   
</html>   
  
WebService代码:   
  
using System;   
using System.Web;   
using System.Collections;   
using System.Web.Services;   
using System.Web.Services.Protocols;   
using System.Data;   
using System.Data.SqlClient;   
using System.Web.Script.Services;//关键程序集引用   
using System.Collections.Generic;//关键程序集引用   
  
/// <summary>   
/// WebService 的摘要说明   
/// </summary>   
[WebService(Namespace = "http://tempuri.org/")]   
[WebServiceBinding(ConformsTo 
= WsiProfiles.BasicProfile1_1)]   
[ScriptService()]
//一定要添加   
public class WebService : System.Web.Services.WebService {   
  
    
public WebService () {   
  
        
//如果使用设计的组件,请取消注释以下行    
        
//InitializeComponent();    
    }   
  
    [WebMethod]   
    [ScriptMethod]   
    
public string[] GetProductName(string prefixText, int count)   
    {   
        List
<string> suggestions=new List<string>();//声明一泛型集合   
        SqlConnection con = new SqlConnection("server=.;database=NorthWind;uid=sa;pwd=;");   
        con.Open();   
        SqlCommand com 
= new SqlCommand("select distinct productname from Products where productname like @prefixname order by productname", con);   
        com.Parameters.Add(
"@prefixname",SqlDbType.NVarChar).Value=prefixText + "%";   
        SqlDataReader sdr 
= com.ExecuteReader();   
        
while (sdr.Read())   
        {   
            suggestions.Add(sdr.GetString(
0));   
        }   
         sdr.close();   
        con.close();   
        
return suggestions.ToArray();   
    }   
}  

 这个控件还是比较有用的。我们使用搜索引擎时,输入几个字符时,可以出来相关的文字提示,应该是Ajax的这种技术的应用吧。可以帮助用户快速挑选出针对TextBox自己要输入的内容,减少输入工作。

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

凡是为AutoCompleteExtender控件提供服务的方法都必需完全满足以下三个条件:
   A.方法的返回类型必需为:string [];
   B.方法的传入参数类型必需为:string  ,   int;
   C.两个传入参数名必需为:prefixText  ,  count。

本文转载自:http://daweijsp.javaeye.com/blog/398446http://jialeifei.blog.163.com/blog/static/22190126200911810431891/

原文地址:https://www.cnblogs.com/purplefox2008/p/1623275.html