1

I am using jQuery UI Autocomplete with ASP.NET like this : First I am serializing Good names into string array then I passing Array to source of jQuery UI AutoComplete

   //PageLoad
    tadok.Entities.TList<tadok.Entities.Good> GoodEntites = tadok.Data.DataRepository.GoodProvider.GetAll();
    List<string> GoodNames = new List<string>();
    foreach (object item_loopVariable in GoodEntites) {
        item = item_loopVariable;
        GoodNames.Add(string.Format(item.GodTitle));

    }
    JavaScriptSerializer serializer = new JavaScriptSerializer();
    Values = serializer.Serialize(GoodNames);

MarkUp Code :

  var availableTags = <%= Values %>
       $("#txtGoodAutoComplete").autocomplete({
         source: availableTags

          });

The object that I am serializing has property with the name ID. How can I serialize ID and storing ID in for example Hidden field on Select item event of autocomplete ?
Update My main challenge is how to Serialize ID ?

1 Answer 1

2

Use select event,

If your object is looks like {'label':'A', 'value':'A', 'ID':'7897975'}

$( ".selector" ).autocomplete({
select: function(event, ui) { 
     $('#hiddenField').val(ui.item.ID);//Item is your selected object.
}
});

Update:

I have never worked on C#. But there should be any built in JSON parser available.

In java i create JSON format like this,

JSONArray jsonArray = new JSONArray();
JSONObject jsonObject = null;

for(Country country : countries){
                jsonObject = new JSONObject();
                jsonObject.put("label", country.getName());
                jsonObject.put("value", country.getCode());
                jsonObject.put("id", country.getId().toString());
                jsonArray.add(jsonObject);
            }
String json = jsonArray.toJSONString();//The json string will look like, [{'label':'A', 'value':'A', 'id':'7897925'},{'label':'B', 'value':'B', 'id':'7497975'},{'label':'C', 'value':'C', 'id':'7843975'},{'label':'D', 'value':'D', 'id':'7857975'}]

//Your autocomplete source should return something like the above json string



By using Javascript Serializer : First Add a class :

public class GoodAutoComplete
{
    public string label;
    public string value;
    public string ID;
}


Then Serialize object like this :

tadok.Entities.TList<tadok.Entities.Good> GoodEntites = tadok.Data.DataRepository.GoodProvider.GetAll();
List<GoodAutoComplete> GoodItems = new List<GoodAutoComplete>();
foreach (object item_loopVariable in GoodEntites) {
    item = item_loopVariable;
    GoodItems.Add(new GoodAutoComplete {
        ID = item.GodId,
        label = string.Format(item.GodTitle + "{(0)}", item.GodDescrp).Replace("()", ""),
        value = string.Format(item.GodTitle + "{(0)}", item.GodDescrp).Replace("()", "")
    });
}
JavaScriptSerializer serializer = new JavaScriptSerializer();
Values = serializer.Serialize(GoodItems);
4
  • Thank you, Would you please explain that how can I serialize ID ? Now In my code I just serialized Good Name .
    – Shahin
    Commented Jun 28, 2011 at 6:02
  • How can I serialize my object like {'label':'A', 'value':'A', 'ID':'7897975'} ?
    – Shahin
    Commented Jun 28, 2011 at 6:15
  • Look for any JSON library for C#
    – user405398
    Commented Jun 28, 2011 at 6:39
  • @TamilVendhan : Thank you for your attention. I just added C# sample with .NET JavascriptSERIALIZER to your answer.
    – Shahin
    Commented Jun 28, 2011 at 6:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.