Ajax,谷歌提示AutoCompleteExtender控件
提示内容从数据库中读取:
------------------------------------------页面
<asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
<!--df-->
歌曲名称:<asp:TextBox ID="txtSongName" runat="server"></asp:TextBox>
<asp:Button ID="btnSelect" runat="server" Text="搜索" onclick="btnSelect_Click" />
<cc1:AutoCompleteExtender ID="AutoCompleteExtender1" ServicePath="WebService.asmx" MinimumPrefixLength="1" TargetControlID="txtSongName" runat="server" ServiceMethod="GetCompletionList"
UseContextKey="True">
</cc1:AutoCompleteExtender>
<!--
1.TargetControlID:指定将被辅助完成自动输入的控件ID,这里的控件只能是TextBox;
2.ServicePath:指出提供服务的WEB服务路径,若不指出则ServiceMethod表示本页面对应的方法名;
3.ServiceMethod:指出提供服务的方法名;
4.MinimumPrefixLength:指出开始提供提示服务时,TextBox控件应有的最小字符数,默认为3;
5.CompletionSetCount:显示的条数,默认为10;
6.EnableCaching:是否在客户端缓存数据,默认为true;
7.CompletionInterval:从服务器读取数据的时间间隔,默认为1000,单位:毫秒
-->
------------------------------------------WebServices.asmx服务的WebService.cs
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Services
using System.Web.Services.Protocols;
using System.Xml.Linq;
using System.Data.SqlClient;
/// <summary>
///WebService 的摘要说明
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
//若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消对下行的注释。
[System.Web.Script.Services.ScriptService]
public class WebService : System.Web.Services.WebService {
public WebService () {
//如果使用设计的组件,请取消注释以下行
//InitializeComponent();
}
[WebMethod]
public string[] GetSuperviserList(string prefixText)
{
List<string> items = new List<string>();//泛型
string sql = "select songName from Music where songName like '%" + key + "%'";
SqlConnection conn = new SqlConnection("Data Source=AP-HZ-00052\\SQLEXPRESS;Initial Catalog=Test;Integrated Security=True");
conn.Open();
SqlCommand cmd = new SqlCommand(sql, conn);
SqlDataReader dr= cmd.ExecuteReader();
while (dr.Read())
{
items.Add(dr[0].ToString());
}
dr.Close();
conn.Close();
return items.ToArray();
}
}