HI WELCOME TO KANSIRIS

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

Leave a Comment

Introduction


The Autocomplete textbox widget is one of the most popular jQuery UI widget, which is used  for provides suggestions while user type into the input field (textbox). You noticed how the related suggestions highlights when we start typing in the Google search box, this is called autocomplete.

We can use this widget to build highly interactive web applications.

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 Multi-values autocomplete textbox in ASP.NET without using Webservice & AJAX autocompleteextender control.

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 tables 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 multi values 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>Multi value 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.  
  5. <script language="javascript" type="text/javascript">
  6. $(function () {
  7. $('#<%=txtCompanyName.ClientID%>').autocomplete({
  8. source: function (request, response) {
  9. $.ajax({
  10. url: "Default.aspx/GetCompanyName",
  11. data: "{ 'pre':'" + request.term + "'}",
  12. dataType: "json",
  13. type: "POST",
  14. contentType: "application/json; charset=utf-8",
  15. success: function (data) {
  16. response($.map(data.d, function (item) {
  17. return {
  18. CompanyName: item.CompanyName,
  19. Industry: item.Industry,
  20. json: item
  21. }
  22. }))
  23. },
  24. error: function (XMLHttpRequest, textStatus, errorThrown) {
  25. alert(textStatus);
  26. }
  27. });
  28. },
  29. focus: function (event, ui) {
  30. $('#<%=txtCompanyName.ClientID%>').val(ui.item.CompanyName);
  31. return false;
  32. },
  33. select: function (event, ui) {
  34. $('#<%=txtCompanyName.ClientID%>').val(ui.item.CompanyName);
  35. return false;
  36. },
  37. }).data("ui-autocomplete")._renderItem = function (ul, item) {
  38. return $("<li>")
  39. .append("<a>Company:" + item.CompanyName + "<br>Industry: " + item.Industry + "</a>")
  40. .appendTo(ul);
  41. };
  42. });
  43. </script>

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

Import this 2 Namespace
  1. using System.Web.Services;
  2. using System.Web.Script.Services;
Write this function into your page behind.
  1. [WebMethod]
  2. [ScriptMethod(ResponseFormat= ResponseFormat.Json)]
  3. public static List<TopCompany> GetCompanyName(string pre)
  4. {
  5. List<TopCompany> allCompany = new List<TopCompany>();
  6. using (MyDatabaseEntities dc = new MyDatabaseEntities())
  7. {
  8. allCompany = (from a in dc.TopCompanies
  9. where a.CompanyName.StartsWith(pre)
  10. select a).ToList();
  11. }
  12. return allCompany;
  13. }

Step-7: Run Application.

0 comments:

Post a Comment

Note: only a member of this blog may post a comment.