Click here to Skip to main content
11,932,770 members (53,791 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


50 bookmarked

Ajax AutoComplete in ASP.NET

, 19 Oct 2010 CPOL
Rate this:
Please Sign up or sign in to vote.
Ajax AutoComplete in ASP.NET

Without using AjaxControlToolKit, we can implement AutoComplete Extender using pure Ajax Call. This article explains how to do make AutoComplete Extender.

OnKeyUp event helps you to fetch data from database with Ajax call. Here one Handler.ashx handles the AJAX request form Client. I add a Class file to handle database operations to better coding practice. Below I explain the database helper Class. Class has one method:

GetTable(string sqlQuery) 

This returns DataTable after fetching data from database. And also includes Provide Class, this Class helps to get SqlConnection string.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;
/// <span class="code-SummaryComment"><summary>

Now we can look into Handler file. When request comes from Ajax Call from Client, it passes the data into our Database helper Class, handler file holds the data in DataTable. Result data are formatted in a table and written in the context. We can add JavaScript function for selecting the data, here api_helper.AddtoTaxtBox(selectedItem) manages client section of data.

Check Handler File

<%@ WebHandler Language="C#" Class="Handler" %>
using System;
using System.Web;
using System.Data;
public class Handler : IHttpHandler {
    public void ProcessRequest (HttpContext context) {

        HttpRequest request = HttpContext.Current.Request;
        //checking string null or empty
        if (!string.IsNullOrEmpty(request.QueryString["name"]))
            string name=request.QueryString["name"];
            //creating instance of new database helper
            DBHelper objDBHelper = new DBHelper();
            //creating Sql Query
            string sqlQuery = string.Format
		("SELECT Name FROM User WHERE Name LIKE '{0}%'", name);
            //filling data from database
            DataTable dataTable = objDBHelper.GetTable(sqlQuery);

             string table = string.Empty;
            //table for hold data
            table = "<table width='100%'>";
            string td = string.Empty;
            //checking datatable
                if (dataTable.Rows.Count >

Now we can check how Ajax works. On Textbox onKeyUp event, call the Ajax code. It sends the entered value into server using Ajax and the result is displayed in div control under the search textbox.

<%@ Page Language="C#" AutoEventWireup="true"  
	CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 

<html xmlns="">
<head runat="server">
     <form id="form1" runat="server">

         <asp:TextBox ID="txtName" runat="server" 
           <div id="myDiv"></div>
             <script language="javascript" type="text/javascript">

               if (typeof (api_helper) == 'undefined') { api_helper = {} }

               api_helper.doAjax = function(HandlerUrl, displayDiv) {
                var Req; try { Req = new XMLHttpRequest(); } 
		catch (e) { try { Req = new ActiveXObject("Msxml2.XMLHTTP"); }
		catch (e) { try { xmlHttp = new ActiveXObject("Microsoft.XMLHTTP"); } 
		catch (e) { return false; } } } Req.onreadystatechange = function() {
		if (Req.readyState == 4) { var d = document.getElementById(displayDiv); 
		d.innerHTML = Req.responseText; } }
      "GET", HandlerUrl, true); Req.send(null);

               api_helper.callAjax = function() {
                var text = document.getElementById("txtName").value;
                if (text != "") {
                    var requestUrl = "Handler.ashx?name=" + text;
                    var displayDiv="myDiv";
                    api_helper.doAjax(requestUrl, displayDiv);

              api_helper.AddtoTaxtBox = function(txt) {
                document.getElementById("txtName").value = txt;
                document.getElementById("myDiv").innerHTML = "";

Sample  ;


Thanks for reading this article. Please feel free to comment.

Tags Ajax AutoComplete, Ajax Example.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

raju melveetilpurayil
United Kingdom United Kingdom
Microsoft Certified Professional Developer.

You may also be interested in...

Comments and Discussions

GeneralMy vote of 4 Pin
Leonardo Paneque24-Apr-12 13:07
memberLeonardo Paneque24-Apr-12 13:07 
GeneralMy vote of 5 Pin
jpmontoya18211-Jan-12 10:03
memberjpmontoya18211-Jan-12 10:03 
GeneralMy vote of 2 Pin
stian.net30-Dec-11 0:32
memberstian.net30-Dec-11 0:32 
Nice article but your code is vulnerable for sql-injections. You should NEVER take a string value from a querystring and put it directly into a sql-command like that. Use parameters to avoid it..
GeneralRe: My vote of 2 Pin
raju melveetilpurayil8-Feb-14 16:48
memberraju melveetilpurayil8-Feb-14 16:48 
GeneralMy vote of 5 Pin
Suraj S Koneri28-Nov-11 23:36
memberSuraj S Koneri28-Nov-11 23:36 
GeneralRe: My vote of 5 Pin
raju melveetilpurayil29-Nov-11 1:21
memberraju melveetilpurayil29-Nov-11 1:21 
GeneralMy vote is 5 Pin
Ali Al Omairi28-Dec-10 10:59
memberAli Al Omairi28-Dec-10 10:59 
GeneralRe: My vote is 5 Pin
[raju.m][makhaai]23-Jan-11 14:17
member[raju.m][makhaai]23-Jan-11 14:17 
GeneralMy vote of 5 Pin
Ali Al Omairi28-Dec-10 10:56
memberAli Al Omairi28-Dec-10 10:56 
GeneralRe: My vote of 5 Pin
[raju.m][makhaai]21-Mar-11 17:15
member[raju.m][makhaai]21-Mar-11 17:15 
GeneralMy vote of 5 Pin
Shahriar Iqbal Chowdhury17-Nov-10 0:44
memberShahriar Iqbal Chowdhury17-Nov-10 0:44 
GeneralMy vote of 5 Pin
Brij15-Oct-10 7:33
mentorBrij15-Oct-10 7:33 
GeneralMy vote of 5 Pin
Rehan Hussain10-Oct-10 22:14
memberRehan Hussain10-Oct-10 22:14 
GeneralRe: My vote of 5 Pin
[raju.m][makhaai]15-Oct-10 1:26
member[raju.m][makhaai]15-Oct-10 1:26 
GeneralMy vote of 5 Pin
swapna.kurian9-Oct-10 8:26
memberswapna.kurian9-Oct-10 8:26 
GeneralRe: My vote of 5 Pin
[raju.m][makhaai]9-Oct-10 21:21
member[raju.m][makhaai]9-Oct-10 21:21 
GeneralMy vote of 5 Pin
ShohelShipon3-Oct-10 21:34
memberShohelShipon3-Oct-10 21:34 
GeneralNICE Pin
kashyap20106-Sep-10 20:54
memberkashyap20106-Sep-10 20:54 
GeneralRe: NICE Pin
[raju.m][makhaai]7-Sep-10 13:56
member[raju.m][makhaai]7-Sep-10 13:56 
GeneralRe: NICE Pin
shakil030400319-Oct-10 6:37
membershakil030400319-Oct-10 6:37 
GeneralRe: NICE Pin
[raju.m][makhaai]19-Oct-10 6:40
member[raju.m][makhaai]19-Oct-10 6:40 
GeneralMy vote of 5 Pin
senpaulose4-Sep-10 5:43
membersenpaulose4-Sep-10 5:43 
GeneralRe: My vote of 5 Pin
raju makhaai6-Sep-10 1:10
memberraju makhaai6-Sep-10 1:10 
GeneralRe: My vote of 5 Pin
EbenRoux6-Sep-10 19:52
memberEbenRoux6-Sep-10 19:52 
GeneralRe: My vote of 5 Pin
[raju.m][makhaai]7-Sep-10 14:02
member[raju.m][makhaai]7-Sep-10 14:02 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.151126.1 | Last Updated 19 Oct 2010
Article Copyright 2010 by raju melveetilpurayil
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid