Monday 5 March 2012

Auto Complete Extender In Asp Dot Net


<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:AutoCompleteExtender ID="AutoCompleteExtender1" runat="server"
  TargetControlID="TextBox1" ServiceMethod="GetCompletionList"
  ServicePath="AjaxLearning.asmx" MinimumPrefixLength="1"
  ShowOnlyCurrentWordInCompletionListItem="True">
</asp:AutoCompleteExtender>
Using Service File Method : (.asmx)
[WebMethod]
public string[] GetCompletionList(string prefixText)
{
SqlConnection conn = new                    SqlConnection(ConfigurationManager.ConnectionStrings["mystring"].ConnectionString);
SqlCommand com = new SqlCommand("SELECT Username FROM Table_Login WHERE Username LIKE '" + prefixText + "%'", conn);
com.CommandType = CommandType.Text;
conn.Open();
SqlDataReader reader = com.ExecuteReader();
List<string> strarr = new List<string>();
while (reader.Read())
{
strarr.Add(reader.GetString(0));
}
conn.Close();
return strarr.ToArray();
}
Using Handler : (.ashx)
public class AutocompleteData : IHttpHandler {
public void ProcessRequest (HttpContext context) {
string firstname = context.Request.QueryString["q"];
string sql = "select top 10 Username from Table_Login where Username like '" + firstname + "%'";
using (SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["mystring"].ConnectionString))
using (SqlCommand command = new SqlCommand(sql, connection))
{
connection.Open();
using (SqlDataReader reader = command.ExecuteReader())
{
while (reader.Read())
{
context.Response.Write(reader.GetString(0) + Environment.NewLine);
}
}
}
}
public bool IsReusable {
get {
return false;
}
}
}


No comments:

Post a Comment