Home > DeveloperSection > Forums > JQuery autocomplete using handler
Andrew Watson
Andrew Watson

Total Post:39

Points:273
Posted on    October-30-2014 11:41 PM

 JQuery JavaScript  Javascript Events 
Ratings:


 1 Reply(s)
 603  View(s)
Rate this:

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:

public class KeywordHandler : IHttpHandler

        {

 

            public void 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());

                    }

                }

            }

 

            public bool IsReusable

            {

                get

                {

                    return false;

                }

            }

        }

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



Brad Pitt
Brad Pitt

Total Post:61

Points:429
Posted on    October-30-2014 11:44 PM

I added only Keyword class and returned it.

And it works.

public class KeywordHandler : IHttpHandler

        {

            class Keyword

            {

                public string value { get; set; }

            }

 

            public void 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 distinct Keyword from [dbo].[Log] where " + "Keyword like @SearchText + '%' and ResultCount != 0";

                        cmd.Parameters.AddWithValue("@SearchText", prefixText);

                        cmd.Connection = conn;

 

                        List<Keyword> suggestedList = new List<Keyword>();

                        conn.Open();

                        using (SqlDataReader sdr = cmd.ExecuteReader())

                        {

                            while (sdr.Read())

                            {

                                suggestedList.Add(new Keyword { value = sdr["Keyword"].ToString() });

                            }

                        }

                        conn.Close();

 

                        suggestedList.OrderBy(x => x.value).ToList();

 

                        context.Response.Write(Newtonsoft.Json.JsonConvert.SerializeObject(suggestedList.Take(10)));

                    }

                }

            }

 

            public bool IsReusable

            {

                get

                {

                    return false;

                }

            }

        }


Don't want to miss updates? Please click the below button!

Follow MindStick