/// <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(); } [WebMethod] public string[] GetNames(string prefixText, int count) { List<string> KeyValue = new List<string>(); SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["sqlcon"].ToString()); count = 10; string str = "SELECT sid,sname FROM student WHERE sname like '" + prefixText + "%'"; con.Open(); SqlCommand com = new SqlCommand(str, con); SqlDataReader dr = com.ExecuteReader(); int i = 0; if (dr.HasRows) { while (dr.Read()) { string item = AjaxControlToolkit.AutoCompleteExtender.CreateAutoCompleteItem(dr["sname"].ToString(), dr["sid"].ToString()); KeyValue.Add(item); i++; } } return KeyValue.ToArray(); }}
<html xmlns="http://www.w3.org/1999/xhtml"><head runat="server"> <title>Untitled Page</title> <script type="text/javascript"> function GetKey(source, eventArgs) { document.getElementById("LblKey").innerText = eventArgs.get_value(); } </script></head><body> <form id="form1" runat="server"> <div> <asp:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server"> </asp:ToolkitScriptManager> <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox> <asp:AutoCompleteExtender ID="AutoCompleteExtender1" runat="server" TargetControlID="TextBox1" ServicePath="WebService.asmx" ServiceMethod="GetNames" MinimumPrefixLength="0" CompletionInterval="100" EnableCaching="true" CompletionSetCount="12" OnClientItemSelected="GetKey"> </asp:AutoCompleteExtender> <asp:Label ID="LblKey" runat="server" Text="Label"></asp:Label> </div> </form></body></html>