Ajax autocomplete textbox code

Ajax auto complete textbox code

<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<
br />
<asp:Label ID="Label1" runat="server" BackColor="#FFCCFF" ForeColor="#003300"
    
Height="23px" Text="Products" Width="8%"></asp:Label>&nbsp;&nbsp;&nbsp;
<asp:TextBox ID="TextBox1" runat="server" AutoCompleteType="Disabled"
    
Height="27px" Width="236px"></asp:TextBox>
<
asp:AutoCompleteExtender ID="TextBox1_AutoCompleteExtender" runat="server"
    
DelimiterCharacters="" Enabled="True" MinimumPrefixLength="1"
    
ServiceMethod="GetCompletionList" ServicePath=""
    
TargetControlID="TextBox1" UseContextKey="True">
</
asp:AutoCompleteExtender
>

.cs code

    [System.Web.Services.WebMethodAttribute(), System.Web.Script.Services.ScriptMethodAttribute()]
    public static string[] GetCompletionList(string prefixText, int count, string contextKey)
    {
        
string cs = ConfigurationManager.ConnectionStrings["NorthwindCS"].ConnectionString;
        
SqlConnection cn = new SqlConnection(cs);
        
SqlDataAdapter da=new SqlDataAdapter("select ProductName from Products",cn);
        
DataSet ds = new DataSet();
        da.Fill(ds,
"Products");
        
DataView dv = new DataView(ds.Tables[0]);
        dv.RowFilter =
string.Format("ProductName like '{0}%'",prefixText);
        
int rcount, size;
        rcount = dv.Count;
        
if (rcount >= count)
            size = count;
        else
            size = rcount;
        
string[] Pnames = new string[size];
        
for (int i = 0; i < size; i++ )
        Pnames[i] = dv[i][
"ProductName"].ToString();
        
return Pnames;
    }



X

Build smarter apps with Machine Learning, Bots, Cognitive Services - Start free.

Start Learning Now