The ff example is from http://jqueryui.com/autocomplete/
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>jQuery UI Autocomplete - Default functionality</title>
<link rel="stylesheet" href="http://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css" />
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>
<link rel="stylesheet" href="/resources/demos/style.css" />
<script>
$(function() {
var availableTags = [
"ActionScript",
"AppleScript",
"Asp",
"BASIC",
"C",
"C++",
"Clojure",
"COBOL",
"ColdFusion",
"Erlang",
"Fortran",
"Groovy",
"Haskell",
"Java",
"JavaScript",
"Lisp",
"Perl",
"PHP",
"Python",
"Ruby",
"Scala",
"Scheme"
];
$( "#tags" ).autocomplete({
source: availableTags
});
});
</script>
</head>
<body>
<div class="ui-widget">
<label for="tags">Tags: </label>
<input id="tags" />
</div>
</body>
</html>
from this example I want to apply it in my ASP.NET project.
My concern is, is it possible to put the content of "availableTags" to a variable from code behind? If yes then how can I connect it to this code to accomplish the same thing?
I would appreciate your help.
Thank you!
Script portion of the ASPX page:
<script>
$(function() {
var availableTags = [
<%= GetAvailableTags() %>
];
$( "#tags" ).autocomplete({
source: availableTags
});
});
</script>
Code behind:
public string GetAvaliableTags()
{
var tags = new[] { "ActionScript", "Scheme" };
return String.Join(",", tags.Select(x => String.Format("\"{0}\"", x)));
}
The GetAvailableTags() function's output will be directly written to the page at render time.
This is a 'quick and dirty' solution of course. If, for example, your auto-complete items contain special characters like quotes, you'll have to take a different approach.
You can do it with ASP.NET AJAX Page Methods as the conduit to the server-side called via jQuery to retrieve a list of availableTags, like this:
<script>
$(document).ready(function() {
$("#tags").autocomplete({
source: function(request, response) {
$.ajax({
type: "POST",
url: "YourPage.aspx/GetAutoComplete",
data: "{}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(msg) {
}
});
}
});
});
</script>
Now in code-behind on YourPage.aspx, create the page method, like this:
[WebMethod]
public static string[] GetAutoComplete()
{
return new[]
{
"ActionScript",
"AppleScript",
"Asp",
"BASIC",
"C",
"C++",
"Clojure",
"COBOL",
"ColdFusion",
"Erlang",
"Fortran",
"Groovy",
"Haskell",
"Java",
"JavaScript",
"Lisp",
"Perl",
"PHP",
"Python",
"Ruby",
"Scala",
"Scheme"
};
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With