5
Answers

Help in auto complete textbox

saif beg

saif beg

11y
1.8k
1


i seek exact solution for my problem. kindly read below.

we are making txtsearch.text as autocomplete.
so we created search.ashx  generic handler file.



code:


<%@ WebHandler Language="C#" Class="Search" %>

 using System;
using System.Data.SqlClient;
using System.Text;
using System.Web;
using System.Configuration;

public class Search : IHttpHandler {

    public SqlConnection con;
    public void ProcessRequest (HttpContext context) {
        string searchText = context.Request.QueryString["q"];
    
       // SqlConnection con = new SqlConnection("Data Source=developer;Initial Catalog=ss_db;User Id=sa;Password=.");

        string constr = ConfigurationManager.ConnectionStrings["dcon"].ToString();

        con = new SqlConnection(constr);
        con.Open();
        SqlCommand cmd = new SqlCommand("select c_id,author_name,user_image from tbless where author_name Like '%'+@Author +'%'", con);
      //  SqlCommand cmd = new SqlCommand("select c_id,book_name,user_image from oldb_book_upload where book_name= @Search and author_name=@author", con);
        cmd.Parameters.AddWithValue("@Author", searchText);
    
        StringBuilder sb = new StringBuilder();
        using (SqlDataReader dr = cmd.ExecuteReader())
        {
            while (dr.Read())
            {
                sb.Append(string.Format("{0},{1}{2}", dr["author_name"], dr["user_image"], Environment.NewLine));
            }
        }
        con.Close();
        context.Response.Write(sb.ToString());
    }
 
    public bool IsReusable {
        get {
            return false;
        }
    }

}





and in default.aspx  we wrote function:


 <link href="csss/jquery.autocomplete.css" rel="stylesheet" type="text/css" />
    <script src="scriptss/jquery-1.4.1.min.js" type="text/javascript"></script>
    <script src="scriptss/jquery.autocomplete.js" type="text/javascript"></script>
 
<script type="text/javascript">
     $(document).ready(function () {
         $("#<%=txtSearch.ID%>").autocomplete("Search.ashx", {
             width: 200,
             formatItem: function (data, i, n, value) {
                 return "<img style = 'width:50px;height:50px' src='File_Upload/" + value.split(",")[1] + "'/> " + value.split(",")[0];
             },
             formatResult: function (data, value) {
                 return value.split(",")[0];
             }
         });
     });
</script>




this is working fine .means all the authors are showing in txtsearch. My problem is when i change the dropdown value to category then it must call another search.ashx generic handler .and in txtsearch only category should show not author.

please suggest

Answers (5)