July 17, 2012

AutoCompleteExtender From AJAX Control Tool Kit Code Example

To know how to use controls from AJAX Control Took Kit , you can read this post.

http://www.dotnetissues.com/2012/07/alwaysvisiblecontrolextender-from.html
Below is the cod sample for how to Use AutoCompleteExtender  Control.
Many times when we are typing in Text box we want the relevant text should come automatically after typing few letters , This functionality is being used in almost all the search engines.

To integrate this functionality we can use AutoCompleteExtender   from AJAX Control Toolkit

Drag n drop this control in Web form and set the property TargetControlID
and then Clink on Add AutoComplete Page Method on the target control as below


This will add GetCompletionList(string prefixText, int count, string contextKey) method in code -behind.
Where You have to write the Business logic to get the relevant List

I have a table 'EmployeeDetails' in DB with a column 'Name' as below

Below code I have written for the AutoComplete Name

AutoCompleteExtender .aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="AutoCompleteExtender.aspx.cs" Inherits="AutoCompleteExtender" %>


<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="asp" %>


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <asp:ScriptManager ID="ScriptManager1" runat="server">
    </asp:ScriptManager>
    <div>
    
    <asp:autocompleteextender ID="Autocompleteextender1" TargetControlID="txtName"  
            runat="server" ServiceMethod="GetCompletionList" UseContextKey="True"></asp:autocompleteextender>
     <br/>
        <asp:TextBox ID="txtName" runat="server"></asp:TextBox>
    </div>
    
    </form>
</body>
</html>


AutoCompleteExtender .aspx.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;




public partial class AutoCompleteExtender : System.Web.UI.Page
{
   
    protected void Page_Load(object sender, EventArgs e)
    {
       


    }


    [System.Web.Services.WebMethodAttribute(), System.Web.Script.Services.ScriptMethodAttribute()]
    public static string[] GetCompletionList(string prefixText, int count, string contextKey)
    {
        SqlConnection connObj = new SqlConnection();
        //DataSet ds;
        SqlCommand objCommand;
       
        connObj.ConnectionString = ConfigurationManager.ConnectionStrings["DotNetTrainingConnectionString"].ToString();
        connObj.Open();


        objCommand = new SqlCommand("Select  Name from Employeedetails where Name LIKE'"+prefixText+"%' ", connObj);
        SqlDataReader dr;
        List<string> NameList = new List<string>();
        dr = objCommand.ExecuteReader();
        while (dr.Read())
        {
            NameList.Add(dr["Name"].ToString());




        }


        return NameList.ToArray();
    }
}


and this is how the behavior will be.


No comments:

Post a Comment