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
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
in the case of ajax
firs add a http handler
<httpHandlers>
<add verb = "POST,GET" path = "ajax/*.ashx" type = "Ajax.PageHandlerFactory,A jax"/>
</httpHandlers>
down load the Ajax.dll from the net
and add reference to ur project then in the page load
Ajax.Utility.RegisterTypeF orAjax(typ eof(WebFor m1));
then
function to return the data in code behind
[Ajax.AjaxMethod()]
public DataTable GetData(string location)
{
try
{
System.Data.SqlClient.SqlD ataAdapter FAdapter
= new System.Data.SqlClient.SqlD ataAdapter
("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;passwo rd=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.getE lementById ("ListBo") .value
,Add_CallBack_ListBox);
return false;
}
function Add_CallBack_ListBox(respo nse)
{
var datatbale = response.value;
var s = new Array();
for(var i=0;i<datatbale.Rows.lengt h;i++)
{
s[s.length] = "<option>"+datatbale.Rows[ i].LOCATIO NCODE+"</o ption>";
}
document.getElementById("T D1").inner HTML =
"<select id=\"sel\">"+s.join("")+"< /select>";
}
and done ... u get an instant list box
firs add a http handler
<httpHandlers>
<add verb = "POST,GET" path = "ajax/*.ashx" type = "Ajax.PageHandlerFactory,A
</httpHandlers>
down load the Ajax.dll from the net
and add reference to ur project then in the page load
Ajax.Utility.RegisterTypeF
then
function to return the data in code behind
[Ajax.AjaxMethod()]
public DataTable GetData(string location)
{
try
{
System.Data.SqlClient.SqlD
= new System.Data.SqlClient.SqlD
("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;passwo
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.getE
,Add_CallBack_ListBox);
return false;
}
function Add_CallBack_ListBox(respo
{
var datatbale = response.value;
var s = new Array();
for(var i=0;i<datatbale.Rows.lengt
{
s[s.length] = "<option>"+datatbale.Rows[
}
document.getElementById("T
"<select id=\"sel\">"+s.join("")+"<
}
and done ... u get an instant list box
and in windows forms
hook the text changed event
private void textBox1_TextChanged(objec t sender, System.EventArgs e)
{
//do the same thing
//to populate the data
// and show the data in
//an appropriate control
//like a combobox
}
hook the text changed event
private void textBox1_TextChanged(objec
{
//do the same thing
//to populate the data
// and show the data in
//an appropriate control
//like a combobox
}
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