So, I changed my Script to this:
<script src="../Scripts/jquery-ui-1.12.1.js"></script>
<script type="text/javascript">
$(document).ready(function () {
var prm = Sys.WebForms.PageRequestManager.getInstance();
prm.add_initializeRequest(InitializeRequest);
prm.add_endRequest(EndRequest);
InitAutoCompl();
InitAutoCompl2();
});
function InitializeRequest(sender, args) {
}
function EndRequest(sender, args) {
InitAutoCompl();
InitAutoCompl2();
}
function InitAutoCompl() {
$("#<%=txtName.ClientID %>").autocomplete({
source: function (request, response) {
$.ajax({
url: '<%=ResolveUrl("~/ProjectViews/AutoComplete.asmx/GetUsers") %>',
data: "prefixText=" + JSON.stringify(request.term),
dataType: "json",
contentType: "application/json; charset=utf-8",
success: function (data) {
response($.map(data.d, function (item) {
return {
label: item,
value: item
};
}));
},
error: function (response) {
alert(response.responseText);
},
failure: function (response) {
alert(response.responseText);
}
});
},
minLength: 1
});
}
And my code in my code in my .asmx:
public class AutoComplete : System.Web.Services.WebService
{
[WebMethod]
[ScriptMethod(UseHttpGet = true)]
public string[] GetUsers(string prefixText)
{
prefixText = prefixText.ToLower();
List<Users> users = new List<Users>();
BL.UsersManager usm = new BL.UsersManager();
users = usm.GetAllUsers();
var list = users.Where(u => u.Name.ToLower().Contains(prefixText))
.Select(u => String.Format("{0} ({1}) - {2}", u.Name, u.Tsz, u.OrganizationUnit.OrganizationUnitName))
.Take(10);
return list.ToArray();
}
I hope it will help others.