Skip to content

Instantly share code, notes, and snippets.

@anuraj
Created August 27, 2015 11:42
Show Gist options
  • Select an option

  • Save anuraj/6e6414995e761fe5e15c to your computer and use it in GitHub Desktop.

Select an option

Save anuraj/6e6414995e761fe5e15c to your computer and use it in GitHub Desktop.
JQuery Autocomplete with Ajax using ASP.NET Web Methods
<%@ 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