Improve company productivity with a Business Account.Sign Up

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 258
  • Last Modified:

Using xmlhttp to populate a dropdown list with text and value

I am very new to javascript and expecially xmlhttp.  

I have been able to successfully implement a solution which retrieves data from an SQL DB as a user enter text into a text box.  Basically, what is happening is the user is attempting to enter a company name, and as the company name is entered into the textbox, on each onkeyup event the DB is queried and returns the top 5 companies whose name matches the entered string.  

This is working pretty well, except that the req.responseText is being applied to the innerHTML of a DIV.  I am interested in populating a dropdown list with companyname as the text and company_id as the value. Ideally, this would be a dropdown list that you can enter text into at run time instead of entering the search criteria into a separate text box.  As the user types, their entry stays at the top of the list and returned data populates the list.

Is this possible?

The javascript runs on an web form and uses another aspx helper file which builds the sql and runs the query onload.  Here is how I am doing this:

<script type="text/javascript">
var req;

function Initialize()
        req=new ActiveXObject("Msxml2.XMLHTTP");
            req=new ActiveXObject("Microsoft.XMLHTTP");
    if(!req&&typeof XMLHttpRequest!="undefined")
    req= new

} function SendQuery(key)
//Here is the url of the helper page which builds and runs the SQL query
    Initialize(); varurl="http://localhost/bdsearch/newpersonhelper.aspx?s=3&k="+key;
        req.onreadystatechange = Process;"GET", varurl, true);

function Process()
    if (req.readyState == 4)
        // only if "OK"
            if (req.status == 200)
                  //This is where the returned req.responseText populates the div
                  //I would like to populate the dropdown here instead.
                    document.getElementById("autocomplete").innerHTML =req.responseText;
                document.getElementById("autocomplete").innerHTML= "There was a problem retrieving data:<br>"+req.statusText;

function ShowDiv(divid)
   if (document.layers) document.layers[divid].visibility="show";
   else document.getElementById(divid).style.visibility="visible";

function HideDiv(divid)
   if (document.layers) document.layers[divid].visibility="hide";
   else document.getElementById(divid).style.visibility="hidden";

function BodyLoad()

INPUT id="keyword" onkeyup="SendQuery(this.value)" style="WIDTH: 500px" name="keyword" autocomplete="off">

and the codebehind of the helper aspx file (varurl="http://localhost/bdsearch/newpersonhelper.aspx?s=3&k="+key;):

 Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim keyword As String = Request("k")
        Dim QueryScope As String = Request("s")
        Dim sql As String
        If keyword <> Nothing And keyword.Trim() <> "" Then
            Select Case QueryScope
                Case "1" ' Normal Query
                    sql = "select top 5 company, company_id from company where company like '" + keyword.Trim().Replace("'", "''") + "%' order by company"
                Case "2" 'anything before or after keyword
                    sql = "select top 5 company, company_id from company where company like '%" + keyword.Trim().Replace("'", "''") + "%' order by company"
                Case "3" ' anything before or after keyword and THE %Keyword%
                    sql = "select top 5 company, company_id from company where company like '" + keyword.Trim().Replace("'", "''") + "%' or company like 'the " + keyword.Trim().Replace("'", "''") + "%' order by company"
            End Select

            Dim conn As SqlConnection = New SqlConnection(ConfigurationSettings.AppSettings("connectionString"))
            Dim dt As DataTable = New DataTable()
            Dim command As SqlCommand = New SqlCommand(sql, conn)
            Dim adapter As SqlDataAdapter = New SqlDataAdapter(command)

            Dim row As DataRow
            For Each row In dt.Rows
                'Response.Write("<strong>" + row("Company").ToString() + "</strong> <br />")
                Response.Write(row("Company").ToString() + "<br />")

        End If

    End Sub

Any ideas?

1 Solution
Hi abenage,

See my tutorial on this topic at

Hope this helps,
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now