Autocomplete Extender in Asp.Net Using Ajax ToolKit Control

  Autocomplete Extender in Asp.Net Using Ajax ToolKit Control

Introduction : Using the Ajax AutocompleteExtender you  can Manage SearchBar easily.  This control can diasplay the sorted data from the list of data from database.

<body> 
    <form id="form1" runat="server"> 
    <asp:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server">
    </asp:ToolkitScriptManager>
    <table style="margin-top:50px;color:Maroon"> 
        <tr> 
            <td> 
                Search Name
            </td> 
            <td> 
                <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox> 
                <asp:AutoCompleteExtender ServiceMethod="Search" MinimumPrefixLength="1
                    CompletionInterval="10" EnableCaching="false" CompletionSetCount="1"    TargetControlID="TextBox1
                    ID="AutoCompleteExtender1" runat="server" FirstRowSelected="false"></asp:AutoCompleteExtender> 
            </td> 
        </tr> 
    </table> 
    </form> 
</body>   

Code : In Code Behind file you can see the code below :
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
namespace AjaxControl
{

    public partial class BoxSearch : System.Web.UI.Page
    {
      
        protected void Page_Load(object sender, EventArgs e)
        {
           
        }
        [System.Web.Script.Services.ScriptMethod()]
        [System.Web.Services.WebMethod]
        public static List<string> Search(string prefixText)

        {
            SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=D:\StringLength\AjaxControl\AjaxControl\App_Data\SearchDB.mdf;Integrated Security=True;User Instance=True");


            SqlCommand cmd = new SqlCommand();
            cmd.CommandText = "select name from tbl_user where " +  "name like @NAME + '%'";
            cmd.Parameters.AddWithValue("@Name", prefixText);
            cmd.Connection = con;
            con.Open();
            List<string> user = new List<string>();
            using (SqlDataReader dr = cmd.ExecuteReader())
            {
                while (dr.Read())
                {
                    user.Add(dr["name"].ToString());
                }
            }
            con.Close();
            return user;
        }
        }

Output : When you run the project you van see the output like below :


Previous
Next Post »