前台页面:
- <form id="form1" runat="server">
- <asp:ScriptManager ID="ScriptManager1" runat="server">
- <Services>
- <asp:ServiceReference Path="WebService.asmx" />
- </Services>
- </asp:ScriptManager>
- <div>
- <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
- <cc1:AutoCompleteExtender ID="AutoCompleteExtender1" TargetControlID="TextBox1" ServiceMethod="GetName"
- runat="server" MinimumPrefixLength="1" CompletionSetCount="5" EnableCaching="true" ServicePath="WebService.asmx">
- </cc1:AutoCompleteExtender>
- </div>
- </form>
webService:
注意:string prefixText, int count 这两个参数 必须完全一致 包括大小写
- #region 获取Name
- [WebMethod]
- public string[] GetName(string prefixText, int count)
- {
- if (count == 0)
- count = 10;
- List<string> list = Tools.GetTestName(count, prefixText);
- return list.ToArray();
- }
- #endregion
底层类:
- #region 返回名字集合
- public static List<string> GetTestName(int count, string prefixText)
- {
- using (SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationManager.AppSettings["connStr"].ToString()))
- {
- conn.Open();
- string sql = string.Format("SELECT Top {0} NAME FROM TiShi_Test WHERE NAME LIKE '{1}%'", count, prefixText);
- List<string> list = new List<string>(count);
- DataSet ds = new DataSet();
- SqlDataAdapter dar = new SqlDataAdapter(sql, conn);
- dar.Fill(ds);
- DataTable dt = ds.Tables[0];
- foreach (DataRow dr in dt.Rows)
- {
- list.Add(dr["Name"].ToString());
- }
- return list;
- }
- }
- #endregion