Google Suggest Functionality Using AJAX
By AzamSharp
Views: 7350

Introduction:

Google Suggest is way cool. You type your search text and it gets the hits without doing the POSTBACK. You can easily implement this functionality using AJAX. In this article we will create a very small application that works like Google suggest. I have just implemented the basic features and you are free to enhance it in any way.

The implementation:   

Let's first see a simple screen shot so that we will have better idea what we are talking about.

See, when I type "A" all the names starting with "A" pops up. Now let's see how this is done.

First we need to implement the server side methods that will get the data from the database. Here is the GetDataSet method that gets the DataSet filled with data from the database.

private DataSet GetDataSet()

{

DataSet ds = null;

string query = @"SELECT * FROM tblPerson";

SqlConnection myConnection = new SqlConnection(GetConnectionString());

SqlDataAdapter ad = new SqlDataAdapter(query,myConnection);

ds = new DataSet();

ad.Fill(ds,"tblPerson");

return ds;

}

Now that we have got the DataSet method done. Let's make a method that will be called from the JavaScript.

[Ajax.AjaxMethod]

public string FillList(string s) {

string word = null;

DataSet ds = new DataSet();

ds = GetDataSet();

ArrayList wordsList = new ArrayList();

foreach(DataRow dr in ds.Tables[0].Rows)

{

wordsList.Add(dr["Name"] as String);

}

foreach(string str in wordsList)

{

if( str.StartsWith(s) && s!=null)

{

word += str + "<BR>";

}}return word; }

Now let's see the JavaScript code. First we made the TextBox as the HTML input control.

<input id="MyTextBox" type="text" onkeypress="GetWords(event,this.value)">

Now, we need to implement the GetWords method. The GetWords method takes two parameters one if the event and other is the TextBox value. If the character pressed in the input box is a "space" (using spacebar) than the text is displayed in the textbox depending on the selecting which is displayed in the DIV element.

<script language="javascript">

function GetWords(pressevent,myVal)
{
var charCode = (pressevent.which)? pressevent.which : (event.keyCode);

// Check for the enter key
if(charCode!= 13)
{
ForumsQuestions.FillList(myVal,GetWords_CallBack)

if(charCode == 32)
{

// Put the value from the from the DIV into the TextBox
var word = document.getElementById("MyDiv").innerHTML;

document.getElementById("MyTextBox").value = word.substring(0,word.length - 4);

}
}
}
function GetWords_CallBack(response)
{
if(response!=null)
{

document.getElementById("MyDiv").innerHTML = response.value;

}

}

</script>

That's it. Now many of you will be wondering that why did I call GetDataSet again and again on every keypress event. I know it's extremely bad for performance. A good idea is to use Cache but unfortunately you cannot use Cache object for the methods that are being called from a method marked with [Ajax.Method] attribute.  However you can use the ViewState object but be careful don't store too much information in the ViewState object.

For more information about why Cache cannot be implemented when a call is being made from Ajax marked method please visit my Blog entry Remote Scripting Caching Problems (AJAX).

I hope you liked the article, happy coding!

 

 

 

By AzamSharp


Enter Comment/Feedback
  •  
  •  
  •  
  •  
  •  

Comments/Feedbacks
Subject: autocomplete
Name: nil
Date: 2/24/2007 1:59:00 AM
Comment:
this is one of the good post i've seen for auto complete textbox without using contol and only ajax..but you haven't mentioned here about div that is displayed when user enter any character in textbox
Subject: I had small problem with this
Name: kalyan
Date: 3/15/2007 9:49:29 PM
Comment:
You have written ForumsQuestions.FillList(myVal,GetWords_CallBack) in java script.

I cant understand what is forumsquestions iam getting error with this.
Subject: Give a Details
Name: Lakshmanarayanan S
Date: 5/23/2007 3:47:02 AM
Comment:
When i am trying to use this code i getting the error

ForumsQuestions - is undifined

Plz give a solution to this.

It is very urgent for me
Plz reply
Subject: Good article...and i need some details
Name: Bala
Date: 5/29/2007 10:32:12 PM
Comment:
You have written ForumsQuestions.FillList(myVal,GetWords_CallBack) in java script.

I cant understand what is forumsquestions iam getting error with this.

It will show the error
Subject: ajax caching implementation
Name: Nitin
Date: 7/12/2007 4:51:15 AM
Comment:
hi
I have implemented the autofill functionality using AJAX and its working fine for me but the issue is that my select query returns lots of results so it takes time for the data to be retrieved and shown using AJAX. how do i optimize the performace of the autofill? how caching can be implemented on the same.?
Please respond soon
Subject: RE: ajax caching implementation
Name: AzamSharp
Date: 7/13/2007 4:55:31 PM
Comment:
Hi Nitin,

You can check out my new article in which I am using Client Callbacks.

http://gridviewguy.com/ArticleDetails.aspx?articleID=240

When fetching the rows you can also optimize the query by pulling out only TOP 20 rows.

Subject: test
Name: lordess911
Date: 1/30/2008 8:04:21 AM
Comment:
i didnt quite understand this line of code.can you please explain


ForumsQuestions.FillList(myVal,GetWords_CallBack)
Subject: RE: Test
Name: AzamSharp
Date: 1/30/2008 12:10:27 PM
Comment:
Hi lordess911, ForumQuestions is the name of the class which contains the method decorated with the Ajax.AjaxMethod attribute. Basically, it will be your code behind and the ForumQuestions will be the name of your webform. The first parameter is the actual value that I am passing and the second parameter is the name of the callback function which will be invoked once the async processing is finished.
Subject: Problem
Name: SandhyaBhavani
Date: 3/1/2008 3:21:55 AM
Comment:
Hi AzamSharp ,

Really Nice post.Thanx For posting....

I implimented ths code in my project.but im getting "Webform name is undefined.."

I added ajax reference and added in web.config also..

y im getting tat prob..

Please can u explain...

Thanx
Sandhya

Subject: RE: Problem
Name: AzamSharp
Date: 3/1/2008 8:12:05 AM
Comment:
Hi SandhyaBhavani, Try referencing the AJaxPro library instead AjaxPro2.



Join WebHost4Life.com






Copyright GridViewGuy 2007-2008