Using jQuery UI Autocomplete in ASP.NET

ε祈祈猫儿з 提交于 2019-12-11 06:49:44

问题


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:


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


来源:https://stackoverflow.com/questions/6501993/using-jquery-ui-autocomplete-in-asp-net

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!