giri-net
giri-net

Reputation: 97

Jquery Autocomplete

iam using jquery autocomplete in asp.net project. it's not working. do you have any idea. the code is given below.

<script type="text/javascript">
$(function () {

    $('#clientabbrev').val("");

    $("#clientstate").autocomplete({
        source: "clientstates.aspx",
        select: function (event, ui) {
            $('#clientstateid').val(ui.item.clientid);
            $('#clientstateabbrev').val(ui.item.clientabbrev);
        }
    });

    $("#clientstate_abbrev").autocomplete({
        source: "clientstatesabbrev.aspx",
        minLength: 2
    });
});
</script>

problem is states.aspx returning the data but it is not showing in the jquery autocomplete control.

Upvotes: 1

Views: 478

Answers (1)

Johnny Oshika
Johnny Oshika

Reputation: 57482

Your server needs to return a JSON serialized array of objects with properties id, label, and value. E.g. :

[ { "id": "1", "label": "Mike Smith", "value": "Mike Smith" }, { "id": "2", "label": "Bruce Wayne", "value": "Bruce Wayne" }]

Can you confirm with firebug or Fiddler that your server is returning the correct response?

If you're having trouble serializing your data in C#, you can try using JavaScriptSerializer like this:

var result = from u in users
             select new {
               id = u.Id,
               value = u.Name,
               label = u.Name
             };

JavaScriptSerialier serializer = new JavaScriptSerializer();
var json = serializer.Serialize(result);
// now return json in your response

Upvotes: 1

Related Questions