function BindRolesDropdownList() {
$.ajax({
type: "Post",
url: "Dashboard.aspx/PopulateSelectRoleList",
data: "{}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(msg) {
$('#ddlRoles').get(0).options.length = 0;
$("#ddlRoles").get(0).options[0] = new Option("Select Role", "-1");
$.each(msg.d, function(index, item) {
$("#ddlRoles").get(0).options[$("#ddlRoles").get(0).options.length] = new Option(item.Display, item.Value);
});
}
});
}
function BindDropdownList() {
BindRolesDropdownList();
}
[WebMethod]
public static ArrayList PopulateSelectRoleList()
{
//ArrayList lst = new ArrayList();
//lst = DataAccess.DataAccess.GetRolesArrayList();
//for (int i = 0; i < lst.Count; i++)
//{
//}
//return lst;
return new ArrayList()
{
new { Value = 1, Display = "Male" },
new { Value = 2, Display = "Female" }
};
}
public static ArrayList GetRolesArrayList()
{
ArrayList aryList = new ArrayList();
DataSet ds = new DataSet();
ds = DBUtility.SQLExecuteDataset("select * from ST_Roles");
foreach (DataRow row in ds.Tables[0].Rows)
{
aryList.Add(row);
}
return aryList;
}
上面選擇的選項列表是它填補我的選擇選項的代碼,我的問題是如何通過ArrayList的迭代,並從數據庫返回的值,而不是在我的代碼部分傳遞的靜態值評論說。填充從ArrayList中
你試過了什麼?你有什麼麻煩? – SLaks
在這裏看:http://stackoverflow.com/questions/7895205/creating-dropdown-selectoption-elements-with-javascript/7895287#7895287 – Sam
我從數據庫的值到arraylist,如果你看看註釋部分下面的代碼有返回類型的ArrayList。我希望我的數據庫中的ArrayList能夠以這種格式返回新的ArrayList() { {value = 1,Display =「Male」}, new {Value = 2,Display =「Female」} }; – Tan