开发者

Using jQuery UI Autocomplete in ASP.NET

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 fi开发者_C百科eld on Select item event of autocomplete ?

Update My main challenge is how to Serialize ID ?


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);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜