forum

Home / DeveloperSection / Forums / JQuery autocomplete using handler

JQuery autocomplete using handler

Anonymous User184830-Oct-2014

I' m trying to create a search box with autocomplete property and using a handler. I got all words from database but can not show them.

It' s jquery part:

$(function () {
   $("#search-box").autocomplete({
      source: "KeywordHandler.ashx",
      minLength: 1,
      //select: function (event, ui) {
      //alert(ui.item.id + " / " + ui.item.value);
      //}
    });
});

It' s handler part:

publicclassKeywordHandler : IHttpHandler
        {
 
            publicvoid ProcessRequest(HttpContext context)
            {
                string prefixText = context.Request.QueryString["term"];
                using (SqlConnection conn = new SqlConnection())
                {
                    conn.ConnectionString = ConfigurationManager.ConnectionStrings["DSN"].ConnectionString;
 
                    using (SqlCommand cmd = new SqlCommand())
                    {
 
                        cmd.CommandText = "select Keyword from [dbo].[Log] where " + "Keyword like @SearchText + '%'";
                        cmd.Parameters.AddWithValue("@SearchText", prefixText);
                        cmd.Connection = conn;
 
                        StringBuilder sb = new StringBuilder();
                        conn.Open();
                        using (SqlDataReader sdr = cmd.ExecuteReader())
                        {
                            while (sdr.Read())
                            {
                                sb.Append(sdr["Keyword"])
                                    .Append(Environment.NewLine);
                            }
                        }
                        conn.Close();
                        context.Response.Write(sb.ToString());
                    }
                }
            }
 
            publicbool IsReusable
            {
                get
                {
                    returnfalse;
                }
            }
        }

At handler, I can get all the words which I need but can not show them as offered keywords at search box.


Updated on 30-Oct-2014
I am a content writter !

Can you answer this question?


Answer

1 Answers

Liked By