前端代码:
<input id="ipt_typeName" name="ipt_typeName"/>
js:
$('#ipt_typeName').combobox({
url: '../Ajax/GetType_Name.ashx',
valueField: 'id',
textField: 'typeName'
});
一般处理程序:
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/json";
DataSet ds = assess.Combx(); //查询数据的方法
string strJSON = DataTableToJsonWithJavaScriptSerializer(ds.Tables[0]);
context. Response.Write(strJSON);
context. Response.End();
}
//Table 转换为json
public string DataTableToJsonWithJavaScriptSerializer(DataTable table)
{
JavaScriptSerializer jsSerializer = new JavaScriptSerializer();
List<Dictionary<string, object>> parentRow = new List<Dictionary<string, object>>();
Dictionary<string, object> childRow;
foreach (DataRow row in table.Rows)
{
childRow = new Dictionary<string, object>();
foreach (DataColumn col in table.Columns)
{
childRow.Add(col.ColumnName, row[col]);
}
parentRow.Add(childRow);
}
return jsSerializer.Serialize(parentRow);
}