Web.Service.cs代码using System;
using System.Collections;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml.Linq;
using System.Data;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Web.Services.Description;
using System.Web.Script.Services;
/// <summary>
/// Summary description for WebService
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
 [System.Web.Script.Services.ScriptService()]
public class WebService : System.Web.Services.WebService {    public WebService () {
        //Uncomment the following line if using designed components 
        //InitializeComponent(); 
    }
    [ScriptMethod ]    [WebMethod]
    public string[] GetCompletionList(string prefixText , int count)
    {
        //List<string> items = new List<string>();
        ArrayList array = new ArrayList();
        SqlConnection cn = new SqlConnection();
        cn.ConnectionString = @"Data Source=.\SQLEXPRESS;
                                      AttachDbFilename=|DataDirectory|\MyData.mdf;
                                      Integrated Security=True;User Instance=True";
        SqlCommand cmd = new SqlCommand();
        cmd.Connection = cn;
        cmd.CommandText  = "SELECT TOP "+count +"[UserName] FROM User WHERE [UserName] LIKE '%"+prefixText+"%'";
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataSet ds = new DataSet();
        da.Fill(ds, "select");
        cn.Open();
        foreach (DataRow dr in ds.Tables["select"].Rows)
        {
            array.Add(dr["UserName"].ToString());
        }
        return (string[])array .ToArray (typeof (string ));
        //SqlDataReader dr = cmd.ExecuteReader();
        //if (dr.HasRows)
        //{        //    if (dr.Read())
        //    {
        //        //items.Add(dr.GetString(0));
        //        //items.Add(dr["UserName"].ToString());
        //        items.Add(dr.GetString(0) + "," + count);
        //    }
        //}
       
        //dr.Close();
        cn.Close();
        //return items.ToArray();
       
    }
}.aspx代码<%@ 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.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>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">    
        <asp:ScriptManager ID="ScriptManager1" runat="server">
        </asp:ScriptManager>
        <br />
    
        <asp:TextBox ID="TextBox1" runat="server" AutoPostBack ="true" Height="28px" 
            Width="573px"></asp:TextBox>
    
    <cc1:AutoCompleteExtender ID="AutoCompleteExtender1" runat="server" TargetControlID ="TextBox1"
      EnableCaching ="true" ServicePath="WebService.asmx"  ServiceMethod="GetCompletionList"
       MinimumPrefixLength ="1" CompletionSetCount ="5" CompletionInterval ="100" UseContextKey ="true">
    </cc1:AutoCompleteExtender>
    
   
    </form>
    </body>
</html>