Saturday, August 10, 2019

How to Implement Autocomplete textbox in ASP.NET WITHOUT using Webservice & AJAX autocomplete extender

There are many examples out there demonstrating how to implement autocomplete textbox, but most of them are using  Ajax autocompleteextender control and webservice, which can make it complex especially to beginners.

Today I will show you, how to implementing Autocomplete textbox in ASP.NET without using Webservice & AJAX autocompleteextender control. Here in this application, we will implement autocomplete textbox  using jQuery AutoComplete Widget.

Here I have done following

  1. Simple autocomplete textbox application using jQuery AutoComplete Widget.
  2. Multi-values autocomplete textbox.
  3. Autocomplete textbox with image.


If you are developing your application using AngularJS, must see Implement autocomplete textbox in AngularJS

Steps :

Step - 1: Create New Project.

Go to File > New > Project > Select asp.net web forms application > Entry Application Name > Click OK.

Step-2: Add a Database.

Go to Solution Explorer > Right Click on App_Data folder > Add > New item > Select SQL Server Database Under Data > Enter Database name > Add.

Step-3: Create table for fetch data.

Open Database > Right Click on Table > Add New Table > Add Columns > Save > Enter table name > Ok.
In this example, I have used one table as below

Step-4: Add Entity Data Model.

Go to Solution Explorer > Right Click on Project name form Solution Explorer > Add > New item > Select ADO.net Entity Data Model under data > Enter model name > Add.
A popup window will come (Entity Data Model Wizard) > Select Generate from database > Next >

Chose your data connection > select your database > next > Select tables > enter Model Namespace > Finish.

Step-5: Add a Webpage and Design for implementing Autocomplete textbox.

Go to Solution Explorer > Right Click on Project name form Solution Explorer > Add > New item > Select web form/ web form using master page under Web > Enter page name > Add.

HTML Code 
  1. <h3>Auto Complete Textbox without using Web Service</h3>
  2. <table>
  3. <tr>
  4. <td>Type Company Name: </td>
  5. <td>
  6. <div class="ui-widget" style="text-align:left">
  7. <asp:TextBox ID="txtCompanyName" runat="server" Width="350px" CssClass="textboxAuto" Font-Size="12px" />
  8. </div>
  9. </td>
  10. </tr>
  11. </table>
JS Code 
  1. <link rel="stylesheet" href="http://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css">
  2. <script src="http://code.jquery.com/jquery-1.9.1.js"></script>
  3. <script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>
  4. <script language="javascript" type="text/javascript">
  5. $(function () {
  6. $('#<%=txtCompanyName.ClientID%>').autocomplete({
  7. source: function (request, response) {
  8. $.ajax({
  9. url: "Default.aspx/GetCompanyName",
  10. data: "{ 'pre':'" + request.term + "'}",
  11. dataType: "json",
  12. type: "POST",
  13. contentType: "application/json; charset=utf-8",
  14. success: function (data) {
  15. response($.map(data.d, function (item) {
  16. return { value: item }
  17. }))
  18. },
  19. error: function (XMLHttpRequest, textStatus, errorThrown) {
  20. alert(textStatus);
  21. }
  22. });
  23. }
  24. });
  25. });
  26. </script>

Step-6: Write a function for fetch data from database and show in Autocomplete TextBox.


  1. [WebMethod]
  2. [ScriptMethod(ResponseFormat= ResponseFormat.Json)]
  3. public static List<string> GetCompanyName(string pre)
  4. {
  5. List<string> allCompanyName = new List<string>();
  6. using (MyDatabaseEntities dc = new MyDatabaseEntities())
  7. {
  8. allCompanyName = (from a in dc.TopCompanies
  9. where a.CompanyName.StartsWith(pre)
  10. select a.CompanyName).ToList();
  11. }
  12. return allCompanyName;
  13. }

Step-7: Run Application.

No comments:

Post a Comment

How to register multiple implementations of the same interface in Asp.Net Core?

 Problem: I have services that are derived from the same interface. public interface IService { } public class ServiceA : IService { ...