在ASP.NET中使用jQuery UI自动完成 [英] Using jQuery UI Autocomplete in ASP.NET
问题描述
我使用jQuery UI自动完成与ASP.NET这样的:
首先我好序列名称转换字符串数组然后我传球达阵,以jQuery用户界面自动完成
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);
标记code:
var availableTags = <%= Values %>
$("#txtGoodAutoComplete").autocomplete({
source: availableTags
});
这是我的序列化对象有名字ID属性。我怎么可以序列ID和存储在ID上自动完成的选择项事件例如隐藏字段?结果
更新
我的主要挑战是如何序列ID?
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 ?
推荐答案
使用选择活动,
如果你的对象是看起来像 {'标签':'A','值':'A','ID':'7897975'}
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.
}
});
更新:
我从来没有工作过的C#。但是应该有任何内置JSON解析器使用。
I have never worked on C#. But there should be any built in JSON parser available.
在java中我创建JSON格式是这样,
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
结果
通过使用Javascript串行:
首先添加一个类:
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);
这篇关于在ASP.NET中使用jQuery UI自动完成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!