Created
August 27, 2015 11:42
-
-
Save anuraj/6e6414995e761fe5e15c to your computer and use it in GitHub Desktop.
JQuery Autocomplete with Ajax using ASP.NET Web Methods
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <%@ Page Language="C#" AutoEventWireup="true" %> | |
| <script runat="server"> | |
| [System.Web.Services.WebMethod] | |
| public static string[] GetData(string q) | |
| { | |
| var result = new[] { @"ActionScript", "AppleScript", "Asp", "BASIC", "C", "C++", | |
| "Clojure", "COBOL", "ColdFusion", "Erlang","Fortran", "Groovy","Haskell", | |
| "Java", "JavaScript", "Lisp", "Perl", "PHP", "Python","Ruby", "Scala", "Scheme" }; | |
| return result.Where(x => x.StartsWith(q)).ToArray(); | |
| } | |
| </script> | |
| <!DOCTYPE html> | |
| <html xmlns="http://www.w3.org/1999/xhtml"> | |
| <head runat="server"> | |
| <title></title> | |
| <link rel="stylesheet" href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css" /> | |
| </head> | |
| <body> | |
| <form id="form1" runat="server"> | |
| <div> | |
| <div class="ui-widget"> | |
| <label for="tags">Tags: </label> | |
| <asp:TextBox runat="server" ID="txtAutoComplete" /> | |
| </div> | |
| </div> | |
| <asp:ScriptManager runat="server" ID="ScriptMgr" EnablePageMethods="true" /> | |
| </form> | |
| <script src="//code.jquery.com/jquery-1.10.2.js"></script> | |
| <script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script> | |
| <script> | |
| $(document).ready(function () { | |
| $("#<%=txtAutoComplete.ClientID %>").autocomplete({ | |
| source: function (request, response) { | |
| $.ajax({ | |
| url: "WebForm1.aspx/GetData", | |
| data: "{ 'q': '" + request.term + "', 'limit': '10' }", | |
| dataType: "json", | |
| type: "POST", | |
| contentType: "application/json; charset=utf-8", | |
| dataFilter: function (data) { return data; }, | |
| success: function (data) { | |
| response($.map(data.d, function (item) { | |
| return { | |
| label: item, | |
| value: item + "" | |
| } | |
| })) | |
| } | |
| }); | |
| } | |
| }); | |
| }); | |
| </script> | |
| </body> | |
| </html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment