ajax控件为AutoCompleteExtender源中代码设置为: <%...@ Page Language="C#" AutoEventWireup="true" CodeFile="AutoCompleteDepart.aspx.cs" Inherits="AutoCompleteDepart" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml" ><head runat="server"> <title>无标题页</title></head><body> <form id="form1" runat="server"> <asp:ScriptManager ID="ScriptManager1" runat="server"> </asp:ScriptManager> 请输入字母前缀: <div> <asp:TextBox ID="TextBox1" runat="server" Width="285px" /> <ajaxToolkit:AutoCompleteExtender ID="AutoCompleteExtender1" runat="server" Enabled="true" TargetControlID="TextBox1" ServicePath="AutoCompleteDepartSevice.asmx" ServiceMethod="GetCompleteDepart" CompletionSetCount="10" MinimumPrefixLength="2"> </ajaxToolkit:AutoCompleteExtender> </div> </form></body></html> 在数据库中读取数据可以双击ajax控件在代码中写入以下方法,最好是添加web服务写入以下代码: 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.Configuration;/**//// <summary>/// AutoCompleteDepartSevice 的摘要说明/// </summary>[WebService(Namespace = "http://tempuri.org/")][WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)][System.Web.Script.Services.ScriptService]public class AutoCompleteDepartSevice : System.Web.Services.WebService ...{ public AutoCompleteDepartSevice () ...{ //如果使用设计的组件,请取消注释以下行 //InitializeComponent(); } //定义数组保存获取的内容 //两个参数“prefixText”表示用户输入的前缀,count表示返回的个数 [WebMethod] public String[] GetCompleteDepart(string prefixText, int count) ...{ // 如果数组为空 string[] autoCompleteWordList = null; if (autoCompleteWordList == null) ...{ //读取数据库的内容 SqlConnection conn=new SqlConnection(ConfigurationManager.ConnectionStrings["DepartConnectionString"].ToString()); conn.Open(); SqlDataAdapter da=new SqlDataAdapter("select departname from departinfo where departname like'" + prefixText + "%' order by departname",conn); DataSet ds = new DataSet(); da.Fill(ds); //读取内容文件的数据到临时数组 string[] temp = new string[ds.Tables[0].Rows.Count]; int i=0; foreach( DataRow dr in ds.Tables[0].Rows) ...{ temp[i] = dr["departname"].ToString(); i++; } //将临时数组的内容赋给返回数组 autoCompleteWordList = temp; if (conn.State == ConnectionState.Open) conn.Close(); } String[] returnValue = new string[count]; returnValue = autoCompleteWordList; //返回数据 return returnValue; } } 若是双击ajax控件则只需要写入GetCompleteDepart方法内的代码