[System.Web.Services.WebMethod] [System.Web.Script.Services.ScriptMethod] public static string[] GetCompletionList(string prefixText, int count) { SqlConnection cn = new SqlConnection(); DataSet ds = new DataSet(); DataTable dt = new DataTable(); String strCn = "Data Source=MY-PC;Initial Catalog=SurveyDB;Integrated Security=True"; cn.ConnectionString = strCn; SqlCommand cmd = new SqlCommand(); cmd.Connection = cn; cmd.CommandType = CommandType.Text; cmd.CommandText = "SELECT * FROM FoodList WHERE Food like @Food"; cmd.Parameters.AddWithValue("@Food", "%" + prefixText + "%"); try { cn.Open(); cmd.ExecuteNonQuery(); SqlDataAdapter da = new SqlDataAdapter(cmd); da.Fill(ds); } catch { } finally { cn.Close(); } dt = ds.Tables[0]; List<string> txtItems = new List<string>(); String dbValues; foreach (DataRow row in dt.Rows) { dbValues = row["Food"].ToString(); dbValues = dbValues.ToLower(); txtItems.Add(dbValues); } return txtItems.ToArray(); }
<ajaxToolkit:AutoCompleteExtender ID="autoComplete1" runat="server" EnableCaching="true" BehaviorID="AutoCompleteEx" MinimumPrefixLength="2" TargetControlID="TextBox1" ServicePath="AutoComplete.asmx" ServiceMethod="GetCompletionList" CompletionInterval="1000" CompletionSetCount="20" CompletionListCssClass="autocomplete_completionListElement" CompletionListItemCssClass="autocomplete_listItem" CompletionListHighlightedItemCssClass="autocomplete_highlightedListItem" DelimiterCharacters=";, :" ShowOnlyCurrentWordInCompletionListItem="true"> <animations> <onshow> <sequence> <%-- Make the completion list transparent and then show it --%> <opacityaction opacity="0" /> <hideaction visible="true" /> <%--Cache the original size of the completion list the first time the animation is played and then set it to zero --%> <ScriptAction Script="// Cache the size and setup the initial size var behavior = $find('AutoCompleteEx'); if (!behavior._height) { var target = behavior.get_completionList(); behavior._height = target.offsetHeight - 2; target.style.height = '0px'; }" /> <%-- Expand from 0px to the appropriate size while fading in --%> <parallel duration=".4"> <fadein /> <length propertykey="height" startvalue="0"> EndValueScript="$find('AutoCompleteEx')._height" /> </length></parallel> </sequence> </onshow> <onhide> <%-- Collapse down to 0px and fade out --%> <parallel duration=".4"> <fadeout /> <length propertykey="height" startvaluescript="<br" mode="hold" /> "$find('AutoCompleteEx')._height" EndValue="0" /> </parallel> </onhide> </animations> <asp:TextBox ID="TextBox1" runat="server" Width="275px">
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)