前台相关代码:       <asp:ScriptManager ID="ScriptManager1" runat="server">
        <Services >
        <asp:ServiceReference  Path="~/WebService.asmx"/>
        </Services>
        </asp:ScriptManager>
        
        <br />
        搜索框:<asp:TextBox ID="Key" runat="server" >
        </asp:TextBox><asp:Button ID="Button1" runat="server"
            Text="查询" />
            
        <cc1:AutoCompleteExtender ID="AutoCompleteExtender1" runat="server" TargetControlID="Key" ServiceMethod="FindDish" CompletionSetCount="3"
        ServicePath="WebService.asmx" MinimumPrefixLength="1" CompletionInterval="1000"
         EnableCaching="true" 
        
        >
       
        </cc1:AutoCompleteExtender>
        
        <p />
webservice 中相关代码: public string[] FindDish(string prefix,int count)
    {
        string strSql = "select DishName From Dish where DishName like '%"+prefix+"%'";
        string strConn="Data Source=LIBAOZHEN\\LIBAOZHENSQL;Initial Catalog=GrogshopWeb;User ID=****;Password=****";
        SqlConnection conn=new SqlConnection(strConn);
        conn.Open();
        DataSet ds=new DataSet();
        SqlDataAdapter da=new SqlDataAdapter(strSql,conn);
        da.Fill(ds,"ds");
        DataTable dt=ds.Tables[0];
        string[] result=new string[dt.Rows.Count];
        for(int i=0;i<dt.Rows.Count;i++)
        {
            result.SetValue(dt.Rows[i][0],i);
        }
        return result;
     
    }
经过测试:数据库查询有效,因为我在后台试着Response过,返回值“金钱青瓜 酸菜鱼 等”。问题应当在调用方面。但是搞不懂。请大哥帮忙!

解决方案 »

  1.   

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="System_OA.Customer.WebForm1" %><%@ 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>无标题页</title>
        <script type="text/javascript">
        
          function pageLoad() {
          }
        
        </script>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
            <asp:ScriptManager ID="ScriptManager1" runat="server" />
            <asp:TextBox ID="MyTextBox" runat="server"></asp:TextBox>
            <cc1:AutoCompleteExtender ID="AutoCompleteExtender1" runat="server"  ServicePath="~/WebService/autoComplete.asmx" ServiceMethod="GetAutoComplete" MinimumPrefixLength="2" CompletionSetCount="5" TargetControlID="MyTextBox">
            </cc1:AutoCompleteExtender>
        </div>
        </form>
    </body>
    </html>namespace System_OA.WebService
    {
        /// <summary>
        /// autoComplete 的摘要说明
        /// </summary>
        [WebService(Namespace = "http://tempuri.org/")]
        [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
        [ToolboxItem(false)]
        // 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消对下行的注释。
        [System.Web.Script.Services.ScriptService]
        public class autoComplete : System.Web.Services.WebService
        {        [WebMethod]
            public string[] GetAutoComplete(string prefixText, int count)
            { 
                List<string> list=new List<string>(count);
                CustomerConsultDAL dal = new CustomerConsultDAL();
                DataTable dt=dal.GetTopList(prefixText);
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    list.Add(dt.Rows[i][0].ToString());
                }
                return list.ToArray();
            }
        }
    }