前台
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<ajaxToolkit:AutoCompleteExtender ID="AutoCompleteExtender1" runat="server"
TargetControlID="TextBox1"
CompletionSetCount="12"
EnableCaching="true"
MinimumPrefixLength="1"
ServicePath="sql_test.asmx"
CompletionListElementID="Panel1"
ServiceMethod="GetCompleteList">
</ajaxToolkit:AutoCompleteExtender>
</div>
<asp:TextBox ID="TextBox1" runat="server" ForeColor="Black"> </asp:TextBox>
sql_test.asmx的代码
using System;
using System.Web.Services;
using System.Collections.Generic;
using System.Data.SqlClient;
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.Web.Script.Services.ScriptService]
public class sql_test : System.Web.Services.WebService {
public sql_test () {
//如果使用设计的组件,请取消注释以下行
//InitializeComponent();
}
[WebMethod]
public string[] GetCompleteList(string prefixText, int count)
{
List <string> items = new List <string> (count);//泛型
SqlConnection myCon = new SqlConnection("Server=AAAA;uid=sa;pwd=TTiger;Database=kmjxc_08_test");//数据库连接
myCon.Open();//打开数据库连接
SqlCommand myCmd = new SqlCommand("select top " + count + " username from user_item where username like '" + prefixText + "%'group by username order by username ", myCon);
SqlDataReader myDR = myCmd.ExecuteReader();
while (myDR.Read())
{
items.Add(myDR["username"].ToString());
}
myCon.Close();//关闭数据库连接
return items.ToArray();
}
}
Ajax AutoComplete 在Sql数据库中读取数据
最新推荐文章于 2022-09-02 23:19:33 发布