Riddhi Valecha

Riddhi Valecha

  • 428
  • 3.2k
  • 397k

JQuery Autocomplete/Autosearch Textbox

Dec 16 2019 7:42 AM
Dear Team,
 
I want to implement an Autocomplete / Autosearch textbox in normal web page.
 
I have tried all the methods, but I get only 1 error -
0x800a01b6 - JavaScript runtime error: Object doesn't support property or method 'autocomplete'
 
My Code -
  1. <%@ Page Language="VB" AutoEventWireup="false" CodeFile=" frmManageAdmission.aspx.vb" Inherits=" frmManageAdmission" %>  
  2. <!DOCTYPE html PUBLIC>  
  3. <html xmlns="http://www.w3.org/1999/xhtml">  
  4. <head runat="server">  
  5. <title> </title>  
  6. </head>  
  7. <body>  
  8. <form id="Form1" method="post" runat="server">  
  9. <link rel="stylesheet" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.16/themes/base/jquery-ui.css" type="text/css" media="all" />  
  10. <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.js" type="text/javascript"></script>  
  11. <script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.16/jquery-ui.min.js" type="text/javascript"></script>  
  12. <script type="text/javascript">  
  13. $(document).ready(function () {  
  14. $("#<%=txtFirstName.ClientID%>").autocomplete({  
  15. source: function (request, response) {  
  16. $.ajax({  
  17. url: '<%=ResolveUrl("~/frmManageAdmission.aspx/GetFirstNames") %>',  
  18. data: "{ 'prefixText': '" + request.term + "'}",  
  19. dataType: "json",  
  20. type: "POST",  
  21. contentType: "application/json; charset=utf-8",  
  22. success: function (data) {  
  23. response($.map(data.d, function (item) {  
  24. return {  
  25. label: item.split('-x-')[1],  
  26. val: item.split('-x-')[0]  
  27. }  
  28. }))  
  29. },  
  30. error: function (response) {  
  31. alert(response.responseText);  
  32. },  
  33. failure: function (response) {  
  34. alert(response.responseText);  
  35. }  
  36. });  
  37. },  
  38. select: function (e, i) {  
  39. $("#<%=hdnfldFirstName.ClientID%>").val(i.item.val);  
  40. },  
  41. minLength: 2  
  42. });  
  43. });  
  44. </script>  
  45. <table cellspacing="0" cellpadding="6" width="100%" border="0">  
  46. <tr>  
  47. <td>First Name</td>  
  48. <td>  
  49. <asp:TextBox ID="txtFirstName" runat="server" MaxLength="100" OnTextChanged="txtFirstName_TextChanged" AutoPostBack="true" CssClass="autosuggest"></asp:TextBox>  
  50. <asp:HiddenField ID="hdnfldFirstName" runat="server" />  
  51. </td>  
  52. </tr>  
  53. </table>  
  54. <br>  
  55. </form>  
  56. </body>  
  57. </html>  
Ccs/vb code -
  1. #Region "ConnectionString"  
  2. <WebMethod()> _  
  3. Public Shared Function ConnectionString() As String  
  4. Try  
  5. Return ConfigurationManager.ConnectionStrings("DBConnectionString").ConnectionString.Trim  
  6. Catch err As Exception  
  7. err.Message.ToString()  
  8. Return Nothing  
  9. End Try  
  10. End Function  
  11. #End Region  
  12. #Region "CheckIfConnectionIsOpen"  
  13. <WebMethod()> _  
  14. Public Function IsOpen() As Boolean  
  15. Try  
  16. SqlConnection.ClearAllPools()  
  17. con = New SqlConnection  
  18. If con.ConnectionString Is String.Empty Then  
  19. con.ConnectionString = ConnectionString()  
  20. End If  
  21. If con.State = ConnectionState.Closed Then  
  22. con.Open()  
  23. Return True  
  24. Else  
  25. Return True  
  26. End If  
  27. Catch err As Exception  
  28. err.Message.ToString()  
  29. Return False  
  30. End Try  
  31. End Function  
  32. #End Region  
  33. #Region "GetFirstNames"  
  34. ''<ScriptMethod(ResponseFormat:=ResponseFormat.Json)>  
  35. <WebMethod()> _  
  36. Public Function GetFirstNames(ByVal prefixText As StringAs String()  
  37. Try  
  38. If IsOpen() = True Then  
  39. Dim s As String = HttpContext.Current.Session("AutoSuggestFirstName").ToString  
  40. ''Dim TrainingSubjects = New List(Of String)  
  41. Dim FirstNames = New List(Of String)  
  42. cmd = New SqlCommand  
  43. cmd.Connection = con  
  44. cmd.CommandText = "SP_AutoSuggestFirstName"  
  45. cmd.CommandType = CommandType.StoredProcedure  
  46. cmd.Parameters.AddWithValue("@PreficxFirstName", prefixText)  
  47. Using sdr As SqlDataReader = cmd.ExecuteReader  
  48. While sdr.Read  
  49. FirstNames.Add(String.Format("{0}-{1}", sdr("Candidate_FName"), sdr("am_id")))  
  50. End While  
  51. End Using  
  52. Return FirstNames.ToArray  
  53. Else  
  54. Return Nothing  
  55. End If  
  56. Catch err As Exception  
  57. err.Message.ToString()  
  58. Return Nothing  
  59. End Try  
  60. End Function  
  61. #End Region  
Please let me know - where I am going wrong..

Answers (11)