Thursday, July 3, 2008

AutoCompleteExtender Ajax

Its the AutoCompleteExtender Ajax Control :-

-------------------
When you Create write the words in the TextBox at a time web service i Method GetData() call & fetch the values from the database & return the String Array of the data that show in the TextBox.

----------------------

asp:TextBox ID="txtname" runat="server" >

ajaxToolkit:AutoCompleteExtender ID="AutoCompleteExtender1" Enabled="true" runat="server" MinimumPrefixLength="1" TargetControlID="txtname" CompletionSetCount="20" CompletionInterval="0000" EnableCaching="true" ServiceMethod="GetData" ServicePath="~/WebService.asmx">


Note-: Here is some invalid tags. Please Ignore it.......
-------------------------------------------------------------------------

It's a web service-:

using System;
using System.Data;
using System.Web;
using System.Collections;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.ComponentModel;
using System.IO;
using System.Data.SqlClient;
using System.Configuration;
using System.Collections.Generic;

///
/// Summary description for WebService
///
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[ToolboxItem(false)]
[System.Web.Script.Services.ScriptService]
public class WebService : System.Web.Services.WebService
{


[WebMethod]
public string[] GetData(string prefixText)
{
List listString = new List();
using (SqlConnection con = new SqlConnection("Initial Catalog=Employee;Server=local;User ID=sa;Password=sa;"))
{
SqlCommand cm = new SqlCommand("SELECT top 5 name FROM WeFire_User Where Name like '" + prefixText + "%'", con);
con.Open();
SqlDataReader dr = cm.ExecuteReader();
if (dr.HasRows)
{
while (dr.Read())
{
listString.Add(dr["name"].ToString());
}
}
}
string[] str = listString.ToArray();
return str;

}

}

No comments: