[英]loading remote data to select2 input type is not working
我正在嘗試使用asp.net中的以下方法將遠程數據加載為隱藏的select2輸入類型
public String ConvertDatasettoArrayList()
{
DataSet ds = new DataSet();
// List<UserDetails> details = new List<UserDetails>();
ArrayList list = new ArrayList();
SqlConnection con = new SqlConnection("server=PBSUSER5;database=graph;user id=sa;password=panacya");
SqlCommand cmd = new SqlCommand("usp_selecttables_info", con);
cmd.CommandType = CommandType.StoredProcedure;
con.Open();
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(ds);
StringBuilder sb = new StringBuilder();
sb.Append("[");
int i = ds.Tables[0].Rows.Count;
int j = 1;
foreach (DataRow dtrow in ds.Tables[0].Rows)
{
sb.Append("{id:" + Convert.ToInt32(dtrow["id"].ToString()) + ",text:");
sb.Append("'" + dtrow[1].ToString()+"'");
sb.Append("}");
if (i != j)
sb.Append(",");
j++;
list.Add(dtrow);
}
sb.Append("]");
System.Web.Script.Serialization.JavaScriptSerializer serializer = new System.Web.Script.Serialization.JavaScriptSerializer();
string s = sb.ToString();
return serializer.Serialize(s);
}
此方法返回以下字符串
[{id:1,text:'panacya'},{id:2,text:'items'},{id:3,text:'systems'},{id:4,text:'devlopment'},{id:5,text:'cellphones'},{id:6,text:'textfeels'}]
腳本是,當我靜態綁定該字符串數組時,它正在工作,但是當綁定遠程數據變量時,它不會加載。
$(function () {
alert('calling');
var d =JSON.parse( '<%=ConvertDatasettoArrayList()%>');
alert(d)
$("#ex2").select2({
data: [{ id: 1, text: 'panacya' }, { id: 2, text: 'items' }, { id: 3, text: 'systems' }, { id: 4, text: 'devlopment' }, { id: 5, text: 'cellphones' }, { id: 6, text: 'textfeels' }]// working fine but for below statement
data: d// remote data variable it is not binding
});
});
</script>
由於JSON-String已經包含'
,因此在將數據放入String中時,要么必須轉義它們,要么使用"
。請使用以下代碼設置d
:
var d=JSON.parse("<%=ConvertDatasettoArrayList()%>");
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.