A jQuery Autocomplete (with .Net) which actually works

Use JSBeautifier to format

JS:

$(“[id$=’_txtCountermandedBy’]”).autocomplete({
source: function(request, response) {
var param = { qry: $(“[id$=’_txtCountermandedBy’]”).val()
};
$.ajax({
type: “POST”,
url: “~/../MyWorkBasket.aspx/GetUserAutocomplete”,
data: JSON.stringify(param),
dataType: “json”,
contentType: “application/json; charset=utf-8”,
dataFilter: function(data) {
return data;
},
success: function(data) {
response($.map(data.d, function(item) {
return {
value: item.Value,
label: item.Label
}
}))
},
error: function(XMLHttpRequest, textStatus, errorThrown) {
alert(textStatus);
}
});
},
select: function(event, ui) {
//debugger;
$(“[id$=’_txtUserId’]”).val(ui.item.value);
$(“[id$=’_txtCountermandedBy’]”).val(ui.item.label);
OfficerSelected(true);
return false;
},
minLength: 3,
delay: 500
}).autocomplete(“widget”).addClass(“fixed-height”);
function OfficerSelected(state) {

if (state == true) {
$(“#imgUser”).attr(“src”, “~/../Styles/images/icons/tick.png”);
$(“#imgUser”).attr(“title”, “Officer selected”);
} else {
$(“#imgUser”).attr(“src”, “~/../Styles/images/icons/cross.png”);
$(“#imgUser”).attr(“title”, “Officer not selected”);
}
}

C#

[WebMethod(EnableSession = true)]
public static List<UserEntityMin> GetUserAutocomplete(string qry)
{

List<UserEntityMin> entList = UserUIC.GetUserAutocomplete(qry);
return entList;
}

public class UserEntityMin
{
string _userId;
string _fullDisplayName;

public string Value
{
get { return _userId; }
set { _userId = value; }
}
public string Label
{
get { return _fullDisplayName; }
set { _fullDisplayName = value; }
}
}

Advertisements