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

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
0
arunatata
Asked:
arunatata
  • 3
1 Solution
 
DhaestCommented:
AJAX AutoComplete/AutoSuggest TextBox
http://www.codeproject.com/Ajax/AJAXAutoSuggest.asp?msg=2148628

Build an Anthem.NET [AJAX] Autosuggest Textbox
http://www.eggheadcafe.com/articles/20060401.asp
0
 
Gautham JanardhanCommented:
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
0
 
Gautham JanardhanCommented:
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

0
 
Gautham JanardhanCommented:
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

            }
0

Featured Post

NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

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