Link to home
Start Free TrialLog in
Avatar of arunatata
arunatata

asked on

Auto-suggest on textbox

Hi,

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#?

Thanks,
Aruna
ASKER CERTIFIED SOLUTION
Avatar of Dirk Haest
Dirk Haest
Flag of Belgium image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Gautham Janardhan
Gautham Janardhan

if u are working in as.net
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
in the case of ajax
firs add a http handler
  <httpHandlers>
  <add verb = "POST,GET" path = "ajax/*.ashx" type = "Ajax.PageHandlerFactory,Ajax"/>
  </httpHandlers>

down load the Ajax.dll from the net
and add reference to ur project then in the page load
Ajax.Utility.RegisterTypeForAjax(typeof(WebForm1));

then
function to return the data in code behind

[Ajax.AjaxMethod()]
            public DataTable GetData(string location)

            {
                  try
                  {
                        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 "
                              +"catalog=northwind;password=pass");
                        DataTable FData =
                              new DataTable();
                        FAdapter.Fill(FData);
                        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"
                        type="text">

and

two functions

            function ajaxFunctionListBox()
            {
                  WebForm1.Add(document.getElementById("ListBo").value
                  ,Add_CallBack_ListBox);
                  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

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

            }