Home > Blockchain >  Jquery/Ajax autocomplete textbox not working in ASP.net
Jquery/Ajax autocomplete textbox not working in ASP.net

Time:11-01

I have a text box that I am trying to have autocomplete with values from a database. The code is, however, not doing anything when I begin typing in the text box. Does anyone have any idea how to fix this? The Scripts are in the head of the page.

 <asp:TextBox placeholder="Search by job title" runat="server" CssClass="search" ID="searchTitle"></asp:TextBox>
  <script src="https://ajax.aspnetcdn.com/ajax/jquery/jquery-1.8.0.js"></script>
    <script src="https://ajax.aspnetcdn.com/ajax/jquery.ui/1.8.22/jquery-ui.js"></script>
    <script>
        $(function () {
            $("#searchTitle").autocomplete({
                source: function (request,response) {
                    var param = { posting_jobPosition: $("#searchTitle").val() };
                    $.ajax({
                        url: "jobseekerHome.aspx/GetTitles",
                        data: JSON.stringify(param),
                        type: "post",
                        contentType: "application/json; charset=utf-8",
                        datafilter: function (data) { return data; },
                        success: function (data) {
                            response($.map(data.d, function (item) { return {value: item }}))
                        },
                    });
                },
                minlength: 1
            });
        });
    </script>
   [WebMethod]
        public static List<string> GetTitles(string posting_jobPosition)
        {
            string CS = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
            OleDbConnection Connection = new OleDbConnection(CS);
            List<string> Titles = new List<string>();
            string query = string.Format("SELECT posting_jobPosition FROM BusinessJobPosting WHERE (posting_jobPosition LIKE '%{0}%' AND isActive = true)", posting_jobPosition);

            OleDbCommand oleCom1 = new OleDbCommand(query, Connection);

            Connection.Open();

            OleDbDataReader reader = oleCom1.ExecuteReader();

            while (reader.Read())
            {
                Titles.Add(reader[0].ToString());
            }

            Connection.Close();

            return Titles;
        }

Thanks guys :)

CodePudding user response:

One possible reason that I see is the ID. Change the id as:

$("#<%=searchTitle.ClientID%>").autocomplete({

related: Accessing control client name and not ID in ASP.NET

  • Related