Sunday, August 11, 2019

How to implement Autocomplete textbox with image in ASP.NET WITHOUT using Webservice and AJAX autocomplete extender

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 a 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 with an image.

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>Autocomplete Textbox With Image</h3>
  2. <table>
  3. <tr>
  4. <td>Select Country :</td>
  5. <td>
  6. <div class="ui-widget" style="text-align:left">
  7. <asp:TextBox ID="txtCountryName" 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. $('#<%=txtCountryName.ClientID%>').autocomplete({
  7. source: function (request, response) {
  8. $.ajax({
  9. url: "Default.aspx/GetCountry",
  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.  
  17. return {
  18. CountryName: item.CountryName,
  19. Logo: item.Logo,
  20. json: item
  21. }
  22. }))
  23. },
  24. error: function (XMLHttpRequest, textStatus, errorThrown) {
  25. alert(textStatus);
  26. }
  27. });
  28. },
  29. focus: function (event, ui) {
  30. // this is for when focus of autocomplete item
  31. $('#<%=txtCountryName.ClientID%>').val(ui.item.CountryName);
  32. return false;
  33. },
  34. select: function (event, ui) {
  35. // this is for when select autocomplete item
  36. $('#<%=txtCountryName.ClientID%>').val(ui.item.CountryName);
  37. return false;
  38. }
  39. }).data("ui-autocomplete")._renderItem = function (ul, item) {
  40. // here return item for autocomplete text box, Here is the place
  41. // where we can modify data as we want to show as autocomplete item
  42. return $("<li>")
  43. .append("<a style='padding-left:40px; background-image:url(/CountryLogo/" + item.Logo + ");" +
  44. "background-repeat:no-repeat;background-position:left center;' >" + item.CountryName + "</a>").appendTo(ul);
  45. };
  46. });
  47. </script>

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

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<CountryMaster> GetCountry(string pre)
  4. {
  5. List<CountryMaster> allCountry = new List<CountryMaster>();
  6. using (MyDatabaseEntities dc = new MyDatabaseEntities())
  7. {
  8. allCountry = dc.CountryMasters.Where(a => a.CountryName.StartsWith(pre)).OrderBy(a => a.CountryName).ToList();
  9. }
  10. return allCountry;
  11. }
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 { ...