Click here to Skip to main content
6,595,444 members and growing! (20,722 online)
Email Password   helpLost your password?
Web Development » ASP.NET Controls » General License: The Code Project Open License (CPOL)

Ajax ComboBox in ASP.NET

By Najmul Hoda

I have created a ComboBox control that mimics the functionality of Google suggest. As you type a search phrase, it automatically displays a list of matching entries in a dropdown.
C#, Javascript, XML, ASP, ASP.NET, WebForms, Ajax
Posted:5 Nov 2008
Views:13,288
Bookmarked:43 times
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
12 votes for this article.
Popularity: 4.47 Rating: 4.14 out of 5
1 vote, 8.3%
1

2
1 vote, 8.3%
3
1 vote, 8.3%
4
9 votes, 75.0%
5

Introduction

I have created a ComboBox control that mimics the functionality of Google suggest.

As you type a search phrase, it automatically displays a list of matching entries in a dropdown without any postback.

See the picture below displaying matching moves with the ComboBox Control.

AjaxComboBox.gif

Using the Code

I have used five files to make this project:

  1. A user control ComboBox.ascx - It contains one textbox to enter and listbox to display result.
  2. A JavaScript file AjaxCallBack.js - Used to request and get response data from server
  3. A default.aspx to display the user control - It asks for input and displays the result
  4. A frmRemote.aspx - It gets data from the server and sends filtered result to the caller
  5. An EmployeeList.xml - It contains the list of employees

Whenever we press a key in the textbox, on onkeyup event, a getResult() function is called.

That calls a FillCombo() function that sends a request to the server and gets a response.

Here is the JavaScript to make a call and get a response from the server.

// JScript File
var XMLHttp; 
var requestURL = 'http://localhost:1455/AjaxComboBox/frmRemote.aspx?SearchText='; 
var is_ie = (navigator.userAgent.indexOf('MSIE') >= 0) ? 1 : 0; 
var is_ie5 = (navigator.appVersion.indexOf("MSIE 5.5")!=-1) ? 1 : 0; 
var is_opera = ((navigator.userAgent.indexOf("Opera6")!=-1)||
		(navigator.userAgent.indexOf("Opera/6")!=-1)) ? 1 : 0; 
    
var count=0;
var is_netscape = (navigator.userAgent.indexOf('Netscape') >= 0) ? 1 : 0; 
var _Combo;

function FillCombo(combo,txt)
{
    _Combo=combo;
    var url = requestURL + txt ;
                       
    XMLHttp = GetXmlHttpObject(stateChangeHandler);
    XMLHttp.open('GET',url,true);
    XMLHttp.send(null);
}
    
function GetXmlHttpObject(handler)
{
    var objXmlHttp = null;
    if (is_ie)
    { 
        var strObjName = (is_ie5) ? 'Microsoft.XMLHTTP' : 'Msxml2.XMLHTTP'; 
        try
        { 
            objXmlHttp = new ActiveXObject(strObjName); 
            objXmlHttp.onreadystatechange = handler; 
        } 
        catch(e)
        { 
           alert('IE detected, but object could not be created. 
		Verify that active scripting and activeX controls are enabled'); 
           return; 
        } 
     } 
     else if (is_opera)
     { 
        alert('Opera detected. The page may not behave as expected.'); 
        return; 
     } 
     else
     { 
        objXmlHttp = new XMLHttpRequest(); 
        objXmlHttp.onload = handler; 
        objXmlHttp.onerror = handler; 
     } 
     return objXmlHttp; 
} 
     
function stateChangeHandler() 
{           
    if(XMLHttp.readyState==4 || XMLHttp.readyState == 'complete')
    { 
        var str = XMLHttp.responseText;    
        if (XMLHttp.responseXML.documentElement != null)
        {   
            FillComboBox_Blur(XMLHttp.responseXML.documentElement);
        }
        else
        {
            alert("No Match");
        }                         
    }   
}  
    
function FillComboBox_Blur(Node)
{    
    var NodeList;  
    NodeList = _Combo;
        
    var TextNodeList = Node.getElementsByTagName('Text'); 
    var IdNodeList = Node.getElementsByTagName('Id');     
        
    var textName; 
    var valueName;
          
    for (var count = NodeList.options.length-1; count >-1; count--)
    {
        NodeList.options[count] = null;            
    }        
        
    for (var count = 0; count < TextNodeList.length; count++)
    {           
        textName = GetInnerText(TextNodeList[count]);  
        valueName = GetInnerText(IdNodeList[count]); 
        TempOption = new Option( textName, valueName,  false, false);
        NodeList.options[NodeList.length] = TempOption; 
        NodeList.size= NodeList.options.length;
        NodeList.style.display='block';
    } 
    if(TextNodeList.length==0)
    {
        NodeList.style.display='none';          
    }
}     
    
function GetInnerText (node)
{
    return (node.textContent || node.innerText || node.text) ;
}

A call from the JavaScript is made to the server. On the server, there is a file frmRemote.aspx, that retrieves the result from the XML file, filters it and sends the resultant data as response.

if (Request["SearchText"] != null)
        {
            string SearchText = Request["SearchText"].ToString();
            int count = 0;
            string strXmlNames = "";

            if (SearchText != "")
            {
                XPathDocument xDoc = new XPathDocument(System.Web.HttpContext.
		Current.Request.PhysicalApplicationPath + "EmployeeList.xml");
                XPathNavigator xNav = xDoc.CreateNavigator();

                XPathExpression strExpression = xNav.Compile("Employee/EmpName");

                XPathNodeIterator xIterator = xNav.Select(strExpression);

                while (xIterator.MoveNext())
                {
                    XPathNavigator nav = xIterator.Current.Clone();
                    if (nav.Value.Trim().StartsWith(SearchText, true, null))
                    {
                        strXmlNames += "<element><Text>" + nav.Value + 
				"</Text> <Id>" + nav.Value + "</Id></element>";
                        count++;
                    }
                }                
            }
            if(count>0)
                strXmlNames += "<element><Text>" + "Total search result: " + 
			count + "</Text> <Id>" + count + "</Id></element>";
            strXmlNames = "<?xml version=\"1.0\" ?><Records>" + strXmlNames + 
			"</Records>";

            Response.Clear();
            Response.ContentType = "text/xml";

            Response.Write(strXmlNames);
            Response.End();
        } 

Point of Interest

The amazing thing about this control is that every time you enter a letter into the search box, an AJAX call is performed to retrieve a list of matching results from the server. 

History

  • 5th November, 2008: Initial post

License

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

About the Author

Najmul Hoda


Member
Najmul Hoda is a Master of Computer Application.
He has worked with .Net technologies in web development and has been programming since 2007.
He is very comfortable in various languages,RDBMS & platforms from C++,C# to VB.NET with Access & MS SQLServer
from Javascript,AJAX to ASP.NET.

Besides programming he loves playing mouth organ, guitar, listening to music,singing, bike riding & cricket.


mail him at : ansari.najmul@gmail.com


Najmul's Articles
Find His All Code Project Atricles


Occupation: Web Developer
Company: NetEdge Computing Solutions
Location: India India

Other popular ASP.NET Controls articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 6 of 6 (Total in Forum: 6) (Refresh)FirstPrevNext
Generalproblem with unicode data Pinmemberamirghaf3:55 18 May '09  
Generalnice PinmemberVuyiswa Maseko3:07 13 Feb '09  
GeneralI am getting error while running this application PinmemberWin32Prog20:14 10 Nov '08  
GeneralRe: I am getting error while running this application PinmemberNazmul Hoda6:26 11 Nov '08  
QuestionCool PinmemberHelbrax4:05 7 Nov '08  
AnswerRe: Cool PinmemberNazmul Hoda7:53 7 Nov '08  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 5 Nov 2008
Editor: Deeksha Shenoy
Copyright 2008 by Najmul Hoda
Everything else Copyright © CodeProject, 1999-2009
Web18 | Advertise on the Code Project