Wednesday, May 6, 2009

Implement Ajax AutoComplete Extender inside Gridview

Hi All,
     This Post implements enabling AJAX Autocomplete Extender while Editing Gridview Control in ASP.NET.Do change Connection String as of yours...

So...here it is.....



   <asp:ScriptManager ID="ScriptManager1" runat="server">

        <Services>

            <asp:ServiceReference Path="~/AutoComplete.asmx" />

        Services>

    asp:ScriptManager>

    <asp:GridView ID="GridView1" runat="server" CellPadding="4" ForeColor="#333333" GridLines="None"

        AutoGenerateColumns="False" OnRowEditing="GridView1_RowEditing">

        <RowStyle BackColor="#FFFBD6" ForeColor="#333333" />

        <FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />

        <PagerStyle BackColor="#FFCC66" ForeColor="#333333" HorizontalAlign="Center" />

        <SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="Navy" />

        <HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />

        <AlternatingRowStyle BackColor="White" />

        <Columns>

            <asp:CommandField ShowEditButton="True" />

            <asp:TemplateField HeaderText="Age" SortExpression="ID">

                <ItemTemplate>

                    <asp:Label ID="lblID" runat="server" Text=''>asp:Label>

                ItemTemplate>

                <EditItemTemplate>

                    <asp:Label ID="lblID" runat="server" Text=''>asp:Label>

                EditItemTemplate>

            asp:TemplateField>

            <asp:TemplateField HeaderText="Name" SortExpression="Name">

                <ItemTemplate>

                    <asp:Label ID="lblName" runat="server" Text=''>asp:Label>

                ItemTemplate>

                <EditItemTemplate>

                    <asp:TextBox ID="txtName" runat="server" Text=''>asp:TextBox>

                    <ajaxToolkit:AutoCompleteExtender runat="server" ID="autoComplete1" TargetControlID="txtName"

                        ServicePath="AutoComplete.asmx" ServiceMethod="GetCompletionList" MinimumPrefixLength="1" CompletionInterval="10" EnableCaching="true" CompletionSetCount="12" />

                EditItemTemplate>

            asp:TemplateField>

            <asp:TemplateField HeaderText="Location" SortExpression="Location">

                <ItemTemplate>

                    <asp:Label ID="lblLocation" runat="server" Text=''>asp:Label>

                ItemTemplate>

                <EditItemTemplate>

                    <asp:Label ID="lblLocation" runat="server" Text=''>asp:Label>

                EditItemTemplate>

            asp:TemplateField>

        Columns>

    asp:GridView>

 WebService Code

 [WebMethod]

  public DataTable GetRecords(string strName)

    {

        string strConn = ConfigurationManager.ConnectionStrings["TestConnectionString"].ConnectionString;

        SqlConnection con = new SqlConnection(strConn);

        SqlCommand cmd = new SqlCommand();

        cmd.Connection = con;

        cmd.CommandType = System.Data.CommandType.Text;

        cmd.Parameters.AddWithValue("@Name", strName);

        cmd.CommandText = "Select Name from Emp where Name like '%'+@Name+'%'";

        DataSet objDs = new DataSet();

        SqlDataAdapter dAdapter = new SqlDataAdapter();

        dAdapter.SelectCommand = cmd;

        con.Open();

        dAdapter.Fill(objDs);

        con.Close();

        return objDs.Tables[0];

    }

 

    [WebMethod]

    public string[] GetCompletionList(string prefixText, int count)

    {

        if (count == 0)

        {

            count = 10;

        }

        DataTable dt = GetRecords(prefixText);

        List<string> items = new List<string>(count);

        for (int i = 0; i <>

        {

            string strName = dt.Rows[i][0].ToString();

            items.Add(strName);

        }

        return items.ToArray();

    }

    


ASPX.CS Code


protected void Page_Load(object sender, EventArgs e)

    {

        Bind();

     

    }

 

    public void Bind()

    {

        SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["TestConnectionString"].ToString());

        conn.Open();

        SqlCommand cmd = new SqlCommand("Select * from EMP", conn);

        DataTable dt = new DataTable();

        SqlDataAdapter da = new SqlDataAdapter();

        da.SelectCommand = cmd;

        da.Fill(dt);

        GridView1.DataSource = dt;

        GridView1.DataBind();

       

    }

    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)

    {

        GridView1.EditIndex = e.NewEditIndex;

        GridView1.DataBind();

    }