Auto-suggest on textbox

Posted on 2007-07-30
Last Modified: 2008-01-09

I want to create an auto-suggest textbox on a form (using ajax), which draws its suggestions from a database. How can I do this in c#?

Question by:arunatata
    LVL 53

    Accepted Solution

    AJAX AutoComplete/AutoSuggest TextBox

    Build an Anthem.NET [AJAX] Autosuggest Textbox
    LVL 29

    Expert Comment

    by:Gautham Janardhan
    if u are working in
    then ajax would be the best solution but if in windors form

    u would need to trap the text change event of the text box and get the items
    LVL 29

    Expert Comment

    by:Gautham Janardhan
    in the case of ajax
    firs add a http handler
      <add verb = "POST,GET" path = "ajax/*.ashx" type = "Ajax.PageHandlerFactory,Ajax"/>

    down load the Ajax.dll from the net
    and add reference to ur project then in the page load

    function to return the data in code behind

                public DataTable GetData(string location)

                            System.Data.SqlClient.SqlDataAdapter FAdapter
                                  = new System.Data.SqlClient.SqlDataAdapter
                                  ("select * from bt_location with(nolock) where "
                                  +" locationcode like '"+location+"%'"
                                  ,"workstation id=GEMFINL;packet "
                                  +"size=4096;user id=username;data source=server;"
                                  +"persist security info=True;initial "
                            DataTable FData =
                                  new DataTable();
                            return FData;
                      catch(Exception ex)
                            string str = ex.Message;
                      return null;

    then in the html side

    <INPUT id="ListBo" onkeyup="return ajaxFunctionListBox()" style="Z-INDEX: 103; LEFT: 176px; POSITION: absolute; TOP: 7px"


    two functions

                function ajaxFunctionListBox()
                      return false;
                function Add_CallBack_ListBox(response)
                  var datatbale = response.value;
                  var s = new Array();
                  for(var i=0;i<datatbale.Rows.length;i++)
                    s[s.length] = "<option>"+datatbale.Rows[i].LOCATIONCODE+"</option>";
                  document.getElementById("TD1").innerHTML =
                  "<select id=\"sel\">"+s.join("")+"</select>";

    and done ... u get an instant list box

    LVL 29

    Expert Comment

    by:Gautham Janardhan
    and in windows forms

    hook the text changed event
                private void textBox1_TextChanged(object sender, System.EventArgs e)
                         //do the same thing
                                             //to populate the data
                                           // and show the data in
                                           //an appropriate control
                                          //like  a combobox


    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
    This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
    Sending a Secure fax is easy with eFax Corporate ( First, Just open a new email message.  In the To field, type your recipient's fax number You can even send a secure international fax — just include t…
    Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

    754 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now